spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Making Cell instance variables private
author
Ben Vogt <[email protected]>
date
2017-01-01 20:36:34
stats
4 file(s) changed, 102 insertions(+), 73 deletions(-)
files
src/Cell.ts
src/Sheet.ts
tests/CellTest.ts
tests/SheetTest.ts
  1diff --git a/src/Cell.ts b/src/Cell.ts
  2index 0dd4fd0..e3986f4 100644
  3--- a/src/Cell.ts
  4+++ b/src/Cell.ts
  5@@ -1,11 +1,11 @@
  6 class Cell {
  7-  public formula: string;
  8-  public value: string;
  9-  public dependencies: Array<string>;
 10-  public error: any;
 11-  public id: string;
 12-  public row: number;
 13-  public col: number;
 14+  private formula: string;
 15+  private value: string;
 16+  private dependencies: Array<string>;
 17+  private error: any;
 18+  private id: string;
 19+  private row: number;
 20+  private col: number;
 21   constructor(id: string) {
 22     var key = parseKey(id);
 23 
 24@@ -24,15 +24,36 @@ class Cell {
 25       }
 26     }
 27   }
 28+  getDependencies() : Array<string> {
 29+    return this.dependencies;
 30+  }
 31+  getColumn() : number {
 32+    return this.col;
 33+  }
 34+  getRow() : number {
 35+    return this.row;
 36+  }
 37+  getId() : string {
 38+    return this.id;
 39+  }
 40   setFormula(formula: string) {
 41     this.formula = formula;
 42   }
 43+  getFormula() : string {
 44+    return this.formula;
 45+  }
 46   setValue(value: string) {
 47     this.value = value;
 48   }
 49+  getValue() : string {
 50+    return this.value;
 51+  }
 52   setError(error: any) {
 53     this.error = error;
 54   }
 55+  getError() : any {
 56+    return this.error;
 57+  }
 58   getRenderedValue() : string {
 59     if (this.error !== null) {
 60       return this.value;
 61@@ -47,15 +68,12 @@ class Cell {
 62 function toNum(chr) {
 63   chr = chr.replace(/\$/g, '');
 64   var base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', i, j, result = 0;
 65-
 66   for (i = 0, j = chr.length - 1; i < chr.length; i += 1, j -= 1) {
 67     result += Math.pow(base.length, j) * (base.indexOf(chr[i]) + 1);
 68   }
 69-
 70   if (result) {
 71     --result;
 72   }
 73-
 74   return result;
 75 }
 76 
 77diff --git a/src/Sheet.ts b/src/Sheet.ts
 78index 72118bc..8827971 100644
 79--- a/src/Sheet.ts
 80+++ b/src/Sheet.ts
 81@@ -51,14 +51,14 @@ var Sheet = (function () {
 82       return this.data[key];
 83     }
 84     addCell(cell: Cell) {
 85-      var cellId = cell.id;
 86+      var cellId = cell.getId();
 87 
 88       if (!(cellId in this.data)) {
 89         this.data[cellId] = cell;
 90       } else {
 91-        this.getCell(cellId).updateDependencies(cell.dependencies);
 92-        this.getCell(cellId).setValue(cell.value);
 93-        this.getCell(cellId).setError(cell.error);
 94+        this.getCell(cellId).updateDependencies(cell.getDependencies());
 95+        this.getCell(cellId).setValue(cell.getValue());
 96+        this.getCell(cellId).setError(cell.getError());
 97       }
 98 
 99       return this.getCell(cellId);
100@@ -105,7 +105,7 @@ var Sheet = (function () {
101       return allDependencies;
102     }
103     getCellDependencies(cell: Cell) {
104-      return this.getDependencies(cell.id);
105+      return this.getDependencies(cell.getId());
106     }
107     setCell(cellKeyString: string, formula: string) {
108       var cell = new Cell(cellKeyString);
109@@ -133,17 +133,17 @@ var Sheet = (function () {
110 
111   var calculateCellFormula = function (cell: Cell) {
112     // to avoid double translate formulas, update cell data in parser
113-    var parsed = parse(cell.formula, cell.id);
114+    var parsed = parse(cell.getFormula(), cell.getId());
115 
116-    instance.matrix.getCell(cell.id).setValue(parsed.result);
117-    instance.matrix.getCell(cell.id).setError(parsed.error);
118+    instance.matrix.getCell(cell.getId()).setValue(parsed.result);
119+    instance.matrix.getCell(cell.getId()).setError(parsed.error);
120 
121     return parsed;
122   };
123 
124   var registerCellInMatrix = function (cell: Cell) {
125     instance.matrix.addCell(cell);
126-    if (cell.formula !== null) {
127+    if (cell.getFormula() !== null) {
128       calculateCellFormula(cell);
129     }
130   };
131diff --git a/tests/CellTest.ts b/tests/CellTest.ts
132index dbead72..84b20f7 100644
133--- a/tests/CellTest.ts
134+++ b/tests/CellTest.ts
135@@ -3,20 +3,20 @@ import { assertEquals, assertArrayEquals } from "./utils/Asserts"
136 
137 //Test constructor
138 var cell = new Cell("A1");
139-assertEquals(cell.col, 0);
140-assertEquals(cell.row, 0);
141-assertArrayEquals(cell.dependencies, []);
142-assertEquals(cell.id, "A1");
143-assertEquals(cell.formula, null);
144+assertEquals(cell.getColumn(), 0);
145+assertEquals(cell.getRow(), 0);
146+assertArrayEquals(cell.getDependencies(), []);
147+assertEquals(cell.getId(), "A1");
148+assertEquals(cell.getFormula(), null);
149 
150 //Test updateDependencies
151 var one = new Cell("A1");
152 one.updateDependencies(["B2", "C1", "D12", "D13"]);
153-assertArrayEquals(one.dependencies, ["B2", "C1", "D12", "D13"]);
154+assertArrayEquals(one.getDependencies(), ["B2", "C1", "D12", "D13"]);
155 one.updateDependencies(["M4"]);
156-assertArrayEquals(one.dependencies, ["B2", "C1", "D12", "D13", "M4"]);
157+assertArrayEquals(one.getDependencies(), ["B2", "C1", "D12", "D13", "M4"]);
158 
159 //Test setValue
160 var v = new Cell("A1");
161 v.setValue("100");
162-assertEquals("100", v.value);
163+assertEquals("100", v.getValue());
164diff --git a/tests/SheetTest.ts b/tests/SheetTest.ts
165index 9bfa791..8f87f39 100644
166--- a/tests/SheetTest.ts
167+++ b/tests/SheetTest.ts
168@@ -6,13 +6,13 @@ import { Errors } from "../src/Errors"
169 var sheet = new Sheet();
170 sheet.setCell("A2", "22");
171 var cell = sheet.getCell("A2");
172-assertEquals(null, cell.formula);
173-assertEquals(22, cell.value);
174-assertEquals("A2", cell.id);
175-assertEquals(1, cell.row);
176-assertEquals(0, cell.col);
177-assertEquals(null, cell.error);
178-assertArrayEquals([], cell.dependencies);
179+assertEquals(null, cell.getFormula());
180+assertEquals(22, cell.getValue());
181+assertEquals("A2", cell.getId());
182+assertEquals(1, cell.getRow());
183+assertEquals(0, cell.getColumn());
184+assertEquals(null, cell.getError());
185+assertArrayEquals([], cell.getDependencies());
186 
187 // Test getCell, null value
188 var nullCell = sheet.getCell("N1");
189@@ -27,10 +27,10 @@ sheet.setCell("A3", "3.4");
190 sheet.setCell("A4", "45");
191 sheet.setCell("A5", "=SUM(A1:A4)");
192 var A5 = sheet.getCell("A5");
193-assertEquals("69.4", A5.value);
194-assertEquals(SUM_FORM.substr(1), A5.formula);
195-assertEquals(null, cell.error);
196-assertArrayEquals(['A1', 'A2', 'A3', 'A4'], A5.dependencies);
197+assertEquals("69.4", A5.getValue());
198+assertEquals(SUM_FORM.substr(1), A5.getFormula());
199+assertEquals(null, cell.getError());
200+assertArrayEquals(['A1', 'A2', 'A3', 'A4'], A5.getDependencies());
201 
202 
203 // Test load
204@@ -48,35 +48,35 @@ sheet.load([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, SUM_FORMULA],
205   [0, 10, 1, 10, 2, 10, 3, 10, 4, 10, SUM_IF_FORMULA],
206   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, SUM_REF_FORMULA]]);
207 var K1 = sheet.getCell("K1");
208-assertEquals("18", K1.value);
209-assertEquals(SUM_FORMULA.substr(1), K1.formula);
210-assertEquals(null, K1.error);
211-assertArrayEquals(['A1', 'B1', 'C1', 'D1', 'H1'], K1.dependencies);
212+assertEquals("18", K1.getValue());
213+assertEquals(SUM_FORMULA.substr(1), K1.getFormula());
214+assertEquals(null, K1.getError());
215+assertArrayEquals(['A1', 'B1', 'C1', 'D1', 'H1'], K1.getDependencies());
216 var K2 = sheet.getCell("K2");
217-assertEquals("200", K2.value);
218-assertEquals(MAX_FORMULA.substr(1), K2.formula);
219-assertEquals(null, K2.error);
220-assertArrayEquals(['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2', 'J2'], K2.dependencies);
221+assertEquals("200", K2.getValue());
222+assertEquals(MAX_FORMULA.substr(1), K2.getFormula());
223+assertEquals(null, K2.getError());
224+assertArrayEquals(['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2', 'J2'], K2.getDependencies());
225 var K3 = sheet.getCell("K3");
226-assertEquals("-53", K3.value);
227-assertEquals(MIN_FORMULA.substr(1), K3.formula);
228-assertEquals(null, K3.error);
229-assertArrayEquals(['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3', 'J3'], K3.dependencies);
230+assertEquals("-53", K3.getValue());
231+assertEquals(MIN_FORMULA.substr(1), K3.getFormula());
232+assertEquals(null, K3.getError());
233+assertArrayEquals(['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3', 'J3'], K3.getDependencies());
234 var K4 = sheet.getCell("K4");
235-assertEquals("30.4", K4.value);
236-assertEquals(AVERAGE_FORMULA.substr(1), K4.formula);
237-assertEquals(null, K4.error);
238-assertArrayEquals(['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4', 'J4'], K4.dependencies);
239+assertEquals("30.4", K4.getValue());
240+assertEquals(AVERAGE_FORMULA.substr(1), K4.getFormula());
241+assertEquals(null, K4.getError());
242+assertArrayEquals(['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4', 'J4'], K4.getDependencies());
243 var K5 = sheet.getCell("K5");
244-assertEquals("50", K5.value);
245-assertEquals(SUM_IF_FORMULA.substr(1), K5.formula);
246-assertEquals(null, K5.error);
247-assertArrayEquals(['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5', 'J5'], K5.dependencies);
248+assertEquals("50", K5.getValue());
249+assertEquals(SUM_IF_FORMULA.substr(1), K5.getFormula());
250+assertEquals(null, K5.getError());
251+assertArrayEquals(['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5', 'J5'], K5.getDependencies());
252 var K6 = sheet.getCell("K6");
253-assertEquals("195.4", K6.value);
254-assertEquals(SUM_REF_FORMULA.substr(1), K6.formula);
255-assertEquals(null, K6.error);
256-assertArrayEquals(['K1', 'K2', 'K3', 'K4'], K6.dependencies);
257+assertEquals("195.4", K6.getValue());
258+assertEquals(SUM_REF_FORMULA.substr(1), K6.getFormula());
259+assertEquals(null, K6.getError());
260+assertArrayEquals(['K1', 'K2', 'K3', 'K4'], K6.getDependencies());
261 
262 //Test REF error
263 var sheet  = new Sheet();
264@@ -86,31 +86,31 @@ sheet.setCell("A3", "=SUM(A1, A2)");
265 sheet.setCell("B1", "=SUM(A3, B2)");
266 sheet.setCell("B2", "=SUM(A1, B1)");
267 var B1 = sheet.getCell("B1");
268-assertEquals(null, B1.value);
269-assertEquals(Errors.get("REF"), B1.error);
270-assertArrayEquals(['A3', 'B2'], B1.dependencies);
271+assertEquals(null, B1.getValue());
272+assertEquals(Errors.get("REF"), B1.getError());
273+assertArrayEquals(['A3', 'B2'], B1.getDependencies());
274 var B2 = sheet.getCell("B2");
275-assertEquals(null, B2.value);
276-assertEquals(Errors.get("REF"), B2.error);
277-assertArrayEquals(['A1', 'B1'], B2.dependencies);
278+assertEquals(null, B2.getValue());
279+assertEquals(Errors.get("REF"), B2.getError());
280+assertArrayEquals(['A1', 'B1'], B2.getDependencies());
281 
282 // Test NAME error
283 var sheet  = new Sheet();
284 sheet.setCell("A1", "1");
285 sheet.setCell("A2", "=SUM(A1, NN)");
286 var A2 = sheet.getCell("A2");
287-assertEquals(null, A2.value);
288-assertEquals(Errors.get("NAME"), A2.error);
289-assertArrayEquals(['A1'], A2.dependencies);
290+assertEquals(null, A2.getValue());
291+assertEquals(Errors.get("NAME"), A2.getError());
292+assertArrayEquals(['A1'], A2.getDependencies());
293 
294 // Test unsupported formula
295 var sheet  = new Sheet();
296 sheet.setCell("A1", "1");
297 sheet.setCell("A2", "=BEN(A1)");
298 var A2 = sheet.getCell("A2");
299-assertEquals(null, A2.value);
300-assertEquals(Errors.get("NAME"), A2.error);
301-assertArrayEquals(['A1'], A2.dependencies);
302+assertEquals(null, A2.getValue());
303+assertEquals(Errors.get("NAME"), A2.getError());
304+assertArrayEquals(['A1'], A2.getDependencies());
305 
306 // Test nested formulas
307 var sheet  = new Sheet();
308@@ -119,7 +119,7 @@ sheet.setCell("A2", "10");
309 sheet.setCell("A3", "44.4");
310 sheet.setCell("A4", "=SUM(A1:A3, MAX(A1, A3))");
311 var A4 = sheet.getCell("A4");
312-assertEquals("99.8", A4.value);
313-assertEquals("SUM(A1:A3, MAX(A1, A3))", A4.formula);
314-assertEquals(null, A4.error);
315-assertArrayEquals(['A1', 'A2', 'A3'], A4.dependencies);
316\ No newline at end of file
317+assertEquals("99.8", A4.getValue());
318+assertEquals("SUM(A1:A3, MAX(A1, A3))", A4.getFormula());
319+assertEquals(null, A4.getError());
320+assertArrayEquals(['A1', 'A2', 'A3'], A4.getDependencies());
321\ No newline at end of file