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/models/common/A1Key.ts
-rw-r--r--
1170
 1import { ParseException } from "../../errors/ParseException";
 2import { AlphaUtils } from "../../utils/AlphaUtils";
 3import { ColumnRowKey } from "./ColumnRowKey";
 4
 5/**
 6 * A1 key is mostly a helper-class to name cells when testing.
 7 */
 8export class A1Key {
 9  private static CRUDE_A1_PATTERN = /^(\D+)(\d+)$/;
10  readonly column: string;
11  readonly row: number;
12
13  constructor(column: string, row: number) {
14    this.column = column;
15    this.row = row;
16  }
17
18  /**
19   * Create key from A1 string.
20   *
21   * @param key - key
22   * @return A1Key
23   */
24  static fromString(key: string): A1Key {
25    const matches = key.match(A1Key.CRUDE_A1_PATTERN);
26    if (matches) {
27      return new A1Key(matches[1], AlphaUtils.rowToInt(matches[2]));
28    }
29    throw new ParseException("Could not parse cell key.");
30  }
31
32  toA1String() {
33    return `${this.column}${this.row.toString()}`;
34  }
35
36  /**
37   * Zero-indexed column.
38   */
39  getColumnIndex() {
40    return AlphaUtils.columnToInt(this.column);
41  }
42
43  /**
44   * Convert to ColumnRowKey, which is what we use almost everywhere.
45   */
46  toColumnRowKey(): ColumnRowKey {
47    return new ColumnRowKey(this.getColumnIndex(), this.row);
48  }
49}