spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Cell, Sheet] cleaning up the internal Cell model
author
Ben Vogt <[email protected]>
date
2017-05-10 04:44:39
stats
3 file(s) changed, 47 insertions(+), 49 deletions(-)
files
src/Cell.ts
src/Sheet.ts
tests/CellTest.ts
  1diff --git a/src/Cell.ts b/src/Cell.ts
  2index fe5f092..52fc98b 100644
  3--- a/src/Cell.ts
  4+++ b/src/Cell.ts
  5@@ -1,12 +1,16 @@
  6 /**
  7- * Cell represents a cell in the spreadsheet. It contains a nullable formula, and a value, which is not nullable unless
  8- * the parsing of the formula results in an error.
  9+ * Cell represents a cell in the spreadsheet. It contains a nullable rawFormulaText, and a value, which is not nullable unless
 10+ * the parsing of the rawFormulaText results in an error.
 11  */
 12 class Cell {
 13-  private formula: string;
 14-  private value: string;
 15-  private dependencies: Array<string>;
 16-  private error: string;
 17+  /**
 18+   * The raw formula text that can be parse, excluding the proceeding =
 19+   * E.g: SUM(A2:A4, 10)
 20+   */
 21+  private rawFormulaText: string = null;
 22+  private typedValue: any = null;
 23+  private dependencies: Array<string> = [];
 24+  private error: string = null;
 25   private id: string;
 26   private row: number;
 27   private col: number;
 28@@ -18,10 +22,6 @@ class Cell {
 29   constructor(id: string) {
 30     var key = parseKey(id);
 31 
 32-    this.formula = null;
 33-    this.value = null;
 34-    this.dependencies = [];
 35-    this.error = null;
 36     this.id = id;
 37     this.row = key.y;
 38     this.col = key.x;
 39@@ -41,7 +41,7 @@ class Cell {
 40 
 41   /**
 42    * Return a list of dependencies in A1-format cell IDs, in no particular order, but likely in order of occurrence in
 43-   * formula.
 44+   * rawFormulaText.
 45    * @returns {Array<string>} list of dependencies in A1-format
 46    */
 47   getDependencies() : Array<string> {
 48@@ -73,60 +73,60 @@ class Cell {
 49   }
 50 
 51   /**
 52-   * Set the formula of this cell.
 53-   * @param formula to set.
 54+   * Get the rawFormulaText of this cell if set. Defaults to null, so should be used in combination with hasFormula().
 55+   * @returns {string} rawFormulaText of this cell, if set. Nullable.
 56    */
 57-  setFormula(formula: string) {
 58-    this.formula = formula;
 59+  getFormula() : string {
 60+    return this.rawFormulaText;
 61   }
 62 
 63   /**
 64-   * Get the formula of this cell if set. Defaults to null, so could return null.
 65-   * @returns {string} formula of this cell, if set. Nullable.
 66+   * Returns true if this cell has a formula to be parsed.
 67+   * @returns {boolean}
 68    */
 69-  getFormula() : string {
 70-    return this.formula;
 71+  hasFormula() : boolean {
 72+    return this.rawFormulaText !== null;
 73   }
 74 
 75   /**
 76-   * Set the value of this cell. If this cell has a primitive value (does not contain a formula), it could be set to a
 77-   * value while the formula field is still null.
 78+   * Set the value of this cell. If this cell has a primitive value (does not contain a rawFormulaText), it could be set to a
 79+   * value while the rawFormulaText field is still null.
 80    * @param value to set
 81    */
 82-  setValue(value: string) {
 83-    this.value = value;
 84+  setValue(value: any) {
 85+    this.typedValue = value;
 86   }
 87 
 88   /**
 89-   * Sets the value or formula for this cell. If the input begins with =, then it is considered to be a formula. If it
 90+   * Sets the value or rawFormulaText for this cell. If the input begins with =, then it is considered to be a rawFormulaText. If it
 91    * is not, then it is a value, and set as the raw value for this cell.
 92    * @param rawFormula
 93    */
 94-  setRawFormula(rawFormula: string) {
 95+  setRawValue(rawFormula: string) {
 96     if (rawFormula.charAt(0) === "=") {
 97-      this.formula = rawFormula.substr(1);
 98+      this.rawFormulaText = rawFormula.substr(1);
 99     } else {
100-      this.value = rawFormula;
101+      this.typedValue = rawFormula;
102     }
103   }
104 
105   /**
106-   * Get the value of this cell. Since value could be null do to an error in the formula, this could return null.
107-   * @returns {string}
108+   * Get the value of this cell. Since value could be null do to an error in the rawFormulaText, this could return null.
109+   * @returns {any}
110    */
111-  getValue() : string {
112-    return this.value;
113+  getValue() : any {
114+    return this.typedValue;
115   }
116 
117   /**
118    * CLears a cells value.
119    */
120   clearValue() {
121-    this.value = null;
122+    this.typedValue = null;
123   }
124 
125   /**
126-   * Set error for this cell. Usually in the case of a parse error when parsing the formula.
127+   * Set error for this cell. Usually in the case of a parse error when parsing the rawFormulaText.
128    * @param error to set.
129    */
130   setError(error: string) {
131@@ -134,7 +134,7 @@ class Cell {
132   }
133 
134   /**
135-   * Get the error for this cell. If the formula is not parsed properly, or is null, this could be null.
136+   * Get the error for this cell. If the rawFormulaText is not parsed properly, or is null, this could be null.
137    * @returns {string} error to return, could be null.
138    */
139   getError() : string {
140@@ -146,7 +146,7 @@ class Cell {
141    * @returns {string}
142    */
143   toString() : string {
144-    return "id=" + this.id + ", value=" + this.value + ", formula=" + this.formula + ", error=" + this.error;
145+    return "id=" + this.id + ", value=" + this.typedValue + ", rawFormulaText=" + this.rawFormulaText + ", error=" + this.error;
146   }
147 }
148 
149diff --git a/src/Sheet.ts b/src/Sheet.ts
150index 40927f7..0ea5470 100644
151--- a/src/Sheet.ts
152+++ b/src/Sheet.ts
153@@ -88,12 +88,7 @@ var Sheet = (function () {
154         this.data[cellId] = cell;
155       } else {
156         this.getCell(cellId).updateDependencies(cell.getDependencies());
157-        var cellValue = cell.getValue();
158-        if (cellValue === null) {
159-          this.getCell(cellId).clearValue();
160-        } else {
161-          this.getCell(cellId).setValue(cellValue);
162-        }
163+        this.getCell(cellId).setValue(cell.getValue());
164         this.getCell(cellId).setError(cell.getError());
165       }
166 
167@@ -154,7 +149,7 @@ var Sheet = (function () {
168      */
169     setCell(id: string, rawFormula: string) {
170       var cell = new Cell(id);
171-      cell.setRawFormula(rawFormula);
172+      cell.setRawValue(rawFormula);
173       registerCellInMatrix(cell);
174       recalculateCellDependencies(cell);
175     }
176@@ -169,7 +164,7 @@ var Sheet = (function () {
177 
178     allDependencies.forEach(function (refId) {
179       var currentCell = instance.matrix.getCell(refId);
180-      if (currentCell && currentCell.getFormula()) {
181+      if (currentCell && currentCell.hasFormula()) {
182         calculateCellFormula(currentCell);
183       }
184     });
185@@ -184,11 +179,7 @@ var Sheet = (function () {
186     // to avoid double translate formulas, update cell data in parser
187     var parsed = parse(cell.getFormula(), cell.getId());
188 
189-    if (parsed.result === null) {
190-      instance.matrix.getCell(cell.getId()).clearValue();
191-    } else {
192-      instance.matrix.getCell(cell.getId()).setValue(parsed.result);
193-    }
194+    instance.matrix.getCell(cell.getId()).setValue(parsed.result);
195     instance.matrix.getCell(cell.getId()).setError(parsed.error);
196 
197     return parsed;
198@@ -200,7 +191,7 @@ var Sheet = (function () {
199    */
200   var registerCellInMatrix = function (cell: Cell) {
201     instance.matrix.addCell(cell);
202-    if (cell.getFormula() !== null) {
203+    if (cell.hasFormula()) {
204       calculateCellFormula(cell);
205     }
206   };
207@@ -231,7 +222,7 @@ var Sheet = (function () {
208     },
209 
210     isSet: function (value) {
211-      return !utils.isUndefined(value) && !utils.isNull(value);
212+      return !utils.isNull(value);
213     },
214 
215     toNum: function (chr) {
216@@ -469,8 +460,8 @@ var Sheet = (function () {
217         origin = this,
218         cell = instance.matrix.getCell(cellId);
219 
220-      // get value
221-      value = cell ? cell.getValue() : "0"; // TODO: fix this, it's sloppy.
222+      // get value, defaulting to undefined
223+      value = cell ? cell.getValue() : undefined;
224       //update dependencies
225       instance.matrix.getCell(origin).updateDependencies([cellId]);
226       // check references error
227diff --git a/tests/CellTest.ts b/tests/CellTest.ts
228index c00baa3..690d928 100644
229--- a/tests/CellTest.ts
230+++ b/tests/CellTest.ts
231@@ -14,6 +14,7 @@ test("Cell.constructor", function(){
232   assertArrayEquals(cell.getDependencies(), []);
233   assertEquals(cell.getId(), "A1");
234   assertEquals(cell.getFormula(), null);
235+  assertEquals(cell.hasFormula(), false);
236 });
237 
238 test("Cell.updateDependencies", function(){