Created
December 20, 2022 04:53
-
-
Save JayGoldberg/a4741ab55ab5a945aeaa36d6198f740a to your computer and use it in GitHub Desktop.
Convert alphabetical spreadsheet column ID to a numeric index (Google Sheets, etc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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