getColName function

String getColName(
  1. String start,
  2. int n
)

Returns the Excel Style column name based on thestart column and n no. of cols after it.

Example: getColName('A', 1) => 'B' Example: getColName('YA', 10) => 'YK'

Implementation

String getColName(String start, int n) {
  int ascii = start.codeUnitAt(0);
  ascii += n;

  if (ascii > 90) {
    int overflow = ascii - 90;
    int firstChar = 64 + (overflow / 26).floor();
    int secondChar = 64 + (overflow % 26);
    if (secondChar == 64) {
      firstChar -= 1;
      secondChar = 90;
    }
    return String.fromCharCode(firstChar + 1) + String.fromCharCode(secondChar);
  } else {
    return String.fromCharCode(ascii);
  }
}