getLastColumn method

int getLastColumn()

Gets the last column index / column count.

// Create a new Excel Document.
final Workbook workbook = Workbook(1);
// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];
final Range range = sheet.getRangeByName('A1:D4');
range.setText('Hi');

// get first row.
print(sheet.getFirstRow());
// get last row.
print(sheet.getLastRow());
// get first column.
print(sheet.getFirstColumn());
// get last Column.
print(sheet.getLastColumn());

// Save and dispose workbook.
final List<int> bytes = workbook.saveAsStream();
File('Output.xlsx').writeAsBytes(bytes);
workbook.dispose();

Implementation

int getLastColumn() {
  final int firstRow = getFirstRow();
  if (firstRow != -1) {
    int firstCol = 1;
    for (int i = firstRow; i <= rows.count; i++) {
      final Row? row = rows[i];
      if (row != null) {
        for (final Range? cell in row.ranges.innerList) {
          if (cell != null && firstCol < cell._index) {
            firstCol = cell._index;
          }
        }
      }
    }
    return firstCol;
  }
  return -1;
}