Skip to content

Instantly share code, notes, and snippets.

@kandros
Created December 17, 2017 15:58
Show Gist options
  • Save kandros/a7bdd66bc8d5d63ee9929bf951c32c43 to your computer and use it in GitHub Desktop.
Save kandros/a7bdd66bc8d5d63ee9929bf951c32c43 to your computer and use it in GitHub Desktop.
to calculate excel column name from numbers
/**
* Takes a positive integer and returns the corresponding column name.
* @param {number} num The positive integer to convert to a column name.
* @return {string} The column name.
*/
function toColumnName(num) {
for (var ret = '', a = 1, b = 26; (num -= a) >= 0; a = b, b *= 26) {
ret = String.fromCharCode(parseInt((num % b) / a) + 65) + ret;
}
return ret;
}
// toColumnName(1) 'A'
// toColumnName(26) 'Z'
// toColumnName(27) 'AA'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment