Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
fix(fix number to column title bug): if column title is big , it will…
Browse files Browse the repository at this point in the history
… show undefined

rewrite ABCatNum and chatatABC method  in  util.js
  • Loading branch information
DR-Univer committed Sep 26, 2020
1 parent a0b4c3e commit 2d6e73e
Showing 1 changed file with 86 additions and 48 deletions.
134 changes: 86 additions & 48 deletions src/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,70 +117,108 @@ function rgbTohex(color) {
};

//列下标 字母转数字
function ABCatNum(abc) {
abc = abc.toUpperCase();
function ABCatNum(a) {
// abc = abc.toUpperCase();

let abc_len = abc.length;
if (abc_len == 0) {
return NaN;
}
// let abc_len = abc.length;
// if (abc_len == 0) {
// return NaN;
// }

let abc_array = abc.split("");
let wordlen = columeHeader_word.length;
let ret = 0;
// let abc_array = abc.split("");
// let wordlen = columeHeader_word.length;
// let ret = 0;

// for (let i = abc_len - 1; i >= 0; i--) {
// if (i == abc_len - 1) {
// ret += columeHeader_word_index[abc_array[i]];
// }
// else {
// ret += Math.pow(wordlen, abc_len - i - 1) * (columeHeader_word_index[abc_array[i]] + 1);
// }
// }

for (let i = abc_len - 1; i >= 0; i--) {
if (i == abc_len - 1) {
ret += columeHeader_word_index[abc_array[i]];
}
else {
ret += Math.pow(wordlen, abc_len - i - 1) * (columeHeader_word_index[abc_array[i]] + 1);
}
// return ret;
if(a==null || a.length==0){
return NaN;
}

return ret;
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);
};
// console.log(a, numout-1);
if(numout==0){
return NaN;
}
return numout-1;
};

//列下标 数字转字母
function chatatABC(index) {
let wordlen = columeHeader_word.length;
function chatatABC(n) {
// let wordlen = columeHeader_word.length;

if (index < wordlen) {
return columeHeader_word[index];
}
else {
let last = 0, pre = 0, ret = "";
let i = 1, n = 0;
// if (index < wordlen) {
// return columeHeader_word[index];
// }
// else {
// let last = 0, pre = 0, ret = "";
// let i = 1, n = 0;

while (index >= (wordlen / (wordlen - 1)) * (Math.pow(wordlen, i++) - 1)) {
n = i;
}
// while (index >= (wordlen / (wordlen - 1)) * (Math.pow(wordlen, i++) - 1)) {
// n = i;
// }

let index_ab = index - (wordlen / (wordlen - 1)) * (Math.pow(wordlen, n - 1) - 1);//970
last = index_ab + 1;
// let index_ab = index - (wordlen / (wordlen - 1)) * (Math.pow(wordlen, n - 1) - 1);//970
// last = index_ab + 1;

for (let x = n; x > 0; x--) {
let last1 = last, x1 = x;//-702=268, 3
// for (let x = n; x > 0; x--) {
// let last1 = last, x1 = x;//-702=268, 3

if (x == 1) {
last1 = last1 % wordlen;
// if (x == 1) {
// last1 = last1 % wordlen;

if (last1 == 0) {
last1 = 26;
}
// if (last1 == 0) {
// last1 = 26;
// }

return ret + columeHeader_word[last1 - 1];
}
// return ret + columeHeader_word[last1 - 1];
// }

last1 = Math.ceil(last1 / Math.pow(wordlen, x - 1));
//last1 = last1 % wordlen;
ret += columeHeader_word[last1 - 1];
// last1 = Math.ceil(last1 / Math.pow(wordlen, x - 1));
// //last1 = last1 % wordlen;
// ret += columeHeader_word[last1 - 1];

if (x > 1) {
last = last - (last1 - 1) * wordlen;
}
}
}
// if (x > 1) {
// last = last - (last1 - 1) * wordlen;
// }
// }
// }

var orda = 'a'.charCodeAt(0);

var ordz = 'z'.charCodeAt(0);

var len = ordz - orda + 1;

var s = "";

while( n >= 0 ) {

s = String.fromCharCode(n % len + orda) + s;

n = Math.floor(n / len) - 1;

}

return s.toUpperCase();
};

function ceateABC(index) {
Expand Down

0 comments on commit 2d6e73e

Please sign in to comment.