我正在开发一个程序,我需要找出给定excel列索引的索引/位置。
如果我通过A
,它应该返回1
如果我通过AA
它应该返回27
如果我通过AB
它应该返回28
如果我通过AAA
它应该返回26 * 26 * 26(不确定但想要获得实际位置)
到目前为止我做了什么
var str = "AB";
var d = 0;
for (var i = 1, m = 26; i < string.length ; i++) {
if(string.length === 1) {
var d = parseInt(string.charCodeAt(i) - index);
} else if (string.length === 2){
var d = parseInt(((string.charCodeAt(i) - index) * m ) + (i));
} else {
var d = parseInt(((string.charCodeAt(i) - index) * m ) + (i));
}
}
答案 0 :(得分:0)
var str = "MM19";
var number_regex = /[+-]?\d+(\.\d+)?/g;
var matches = [];
str.replace(number_regex, function (match) {
matches.push(match);
});
str = str.replace(/[0-9]/g, '');
matches.push(str);
console.log(matches[1]);
var index = 64;
var string = matches[1];
var columnIndex = 0;
var counter = 0;
var baseValue;
var m = 26;
console.log("length is:", string.length);
for (var i = string.length-1; i >= 0; i--) {
columnIndex = columnIndex + (string.charCodeAt(i) - index) * Math.pow(m, counter);
counter++;
}
console.log("index value", columnIndex);