getColumnIndex method

int getColumnIndex(
  1. String columnName
)

Converts column name into index.

// Create a new Excel Document.
final Workbook workbook = Workbook(1);
// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];
// get column Index.
print(sheet.getColumnIndex('AA'));
// Save and dispose workbook.
final List<int> bytes = workbook.saveAsStream();
File('Output.xlsx').writeAsBytes(bytes);
workbook.dispose();

Implementation

int getColumnIndex(String columnName) {
  if (columnName.isEmpty) {
    final Error error =
        ArgumentError('columnName - name cannot be less then 1 symbols.');
    throw error;
  }

  int iColumn = 0;

  // ignore: prefer_final_locals
  for (int i = 0, len = columnName.length; i < len; i++) {
    final String currentChar = columnName[i];
    iColumn *= 26;
    iColumn += 1 +
        ((currentChar.codeUnitAt(0) >= 'a'.codeUnitAt(0))
            ? (currentChar.codeUnitAt(0) - 'a'.codeUnitAt(0))
            : (currentChar.codeUnitAt(0) - 'A'.codeUnitAt(0)));
  }
  if (iColumn < 0) {
    iColumn = -iColumn;
  }
  return iColumn;
}