-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcelSheetColumTile.js
51 lines (37 loc) · 1013 Bytes
/
excelSheetColumTile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @param {number} columnNumber
* @return {string}
*/
var convertToTitle = function (columnNumber) {
// const alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
// const alpha = Array.from(Array(26)).map((e, i) => i + 65);
// const alphabet = alpha.map((x) => String.fromCharCode(x));
const alphabet = [...'abcdefghijklmnopqrstuvwxyz'];
const array = breakNum(columnNumber)
let toAlpha = array.map((value) => {
let index = value - 1
return alphabet[index]
})
return toAlpha.join('').toUpperCase()
};
const breakNum = (num) => {
const arr = []
let cn = num
let length = 26
while (true) {
let mod = cn % length
if (cn <= 26) {
arr.unshift(cn)
break;
}
if (mod === 0) {
mod = 26
cn = cn - 1
}
arr.unshift(mod)
cn = Math.floor(cn / 26)
}
return arr
}
console.log(convertToTitle(28));
console.log(convertToTitle(702));