-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path168. ExcelSheetColumnTitle.js
More file actions
35 lines (35 loc) · 1.05 KB
/
Copy path168. ExcelSheetColumnTitle.js
File metadata and controls
35 lines (35 loc) · 1.05 KB
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
var convertToTitle = function(columnNumber) {
// [1...26, 27...53, 54...80]
let 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']
let title = '';
let remainder = columnNumber % 26
let quotient = Math.floor(columnNumber / 26)
if(columnNumber === 52){
return 'AZ'
}
if(remainder === 0){
if((columnNumber / 26) % 1 === 0){
while(columnNumber/26 >= 1){
title = 'Z' + title
columnNumber = columnNumber/26
}
}
return title
}
else
title = alphabet[remainder-1] + title
while(quotient > 0){
remainder = quotient % 26
quotient = Math.floor(quotient / 26)
if(remainder === 0){
title = 'Z' + title
break
}
else
title = alphabet[remainder-1] + title
}
// title = alphabet[remainder-1] + title
// console.log([title])
return title
};
// console.log(convertToTitle(52))