Created
December 17, 2017 15:58
-
-
Save kandros/a7bdd66bc8d5d63ee9929bf951c32c43 to your computer and use it in GitHub Desktop.
to calculate excel column name from numbers
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
| /** | |
| * 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