1. 程式人生 > 其它 >js excel裡面的字母和數字的轉換處理

js excel裡面的字母和數字的轉換處理

字母轉數字

function stringTonum(a) {
  var str = a.toLowerCase().split("");
  var num = 0;
  var al = str.length;
  var getCharNumber = function (charx) {
    return charx.charCodeAt() - 96;
  };
  var numout = 0;
  var charnum = 0;
  for (var i = 0; i < al; i++) {
    charnum = getCharNumber(str[i]);
    numout += charnum * Math.pow(26, al - i - 1);
  };
  return numout;
}

//呼叫

stringTonum('A')

數字轉字母

function numToString(numm) {
  var stringArray = [];
  var numToStringAction = function (nnum) {
    var num = nnum - 1;
    var a = parseInt(num / 26);
    var b = num % 26;
    stringArray.push(String.fromCharCode(64 + parseInt(b + 1)));
    if (a > 0) {
      numToStringAction(a);
    }
  }
  numToStringAction(numm);
  return stringArray.reverse().join("");
}

//呼叫

numToString(28)

//轉載:https://blog.csdn.net/a19352226/article/details/82422403