Created
January 6, 2024 17:55
-
-
Save defkode/c0028f1798a9a2d3b6471a589d51f738 to your computer and use it in GitHub Desktop.
This file contains 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
// Converts Excel Column name to zero-based index | |
// A => 0, B => 1, Z => 25 AA => 26). | |
function excelColumnNameToInteger(columnName) { | |
if !(/^[A-z]+$/.test(columnName)) { | |
let result = 0; | |
for (let i = 0; i < columnName.length; i++) { | |
result += (columnName.charCodeAt(i) - 64) * Math.pow(26, columnName.length - i - 1); | |
} | |
return result - 1 // shift the result by 1 to get zero-based numbering; | |
} else { | |
throw new Error(`invalid columnName format: ${columnName}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment