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/spreadsheet/Sheet.ts
-rw-r--r--
2178
 1import { A1Key } from "../models/common/A1Key";
 2import { ColumnRowKey } from "../models/common/ColumnRowKey";
 3import { Parsers } from "../utils/Parsers";
 4import { CellObject } from "./CellObject";
 5
 6/**
 7 * A sheet in a spreadsheet, or spreadsheet-like object.
 8 */
 9export interface ISheet {
10  /**
11   * Unique identifier for the sheet. 7 characters, first three are 'she'.
12   */
13  id: string;
14  /**
15   * Unique name of the sheet.
16   */
17  name: string;
18
19  /** Sheet Range */
20  "!ref"?: string;
21
22  /**
23   * Indexing with a cell address string maps to a cell object
24   * Special keys start with '!'
25   */
26  [cell: string]: CellObject | string;
27}
28
29export class Sheet implements ISheet {
30  static properties = new Set(["id", "name", "!ref"]);
31
32  id: string;
33  name: string;
34
35  [cell: string]: CellObject | any;
36
37  "!ref"?: string;
38
39  constructor(sheet: ISheet) {
40    const ref = Parsers.parseReferencePair(sheet["!ref"] || "A1:A1");
41    let highestColumn = ref.endColumnIndex;
42    let highestRow = ref.endRowIndex;
43    Object.keys(sheet).map((key) => {
44      if (!Sheet.properties.has(key)) {
45        const a1 = A1Key.fromString(key);
46        highestColumn = Math.max(highestColumn, a1.getColumnIndex());
47        highestRow = Math.max(highestRow, a1.row);
48      }
49      this[key] = sheet[key];
50    });
51    const start = new ColumnRowKey(ref.startColumnIndex, ref.startRowIndex).toA1();
52    const end = new ColumnRowKey(highestColumn, highestRow).toA1();
53    this["!ref"] = sheet["!ref"] || `${start}:${end}`;
54  }
55
56  setCell(a1: string, cell: CellObject) {
57    const ref = Parsers.parseReferencePair(this["!ref"] || "A1:A1");
58    let highestColumn = ref.endColumnIndex;
59    let highestRow = ref.endRowIndex;
60    const key = A1Key.fromString(a1);
61    highestColumn = Math.max(highestColumn, key.getColumnIndex());
62    highestRow = Math.max(highestRow, key.row);
63    const start = new ColumnRowKey(ref.startColumnIndex, ref.startRowIndex).toA1();
64    const end = new ColumnRowKey(highestColumn, highestRow).toA1();
65    this[a1] = cell;
66    this["!ref"] = `${start}:${end}`;
67  }
68
69  getCell(a1: string): CellObject | null {
70    const value = this[a1];
71    return value ? value : null;
72  }
73}