insertColumn method
Inserts an empty column for the specified column index.
// Create a new Excel Document.
final Workbook workbook = Workbook();
// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];
Range range = sheet.getRangeByName('A1');
range.setText('Hello');
range = sheet.getRangeByName('B1');
range.setText('World');
// Insert a column.
sheet.insertColumn(2, 1, ExcelInsertOptions.formatAsBefore);
// Save and dispose workbook.
final List<int> bytes = workbook.saveAsStream();
File('InsertColumn.xlsx').writeAsBytes(bytes);
workbook.dispose();
Implementation
void insertColumn(int columnIndex,
[int? columnCount, ExcelInsertOptions? insertOptions]) {
if (columnIndex < 1 || columnIndex > workbook.maxColumnCount) {
throw Exception(
'Value cannot be less 1 and greater than max column index.');
}
columnCount ??= 1;
if (columnCount < 0) {
throw Exception('count');
}
insertOptions ??= ExcelInsertOptions.formatDefault;
final int firstRow = getFirstRow();
final int lastRow = getLastRow();
final int lastColumn = getLastColumn();
if (rows.count > 0) {
for (int i = lastRow; i >= firstRow; i--) {
if (rows[i] != null) {
for (int count = 1; count <= columnCount; count++) {
for (int j = lastColumn + columnCount; j >= columnIndex; j--) {
final Range? range = rows[i]!.ranges[j];
if (range == null &&
j != columnIndex &&
rows[i]!.ranges[j - 1] != null) {
rows[i]!.ranges[j] = Range(this);
rows[i]!.ranges[j] = rows[i]!.ranges[j - 1];
rows[i]!.ranges[j]!.index = j;
rows[i]!.ranges[j]!.row = rows[i]!.ranges[j]!.row;
rows[i]!.ranges[j]!.lastRow = rows[i]!.ranges[j]!.lastRow;
rows[i]!.ranges[j]!.column = rows[i]!.ranges[j]!.column + 1;
rows[i]!.ranges[j]!.lastColumn =
rows[i]!.ranges[j]!.lastColumn + 1;
} else if (range != null &&
j != columnIndex &&
rows[i]!.ranges[j - 1] != null) {
rows[i]!.ranges[j] = rows[i]!.ranges[j - 1];
rows[i]!.ranges[j]!.index = j;
rows[i]!.ranges[j]!.row = rows[i]!.ranges[j]!.row;
rows[i]!.ranges[j]!.lastRow = rows[i]!.ranges[j]!.lastRow;
rows[i]!.ranges[j]!.column = rows[i]!.ranges[j]!.column + 1;
rows[i]!.ranges[j]!.lastColumn =
rows[i]!.ranges[j]!.lastColumn + 1;
} else if (j == columnIndex &&
rows[i]!.ranges[j] == rows[i]!.ranges[columnIndex]) {
if (insertOptions == ExcelInsertOptions.formatAsBefore) {
if (rows[i]!.ranges[j - 1] != null) {
rows[i]!.ranges[j] = Range(this);
rows[i]!.ranges[j]!.index = j;
rows[i]!.ranges[j]!.row = rows[i]!.ranges[j - 1]!.row;
rows[i]!.ranges[j]!.lastRow =
rows[i]!.ranges[j - 1]!.lastRow;
rows[i]!.ranges[j]!.column =
rows[i]!.ranges[j - 1]!.column + 1;
rows[i]!.ranges[j]!.lastColumn =
rows[i]!.ranges[j - 1]!.lastColumn + 1;
rows[i]!.ranges[j]!.cellStyle =
rows[i]!.ranges[j - 1]!.cellStyle;
} else {
rows[i]!.ranges[j] = null;
}
} else if (insertOptions == ExcelInsertOptions.formatAsAfter) {
if (rows[i]!.ranges[j + 1] != null) {
rows[i]!.ranges[j] = Range(this);
rows[i]!.ranges[j]!.index = j;
rows[i]!.ranges[j]!.row = rows[i]!.ranges[j + 1]!.row;
rows[i]!.ranges[j]!.lastRow =
rows[i]!.ranges[j + 1]!.lastRow;
rows[i]!.ranges[j]!.column =
rows[i]!.ranges[j + 1]!.column - 1;
rows[i]!.ranges[j]!.lastColumn =
rows[i]!.ranges[j + 1]!.lastColumn - 1;
rows[i]!.ranges[j]!.cellStyle =
rows[i]!.ranges[j + 1]!.cellStyle;
} else {
rows[i]!.ranges[j] = null;
}
} else {
rows[i]!.ranges[j] = null;
}
} else {
rows[i]!.ranges[j] = null;
}
}
}
}
}
if (hyperlinks.count > 0) {
for (final Hyperlink link in hyperlinks.innerList) {
if (link.attachedType == ExcelHyperlinkAttachedType.range &&
link.column >= columnIndex) {
link.column = link.column + columnCount;
}
}
}
if (insertOptions == ExcelInsertOptions.formatAsAfter) {
for (int count = 1; count <= columnCount; count++) {
for (int j = lastColumn + columnCount; j > columnIndex; j--) {
if (j > 1) {
if (columns[j] == null && columns[j - 1] == null) {
columns[j - 1] = Column(this);
columns[j - 1]!.index = j - 1;
columns[j] = Column(this);
columns[j]!.index = j;
} else if (columns[j] == null && columns[j - 1] != null) {
columns[j] = Column(this);
columns[j]!.index = j;
columns[j]!.width = columns[j - 1]!.width;
} else if (columns[j] != null && columns[j - 1] == null) {
columns[j - 1] = Column(this);
columns[j - 1]!.index = j - 1;
columns[j]!.width = columns[j - 1]!.width;
} else {
columns[j]!.width = columns[j - 1]!.width;
}
} else {
if (columns[j] == null) {
columns[j] = Column(this);
columns[j]!.index = j;
}
columns[j]!.width = 0.0;
}
}
}
} else if (insertOptions == ExcelInsertOptions.formatAsBefore ||
insertOptions == ExcelInsertOptions.formatDefault) {
for (int count = 1; count <= columnCount; count++) {
for (int j = lastColumn + columnCount; j >= columnIndex; j--) {
if (j > 1) {
if (columns[j] == null && columns[j - 1] == null) {
columns[j - 1] = Column(this);
columns[j - 1]!.index = j - 1;
columns[j] = Column(this);
columns[j]!.index = j;
} else if (columns[j] == null && columns[j - 1] != null) {
columns[j] = Column(this);
columns[j]!.index = j;
columns[j]!.width = columns[j - 1]!.width;
} else if (columns[j] != null && columns[j - 1] == null) {
columns[j - 1] = Column(this);
columns[j - 1]!.index = j - 1;
columns[j]!.width = columns[j - 1]!.width;
} else {
columns[j]!.width = columns[j - 1]!.width;
}
} else {
if (columns[j] == null) {
columns[j] = Column(this);
columns[j]!.index = j;
}
columns[j]!.width = 0.0;
}
}
}
}
if (insertOptions == ExcelInsertOptions.formatDefault) {
for (int z = columnIndex; z < columnIndex + columnCount; z++) {
if (columns[z] == null) {
columns[z] = Column(this);
columns[z]!.index = z;
}
columns[z]!.width = 0.0;
}
}
}
}