Skip to content

Instantly share code, notes, and snippets.

@JayGoldberg
Created December 20, 2022 04:53
Show Gist options
  • Save JayGoldberg/a4741ab55ab5a945aeaa36d6198f740a to your computer and use it in GitHub Desktop.
Save JayGoldberg/a4741ab55ab5a945aeaa36d6198f740a to your computer and use it in GitHub Desktop.
Convert alphabetical spreadsheet column ID to a numeric index (Google Sheets, etc)
// given a column 'A'...'AB' etc, return a numeric index
function columnToIndex(column) {
// Initialize the index to 0
var index = 0;
// Iterate through the characters in the column string
for (var i = 0; i < column.length; i++) {
// Get the character at the current index
var char = column.charAt(i);
// Convert the character to a number (A = 1, B = 2, etc.)
var num = char.charCodeAt(0) - 64;
// Multiply the number by 26 to the power of the index
// (A = 1, AA = 26, AAA = 676, etc.)
num *= Math.pow(26, column.length - i - 1);
// Add the number to the index
index += num;
}
// Return the index
return index-1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment