f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/main/js/common/utils/AlphaUtils.ts
-rw-r--r--
1213
 1export class AlphaUtils {
 2  /**
 3   * Convert a column in A1 notation (where column is A, B, C... ZA, ZB... etc.) to integer (A=1, B=2)
 4   *
 5   * @param column to convert to zero-indexed int.
 6   * @return int greater than or equal to 1.
 7   */
 8  static columnToInt(column: string): number {
 9    const base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
10    let i,
11      j,
12      result = 0;
13    for (i = 0, j = column.length - 1; i < column.length; i += 1, j -= 1) {
14      result += Math.pow(base.length, j) * (base.indexOf(column[i]) + 1);
15    }
16    if (result) {
17      --result;
18    }
19    return result;
20  }
21
22  /**
23   * Convert a row string to row index, which is zero indexed. This is simple, but I don't want to repeat it everywhere.
24   * @param row - string integer greater than or equal to 1.
25   */
26  static rowToInt(row: string): number {
27    return parseInt(row) - 1;
28  }
29
30  /**
31   * Take a one-indexed number, and return the letter. Eg: 1=A, 2=B, etc.
32   * @param num
33   */
34  static oneIndexedNumberToLetter(num: number) {
35    let ret, a, b;
36    for (ret = "", a = 1, b = 26; (num -= a) >= 0; a = b, b *= 26) {
37      ret = String.fromCharCode(parseInt(((num % b) / a).toString()) + 65) + ret;
38    }
39    return ret;
40  }
41}