-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterToColumn.js
More file actions
40 lines (35 loc) · 1.26 KB
/
LetterToColumn.js
File metadata and controls
40 lines (35 loc) · 1.26 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
36
37
38
39
40
const columnToLetter = (column) => {
let temp, letter = '';
while (column > 0) {
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
const letterToColumn = (letter) => {
letter = letter.toUpperCase();
let column = 0, length = letter.length;
for (let i = 0; i < length; i++) {
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return column;
}
/**
* convert simple range sheet (A2) to understandable numeric value for javascript loops
* Example with A2 : column A becomes number 0, row 2 becomes number 1
* @param {String} range
* @returns {Object} contains column and row number
*/
const rangeSheetToNumericJs = (range) => {
let searchDigital = parseInt(range.match(/\d+/g));
resultDigit = isNaN(searchDigital) ? undefined : searchDigital; // check if a value is NaN or not
const row = searchDigital.length > 0 ? searchDigital[0] : searchDigital;
let letter = range.match(/[a-zA-Z]+/g)[0];
letter = letter.toUpperCase();
let column = 0, length = letter.length;
for (let i = 0; i < length; i++) {
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return { column: column - 1, row: row - 1 || undefined };
}