Created
November 3, 2011 01:47
-
-
Save ynakajima/1335548 to your computer and use it in GitHub Desktop.
Excel列名変換問題回答 JS版
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
#!/usr/bin/js | |
var mode = arguments[0]; | |
if ( mode == 0 ) { | |
var string = arguments[1]; | |
var number = 0; | |
for (var i = 0, iMax = string.length; i < iMax; i ++) { | |
var chr = string.slice(i, i + 1); | |
var num = chr.charCodeAt(0) - 64; | |
number += Math.pow(26, (iMax - i - 1)) * num; | |
} | |
print(number); | |
} else if ( mode == 1) { | |
var number = Number(arguments[1]); | |
var string = []; | |
while (number > 26) { | |
if (number % 26) { | |
var n = number % 26; | |
number = parseInt(number / 26); | |
} else { | |
var n = 26; | |
number = parseInt(number / 26) - 1; | |
} | |
string.push(String.fromCharCode(n + 64)); | |
} | |
if (number > 0) string.push(String.fromCharCode(number + 64)); | |
string.reverse(); | |
print(string.join("")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment