Skip to content

Instantly share code, notes, and snippets.

@defkode
Created January 6, 2024 17:55
Show Gist options
  • Save defkode/c0028f1798a9a2d3b6471a589d51f738 to your computer and use it in GitHub Desktop.
Save defkode/c0028f1798a9a2d3b6471a589d51f738 to your computer and use it in GitHub Desktop.
// 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