insertRow method
Inserts an empty row in the specified row 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 row
sheet.insertRow(1, 1, ExcelInsertOptions.formatAsAfter);
// Insert a column.
sheet.insertColumn(2, 1, ExcelInsertOptions.formatAsBefore);
// Save and dispose workbook.
final List<int> bytes = workbook.saveAsStream();
File('InsertRow.xlsx').writeAsBytes(bytes);
workbook.dispose();
Implementation
void insertRow(int rowIndex,
[int? rowCount, ExcelInsertOptions? insertOptions]) {
if (rowIndex < 1 || rowIndex > workbook._maxRowCount) {
throw Exception('rowIndex');
}
rowCount ??= 1;
if (rowCount < 0) {
throw Exception('count');
}
insertOptions ??= ExcelInsertOptions.formatDefault;
final bool isLastRow = (rowIndex + rowCount) >= workbook._maxRowCount;
const int columnIndex = 1;
final int lastRow = getLastRow();
if (!isLastRow) {
for (int count = 1; count <= rowCount; count++) {
for (int i = lastRow + rowCount; i >= rowIndex; i--) {
final Row? row = rows[i];
if (row == null && i != rowIndex && rows[i - 1] != null) {
rows[i] = Row(this);
rows[i] = rows[i - 1];
rows[i]!.index = rows[i]!.index + 1;
for (int j = rows[i]!.ranges.innerList.length; j >= 1; j--) {
final Range? range = rows[i]!.ranges[j];
if (range != null) {
rows[i]!.ranges[j]!.row = rows[i]!.ranges[j]!.row + 1;
rows[i]!.ranges[j]!.lastRow = rows[i]!.ranges[j]!.lastRow + 1;
rows[i]!.ranges[j]!.column = rows[i]!.ranges[j]!.column;
rows[i]!.ranges[j]!.lastColumn = rows[i]!.ranges[j]!.lastColumn;
}
}
} else if (row != null && i != rowIndex && rows[i - 1] != null) {
rows[i] = rows[i - 1];
rows[i]!.index = rows[i]!.index + 1;
for (int j = rows[i]!.ranges.innerList.length; j >= 1; j--) {
final Range? range = rows[i]!.ranges[j];
if (range != null) {
rows[i]!.ranges[j]!.row = rows[i]!.ranges[j]!.row + 1;
rows[i]!.ranges[j]!.lastRow = rows[i]!.ranges[j]!.lastRow + 1;
rows[i]!.ranges[j]!.column = rows[i]!.ranges[j]!.column;
rows[i]!.ranges[j]!.lastColumn = rows[i]!.ranges[j]!.lastColumn;
}
}
} else if (i == rowIndex) {
rows[i] = Row(this);
rows[i]!.index = rowIndex;
if (insertOptions == ExcelInsertOptions.formatAsBefore) {
if (rows[i - 1] != null) {
if (rows[i - 1]!.height != 0) {
rows[i]!.height = rows[i - 1]!.height;
}
for (int z = 1;
z <= rows[i - 1]!.ranges.innerList.length;
z++) {
if (rows[i - 1]!.ranges[z] != null) {
rows[i]!.ranges[z] = Range(this);
rows[i]!.ranges[z]!._index = rows[i - 1]!.ranges[z]!._index;
rows[i]!.ranges[z]!.row = rows[i - 1]!.ranges[z]!.row + 1;
rows[i]!.ranges[z]!.lastRow =
rows[i - 1]!.ranges[z]!.lastRow + 1;
rows[i]!.ranges[z]!.column = rows[i - 1]!.ranges[z]!.column;
rows[i]!.ranges[z]!.lastColumn =
rows[i - 1]!.ranges[z]!.lastColumn;
rows[i]!.ranges[z]!.cellStyle =
rows[i - 1]!.ranges[z]!.cellStyle;
}
}
}
} else if (insertOptions == ExcelInsertOptions.formatAsAfter) {
if (rows[i + 1] != null) {
if (rows[i + 1]!.height != 0) {
rows[i]!.height = rows[i + 1]!.height;
}
for (int z = 1;
z <= rows[i + 1]!.ranges.innerList.length;
z++) {
if (rows[i + 1]!.ranges[z] != null) {
rows[i]!.ranges[z] = Range(this);
rows[i]!.ranges[z]!._index = rows[i + 1]!.ranges[z]!._index;
rows[i]!.ranges[z]!.row = rows[i + 1]!.ranges[z]!.row - 1;
rows[i]!.ranges[z]!.lastRow =
rows[i + 1]!.ranges[z]!.lastRow - 1;
rows[i]!.ranges[z]!.column = rows[i + 1]!.ranges[z]!.column;
rows[i]!.ranges[z]!.lastColumn =
rows[i + 1]!.ranges[z]!.lastColumn;
rows[i]!.ranges[z]!.cellStyle =
rows[i + 1]!.ranges[z]!.cellStyle;
}
}
}
} else {
rows[i]!.ranges[columnIndex] = null;
}
if (hyperlinks.count > 0) {
for (final Hyperlink link in hyperlinks.innerList) {
if (link._attachedType == ExcelHyperlinkAttachedType.range &&
link._row >= rowIndex) {
link._row = link._row + 1;
}
}
}
} else {
rows[i] = Row(this);
rows[i]!.index = i;
}
}
}
}
}