spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Using typed Matrix.
author
Ben Vogt <[email protected]>
date
2016-12-27 03:53:37
stats
2 file(s) changed, 154 insertions(+), 181 deletions(-)
files
ts/sheet.js
ts/sheet.ts
  1diff --git a/ts/sheet.js b/ts/sheet.js
  2index 1cd00cf..6fcca3d 100644
  3--- a/ts/sheet.js
  4+++ b/ts/sheet.js
  5@@ -61,11 +61,10 @@ var Cell = (function () {
  6 var mine = (function () {
  7     'use strict';
  8     var instance = this;
  9+    // Will be overwritten, but needs to be initialized, and have some functions for tsc compilation.
 10     var parser = {
 11-        setObj: function (obj) {
 12-        },
 13-        parse: function (thing) {
 14-        }
 15+        setObj: function (obj) { },
 16+        parse: function (formula) { }
 17     };
 18     var FormulaParser = function (handler) {
 19         var formulaLexer = function () { };
 20@@ -107,42 +106,37 @@ var mine = (function () {
 21             return error ? error.output : null;
 22         }
 23     };
 24-    var Matrix = function () {
 25-        // var item = {
 26-        //   id: '',
 27-        //   formula: '',
 28-        //   value: '',
 29-        //   error: '',
 30-        //   deps: [],
 31-        //   formulaEdit: false
 32-        // };
 33-        this.data = [];
 34-        this.getItem = function (key) {
 35-            return instance.matrix.data.filter(function (item) {
 36-                return item.id === key.toString();
 37+    var Matrix = (function () {
 38+        function Matrix() {
 39+            this.data = [];
 40+        }
 41+        Matrix.prototype.getItem = function (key) {
 42+            return this.data.filter(function (cell) {
 43+                return cell.id === key.toString();
 44             })[0];
 45         };
 46-        this.addItem = function (cell) {
 47-            var cellId = cell.id, coords = instance.utils.cellCoords(cellId);
 48+        Matrix.prototype.addItem = function (cell) {
 49+            var cellId = cell.id;
 50+            var key = new A1CellKey(cellId);
 51+            var coords = instance.utils.cellCoords(cellId);
 52             cell.row = coords.row;
 53             cell.col = coords.col;
 54-            var existingCell = instance.matrix.data.filter(function (cell) {
 55+            var existingCell = this.data.filter(function (cell) {
 56                 return cell.id === cellId;
 57             })[0];
 58             if (!existingCell) {
 59-                instance.matrix.data.push(cell);
 60+                this.data.push(cell);
 61             }
 62             else {
 63-                // instance.matrix.updateItem(cellExist, item);
 64-                instance.matrix.getItem(cellId).updateDependencies(cell.dependencies);
 65-                instance.matrix.getItem(cellId).setValue(cell.value);
 66-                instance.matrix.getItem(cellId).setError(cell.error);
 67+                this.getItem(key).updateDependencies(cell.dependencies);
 68+                this.getItem(key).setValue(cell.value);
 69+                this.getItem(key).setError(cell.error);
 70             }
 71-            return instance.matrix.getItem(new A1CellKey(cellId));
 72+            return this.getItem(new A1CellKey(cellId));
 73         };
 74-        this.getDependencies = function (id) {
 75+        Matrix.prototype.getDependencies = function (id) {
 76             var getDependencies = function (id) {
 77-                var filtered = instance.matrix.data.filter(function (cell) {
 78+                var filtered = this.data.filter(function (cell) {
 79                     if (cell.deps) {
 80                         return cell.deps.indexOf(id) > -1;
 81                     }
 82@@ -154,7 +148,7 @@ var mine = (function () {
 83                     }
 84                 });
 85                 return deps;
 86-            };
 87+            }.bind(this);
 88             var allDependencies = [];
 89             var getTotalDependencies = function (id) {
 90                 var deps = getDependencies(id);
 91@@ -162,43 +156,21 @@ var mine = (function () {
 92                     deps.forEach(function (refId) {
 93                         if (allDependencies.indexOf(refId) === -1) {
 94                             allDependencies.push(refId);
 95-                            var item = instance.matrix.getItem(new A1CellKey(refId));
 96+                            var item = this.getItem(new A1CellKey(refId));
 97                             if (item.deps.length) {
 98                                 getTotalDependencies(refId);
 99                             }
100                         }
101                     });
102                 }
103-            };
104+            }.bind(this);
105             getTotalDependencies(id);
106             return allDependencies;
107         };
108-        this.getCellDependencies = function (cell) {
109-            return instance.matrix.getDependencies(cell.id);
110+        Matrix.prototype.getCellDependencies = function (cell) {
111+            return this.getDependencies(cell.id);
112         };
113-        var recalculateCellDependencies = function (cell) {
114-            var allDependencies = instance.matrix.getCellDependencies(cell);
115-            allDependencies.forEach(function (refId) {
116-                var currentCell = instance.matrix.getItem(new A1CellKey(refId));
117-                if (currentCell && currentCell.formula) {
118-                    calculateCellFormula(currentCell);
119-                }
120-            });
121-        };
122-        var calculateCellFormula = function (cell) {
123-            // to avoid double translate formulas, update item data in parser
124-            var parsed = parse(cell.formula, cell.id);
125-            var key = new A1CellKey(cell.id);
126-            // instance.matrix.updateCellItem(key, {value: value, error: error});
127-            instance.matrix.getItem(key).setValue(parsed.result);
128-            instance.matrix.getItem(key).setError(parsed.error);
129-            return parsed;
130-        };
131-        var registerCellInMatrix = function (cell) {
132-            instance.matrix.addItem(cell);
133-            calculateCellFormula(cell);
134-        };
135-        this.scan = function () {
136+        Matrix.prototype.scan = function () {
137             var input = [
138                 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "SUM(A1:D1, H1)"],
139                 [-1, -10, 2, 4, 100, 1, 50, 20, 200, -100, "MAX(A2:J2)"],
140@@ -210,19 +182,38 @@ var mine = (function () {
141                 for (var x = 0; x < input[0].length; x++) {
142                     // set the cell here
143                     var id = utils.XYtoA1(x, y);
144-                    var cell = {
145-                        id: id,
146-                        formula: input[y][x].toString()
147-                    };
148-                    var trueCell = new Cell(input[y][x].toString(), id);
149-                    registerCellInMatrix(trueCell);
150-                    recalculateCellDependencies(trueCell);
151+                    var cell = new Cell(input[y][x].toString(), id);
152+                    registerCellInMatrix(cell);
153+                    recalculateCellDependencies(cell);
154                 }
155             }
156             this.data.forEach(function (item) {
157                 console.log(item.id, item.formula, item.value);
158             });
159         };
160+        return Matrix;
161+    }());
162+    var recalculateCellDependencies = function (cell) {
163+        var allDependencies = instance.matrix.getCellDependencies(cell);
164+        allDependencies.forEach(function (refId) {
165+            var currentCell = instance.matrix.getItem(new A1CellKey(refId));
166+            if (currentCell && currentCell.formula) {
167+                calculateCellFormula(currentCell);
168+            }
169+        });
170+    };
171+    var calculateCellFormula = function (cell) {
172+        // to avoid double translate formulas, update item data in parser
173+        var parsed = parse(cell.formula, cell.id);
174+        var key = new A1CellKey(cell.id);
175+        // instance.matrix.updateCellItem(key, {value: value, error: error});
176+        instance.matrix.getItem(key).setValue(parsed.result);
177+        instance.matrix.getItem(key).setError(parsed.error);
178+        return parsed;
179+    };
180+    var registerCellInMatrix = function (cell) {
181+        instance.matrix.addItem(cell);
182+        calculateCellFormula(cell);
183     };
184     var utils = {
185         isArray: function (value) {
186diff --git a/ts/sheet.ts b/ts/sheet.ts
187index 05b4740..668f1f7 100644
188--- a/ts/sheet.ts
189+++ b/ts/sheet.ts
190@@ -107,13 +107,10 @@ var mine = (function () {
191   'use strict';
192   var instance = this;
193 
194+  // Will be overwritten, but needs to be initialized, and have some functions for tsc compilation.
195   var parser = {
196-    setObj: function (obj: string) {
197-
198-    },
199-    parse: function (thing: any) {
200-
201-    }
202+    setObj: function (obj: string) {},
203+    parse: function (formula: string) {}
204   };
205 
206   var FormulaParser = function(handler) {
207@@ -164,51 +161,42 @@ var mine = (function () {
208     }
209   };
210 
211-  var Matrix = function () {
212 
213-    // var item = {
214-    //   id: '',
215-    //   formula: '',
216-    //   value: '',
217-    //   error: '',
218-    //   deps: [],
219-    //   formulaEdit: false
220-    // };
221-
222-    this.data = [];
223-
224-    this.getItem = function (key: A1CellKey) {
225-      return instance.matrix.data.filter(function (item) {
226-        return item.id === key.toString();
227+  class Matrix {
228+    public data: Array<Cell>;
229+    constructor() {
230+      this.data = [];
231+    }
232+    getItem(key: A1CellKey) {
233+      return this.data.filter(function (cell) {
234+        return cell.id === key.toString();
235       })[0];
236-    };
237-
238-    this.addItem = function (cell: Cell) {
239-      var cellId = cell.id,
240-        coords = instance.utils.cellCoords(cellId);
241+    }
242+    addItem(cell: Cell) {
243+      var cellId = cell.id;
244+      var key = new A1CellKey(cellId);
245+      var coords = instance.utils.cellCoords(cellId);
246 
247       cell.row = coords.row;
248       cell.col = coords.col;
249 
250-      var existingCell = instance.matrix.data.filter(function (cell) {
251+      var existingCell = this.data.filter(function (cell) {
252         return cell.id === cellId;
253       })[0];
254 
255       if (!existingCell) {
256-        instance.matrix.data.push(cell);
257+        this.data.push(cell);
258       } else {
259-        // instance.matrix.updateItem(cellExist, item);
260-        instance.matrix.getItem(cellId).updateDependencies(cell.dependencies);
261-        instance.matrix.getItem(cellId).setValue(cell.value);
262-        instance.matrix.getItem(cellId).setError(cell.error);
263+        this.getItem(key).updateDependencies(cell.dependencies);
264+        this.getItem(key).setValue(cell.value);
265+        this.getItem(key).setError(cell.error);
266       }
267 
268-      return instance.matrix.getItem(new A1CellKey(cellId));
269-    };
270-
271-    this.getDependencies = function (id: string) {
272+      return this.getItem(new A1CellKey(cellId));
273+    }
274+    getDependencies(id: string) {
275       var getDependencies = function (id: string) {
276-        var filtered = instance.matrix.data.filter(function (cell) {
277+        var filtered = this.data.filter(function (cell) {
278           if (cell.deps) {
279             return cell.deps.indexOf(id) > -1;
280           }
281@@ -222,10 +210,8 @@ var mine = (function () {
282         });
283 
284         return deps;
285-      };
286-
287+      }.bind(this);
288       var allDependencies = [];
289-
290       var getTotalDependencies = function (id: string) {
291         var deps = getDependencies(id);
292 
293@@ -234,53 +220,21 @@ var mine = (function () {
294             if (allDependencies.indexOf(refId) === -1) {
295               allDependencies.push(refId);
296 
297-              var item = instance.matrix.getItem(new A1CellKey(refId));
298+              var item = this.getItem(new A1CellKey(refId));
299               if (item.deps.length) {
300                 getTotalDependencies(refId);
301               }
302             }
303           });
304         }
305-      };
306-
307+      }.bind(this);
308       getTotalDependencies(id);
309-
310       return allDependencies;
311-    };
312-
313-    this.getCellDependencies = function (cell: Cell) {
314-      return instance.matrix.getDependencies(cell.id);
315-    };
316-
317-    var recalculateCellDependencies = function (cell: Cell) {
318-      var allDependencies = instance.matrix.getCellDependencies(cell);
319-
320-      allDependencies.forEach(function (refId) {
321-        var currentCell = instance.matrix.getItem(new A1CellKey(refId));
322-        if (currentCell && currentCell.formula) {
323-          calculateCellFormula(currentCell);
324-        }
325-      });
326-    };
327-
328-    var calculateCellFormula = function (cell: Cell) {
329-      // to avoid double translate formulas, update item data in parser
330-      var parsed = parse(cell.formula, cell.id);
331-      var key =  new A1CellKey(cell.id);
332-
333-      // instance.matrix.updateCellItem(key, {value: value, error: error});
334-      instance.matrix.getItem(key).setValue(parsed.result);
335-      instance.matrix.getItem(key).setError(parsed.error);
336-
337-      return parsed;
338-    };
339-
340-    var registerCellInMatrix = function (cell: Cell) {
341-      instance.matrix.addItem(cell);
342-      calculateCellFormula(cell);
343-    };
344-
345-    this.scan = function () {
346+    }
347+    getCellDependencies(cell: Cell) {
348+      return this.getDependencies(cell.id);
349+    }
350+    scan() {
351       var input = [
352         [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "SUM(A1:D1, H1)"],
353         [-1, -10, 2, 4, 100, 1, 50, 20, 200, -100, "MAX(A2:J2)"],
354@@ -292,19 +246,44 @@ var mine = (function () {
355         for (var x = 0; x < input[0].length; x++) {
356           // set the cell here
357           var id = utils.XYtoA1(x, y);
358-          var cell = {
359-            id: id,
360-            formula: input[y][x].toString()
361-          };
362-          var trueCell = new Cell(input[y][x].toString(), id);
363-          registerCellInMatrix(trueCell);
364-          recalculateCellDependencies(trueCell);
365+          var cell = new Cell(input[y][x].toString(), id);
366+          registerCellInMatrix(cell);
367+          recalculateCellDependencies(cell);
368         }
369       }
370       this.data.forEach(function (item) {
371         console.log(item.id, item.formula, item.value);
372-      })
373-    };
374+      });
375+    }
376+  }
377+
378+
379+  var recalculateCellDependencies = function (cell: Cell) {
380+    var allDependencies = instance.matrix.getCellDependencies(cell);
381+
382+    allDependencies.forEach(function (refId) {
383+      var currentCell = instance.matrix.getItem(new A1CellKey(refId));
384+      if (currentCell && currentCell.formula) {
385+        calculateCellFormula(currentCell);
386+      }
387+    });
388+  };
389+
390+  var calculateCellFormula = function (cell: Cell) {
391+    // to avoid double translate formulas, update item data in parser
392+    var parsed = parse(cell.formula, cell.id);
393+    var key =  new A1CellKey(cell.id);
394+
395+    // instance.matrix.updateCellItem(key, {value: value, error: error});
396+    instance.matrix.getItem(key).setValue(parsed.result);
397+    instance.matrix.getItem(key).setError(parsed.error);
398+
399+    return parsed;
400+  };
401+
402+  var registerCellInMatrix = function (cell: Cell) {
403+    instance.matrix.addItem(cell);
404+    calculateCellFormula(cell);
405   };
406 
407   var utils = {