Get column index by letter Apps Script

This function is useful if you use a full range from column A.

function getIndexByColumn(letter) {
    var column = 0, length = letter.length;
    for (var i = 0; i < length; i++){
      column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
    }
    return column - 1;
}

// Test
getIndexByColumn('AZ'); // 51
getIndexByColumn('AA'); // 26
getIndexByColumn('A'); // 0
getIndexByColumn('Z'); // 25

Leave a Comment