Skip to content

Instantly share code, notes, and snippets.

@omarluq
Last active October 15, 2025 19:48
Show Gist options
  • Select an option

  • Save omarluq/735a979d9ff4e2be2bdbb98a723b7f63 to your computer and use it in GitHub Desktop.

Select an option

Save omarluq/735a979d9ff4e2be2bdbb98a723b7f63 to your computer and use it in GitHub Desktop.
function colNameToNum(col) {
let result = 0;
let colName = col.toUpperCase();
for (let i = 0; i < colName.length; i++) {
// A=1, B=2, ... Z=26
const charValue = colName.charCodeAt(i) - "A".charCodeAt(0) + 1;
// Base-26 conversion
result = result * 26 + charValue;
}
// Convert to 0-indexed
return result - 1;
}
console.log(colNameToNum("AA")); // 26
console.log(colNameToNum("a")); // 0
console.log(colNameToNum("B")); // 1
console.log(colNameToNum("z")); // 25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment