spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Making progress on converting everything to a type
author
Ben Vogt <[email protected]>
date
2016-12-26 18:41:35
stats
2 file(s) changed, 54 insertions(+), 65 deletions(-)
files
ts/sheet.js
ts/sheet.ts
  1diff --git a/ts/sheet.js b/ts/sheet.js
  2index 3fc9ee3..961152f 100644
  3--- a/ts/sheet.js
  4+++ b/ts/sheet.js
  5@@ -99,33 +99,28 @@ var mine = (function () {
  6                 return item.id === key.toString();
  7             })[0];
  8         };
  9-        this.removeItem = function (id) {
 10-            instance.matrix.data = instance.matrix.data.filter(function (item) {
 11-                return item.id !== id;
 12-            });
 13-        };
 14-        this.updateItem = function (item, props) {
 15-            if (instance.utils.isString(item)) {
 16-                item = instance.matrix.getItem(new A1CellKey(item));
 17+        this.updateItem = function (cell, props) {
 18+            if (instance.utils.isString(cell)) {
 19+                cell = instance.matrix.getItem(new A1CellKey(cell));
 20             }
 21-            if (item && props) {
 22+            if (cell && props) {
 23                 for (var p in props) {
 24-                    if (item[p] && instance.utils.isArray(item[p])) {
 25+                    if (cell[p] && instance.utils.isArray(cell[p])) {
 26                         if (instance.utils.isArray(props[p])) {
 27                             props[p].forEach(function (i) {
 28-                                if (item[p].indexOf(i) === -1) {
 29-                                    item[p].push(i);
 30+                                if (cell[p].indexOf(i) === -1) {
 31+                                    cell[p].push(i);
 32                                 }
 33                             });
 34                         }
 35                         else {
 36-                            if (item[p].indexOf(props[p]) === -1) {
 37-                                item[p].push(props[p]);
 38+                            if (cell[p].indexOf(props[p]) === -1) {
 39+                                cell[p].push(props[p]);
 40                             }
 41                         }
 42                     }
 43                     else {
 44-                        item[p] = props[p];
 45+                        cell[p] = props[p];
 46                     }
 47                 }
 48             }
 49@@ -145,8 +140,8 @@ var mine = (function () {
 50             }
 51             return instance.matrix.getItem(new A1CellKey(cellId));
 52         };
 53-        this.updateCellItem = function (id, props) {
 54-            var item = instance.matrix.getItem(new A1CellKey(id));
 55+        this.updateCellItem = function (key, props) {
 56+            var item = instance.matrix.getItem(key);
 57             instance.matrix.updateItem(item, props);
 58         };
 59         this.getDependencies = function (id) {
 60@@ -197,7 +192,7 @@ var mine = (function () {
 61         var calculateCellFormula = function (formula, id) {
 62             // to avoid double translate formulas, update item data in parser
 63             var parsed = parse(formula, id), value = parsed.result, error = parsed.error;
 64-            instance.matrix.updateCellItem(id, { value: value, error: error });
 65+            instance.matrix.updateCellItem(new A1CellKey(id), { value: value, error: error });
 66             return parsed;
 67         };
 68         var registerCellInMatrix = function (cell) {
 69@@ -478,11 +473,11 @@ var mine = (function () {
 70             throw Error('NAME');
 71         },
 72         cellValue: function (cell) {
 73-            var value, element = this, item = instance.matrix.getItem(new A1CellKey(cell));
 74+            var value, origin = this, item = instance.matrix.getItem(new A1CellKey(cell));
 75             // get value
 76             value = item ? item.value : "0"; // TODO: fix this, it's sloppy.
 77             //update dependencies
 78-            instance.matrix.updateCellItem(element, { deps: [cell] });
 79+            instance.matrix.updateCellItem(new A1CellKey(origin), { deps: [cell] });
 80             // check references error
 81             if (item && item.deps) {
 82                 if (item.deps.indexOf(cell) !== -1) {
 83@@ -502,11 +497,11 @@ var mine = (function () {
 84             throw Error('NOT_AVAILABLE');
 85         },
 86         cellRangeValue: function (start, end) {
 87-            var coordsStart = instance.utils.cellCoords(start), coordsEnd = instance.utils.cellCoords(end), element = this;
 88+            var coordsStart = instance.utils.cellCoords(start), coordsEnd = instance.utils.cellCoords(end), origin = this;
 89             // iterate cells to get values and indexes
 90             var cells = instance.utils.iterateCells.call(this, coordsStart, coordsEnd), result = [];
 91             //update dependencies
 92-            instance.matrix.updateCellItem(element, { deps: cells.index });
 93+            instance.matrix.updateCellItem(new A1CellKey(origin), { deps: cells.index });
 94             result.push(cells.value);
 95             return result;
 96         },
 97@@ -520,18 +515,12 @@ var mine = (function () {
 98             return instance.helper.cellRangeValue.call(this, start, end);
 99         }
100     };
101-    var parse = function (formula, element) {
102+    var parse = function (formula, key) {
103         var result = null, error = null;
104         try {
105-            parser.setObj(element);
106+            parser.setObj(key);
107             result = parser.parse(formula);
108-            var id;
109-            if (element instanceof HTMLElement) {
110-                id = element.getAttribute('id');
111-            }
112-            else if (element && element.id) {
113-                id = element.id;
114-            }
115+            var id = key;
116             var deps = instance.matrix.getDependencies(id);
117             if (deps.indexOf(id) !== -1) {
118                 result = null;
119diff --git a/ts/sheet.ts b/ts/sheet.ts
120index f590a75..9bd51cf 100644
121--- a/ts/sheet.ts
122+++ b/ts/sheet.ts
123@@ -151,28 +151,28 @@ var mine = (function () {
124       })[0];
125     };
126 
127-    this.updateItem = function (item, props) {
128-      if (instance.utils.isString(item)) {
129-        item = instance.matrix.getItem(new A1CellKey(item));
130+    this.updateItem = function (cell, props) {
131+      if (instance.utils.isString(cell)) {
132+        cell = instance.matrix.getItem(new A1CellKey(cell));
133       }
134 
135-      if (item && props) {
136+      if (cell && props) {
137         for (var p in props) {
138-          if (item[p] && instance.utils.isArray(item[p])) {
139+          if (cell[p] && instance.utils.isArray(cell[p])) {
140             if (instance.utils.isArray(props[p])) {
141               props[p].forEach(function (i) {
142-                if (item[p].indexOf(i) === -1) {
143-                  item[p].push(i);
144+                if (cell[p].indexOf(i) === -1) {
145+                  cell[p].push(i);
146                 }
147               });
148             } else {
149 
150-              if (item[p].indexOf(props[p]) === -1) {
151-                item[p].push(props[p]);
152+              if (cell[p].indexOf(props[p]) === -1) {
153+                cell[p].push(props[p]);
154               }
155             }
156           } else {
157-            item[p] = props[p];
158+            cell[p] = props[p];
159           }
160         }
161       }
162@@ -199,14 +199,14 @@ var mine = (function () {
163     };
164 
165 
166-    this.updateCellItem = function (id, props) {
167-      var item = instance.matrix.getItem(new A1CellKey(id));
168+    this.updateCellItem = function (key: A1CellKey, props) {
169+      var item = instance.matrix.getItem(key);
170 
171       instance.matrix.updateItem(item, props);
172     };
173 
174-    this.getDependencies = function (id) {
175-      var getDependencies = function (id) {
176+    this.getDependencies = function (id: string) {
177+      var getDependencies = function (id: string) {
178         var filtered = instance.matrix.data.filter(function (cell) {
179           if (cell.deps) {
180             return cell.deps.indexOf(id) > -1;
181@@ -225,7 +225,7 @@ var mine = (function () {
182 
183       var allDependencies = [];
184 
185-      var getTotalDependencies = function (id) {
186+      var getTotalDependencies = function (id: string) {
187         var deps = getDependencies(id);
188 
189         if (deps.length) {
190@@ -262,13 +262,13 @@ var mine = (function () {
191       });
192     };
193 
194-    var calculateCellFormula = function (formula, id) {
195+    var calculateCellFormula = function (formula: string, id: string) {
196       // to avoid double translate formulas, update item data in parser
197       var parsed = parse(formula, id),
198         value = parsed.result,
199         error = parsed.error;
200 
201-      instance.matrix.updateCellItem(id, {value: value, error: error});
202+      instance.matrix.updateCellItem(new A1CellKey(id), {value: value, error: error});
203 
204       return parsed;
205     };
206@@ -610,12 +610,13 @@ var mine = (function () {
207 
208     cellValue: function (cell) {
209       var value,
210-        element = this,
211+        origin = this,
212         item = instance.matrix.getItem(new A1CellKey(cell));
213+
214       // get value
215       value = item ? item.value : "0"; // TODO: fix this, it's sloppy.
216       //update dependencies
217-      instance.matrix.updateCellItem(element, {deps: [cell]});
218+      instance.matrix.updateCellItem(new A1CellKey(origin), {deps: [cell]});
219       // check references error
220       if (item && item.deps) {
221         if (item.deps.indexOf(cell) !== -1) {
222@@ -639,16 +640,16 @@ var mine = (function () {
223       throw Error('NOT_AVAILABLE');
224     },
225 
226-    cellRangeValue: function (start, end) {
227+    cellRangeValue: function (start: string, end: string) {
228       var coordsStart = instance.utils.cellCoords(start),
229         coordsEnd = instance.utils.cellCoords(end),
230-        element = this;
231+        origin = this;
232 
233       // iterate cells to get values and indexes
234       var cells = instance.utils.iterateCells.call(this, coordsStart, coordsEnd),
235         result = [];
236       //update dependencies
237-      instance.matrix.updateCellItem(element, {deps: cells.index});
238+      instance.matrix.updateCellItem(new A1CellKey(origin), {deps: cells.index});
239 
240       result.push(cells.value);
241       return result;
242@@ -667,22 +668,17 @@ var mine = (function () {
243     }
244   };
245 
246-  var parse = function (formula, element) {
247+  var parse = function (formula, key) {
248     var result = null,
249       error = null;
250 
251     try {
252 
253-      parser.setObj(element);
254+      parser.setObj(key);
255       result = parser.parse(formula);
256 
257-      var id;
258+      var id = key;
259 
260-      if (element instanceof HTMLElement) {
261-        id = element.getAttribute('id');
262-      } else if (element && element.id) {
263-        id = element.id;
264-      }
265 
266       var deps = instance.matrix.getDependencies(id);
267