commit
message
Refactoring how we load data into a Sheet
author
Ben Vogt <[email protected]>
date
2016-12-30 22:30:24
stats
2 file(s) changed,
32 insertions(+),
25 deletions(-)
files
src/Main.ts
src/Sheet.ts
1diff --git a/src/Main.ts b/src/Main.ts
2index 57c581e..f364852 100644
3--- a/src/Main.ts
4+++ b/src/Main.ts
5@@ -1,4 +1,5 @@
6 import { Sheet } from "./Sheet"
7
8 var sheet = new Sheet();
9-sheet.init();
10+sheet.load();
11+sheet.toString();
12\ No newline at end of file
13diff --git a/src/Sheet.ts b/src/Sheet.ts
14index 8e5da10..dd218e9 100644
15--- a/src/Sheet.ts
16+++ b/src/Sheet.ts
17@@ -6,7 +6,6 @@ import { Errors } from "./Errors"
18 import * as Formula from "formulajs"
19
20 var Sheet = (function () {
21- 'use strict';
22 var instance = this;
23
24 // Will be overwritten, but needs to be initialized, and have some functions for tsc compilation.
25@@ -113,26 +112,10 @@ var Sheet = (function () {
26 getCellDependencies(cell: Cell) {
27 return this.getDependencies(cell.id);
28 }
29- scan() {
30- var input = [
31- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "SUM(A1:D1, H1)"],
32- [-1, -10, 2, 4, 100, 1, 50, 20, 200, -100, "MAX(A2:J2)"],
33- [-1, -40, -53, 1, 10, 30, 10, 301, -1, -20, "MIN(A3:J3)"],
34- [20, 50, 100, 20, 1, 5, 15, 25, 45, 23, "AVERAGE(A4:J4)"],
35- [0, 10, 1, 10, 2, 10, 3, 10, 4, 10, "SUMIF(A5:J5,'>5')"]
36- ];
37- for (var y = 0; y < input.length; y++) {
38- for (var x = 0; x < input[0].length; x++) {
39- // set the cell here
40- var id = utils.XYtoA1(x, y);
41- var cell = new Cell(input[y][x].toString(), id);
42- registerCellInMatrix(cell);
43- recalculateCellDependencies(cell);
44- }
45- }
46- for (var key in this.data) {
47- console.log(this.data[key].id, this.data[key].formula, this.data[key].value);
48- }
49+ setCell(cellKeyString: string, formula: string) {
50+ var cell = new Cell(formula, cellKeyString);
51+ registerCellInMatrix(cell);
52+ recalculateCellDependencies(cell);
53 }
54 }
55
56@@ -552,15 +535,39 @@ var Sheet = (function () {
57 }
58 };
59
60- this.init = function () {
61- instance = this;
62- parser = FormulaParser(instance);
63- instance.matrix = new Matrix();
64- instance.matrix.scan();
65+ var setCell = function (cellKeyString: string, value: string) {
66+ instance.matrix.setCell(cellKeyString, value);
67 };
68+
69+ this.load = function () {
70+ var input = [
71+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "SUM(A1:D1, H1)"],
72+ [-1, -10, 2, 4, 100, 1, 50, 20, 200, -100, "MAX(A2:J2)"],
73+ [-1, -40, -53, 1, 10, 30, 10, 301, -1, -20, "MIN(A3:J3)"],
74+ [20, 50, 100, 20, 1, 5, 15, 25, 45, 23, "AVERAGE(A4:J4)"],
75+ [0, 10, 1, 10, 2, 10, 3, 10, 4, 10, "SUMIF(A5:J5,'>5')"]
76+ ];
77+ for (var y = 0; y < input.length; y++) {
78+ for (var x = 0; x < input[0].length; x++) {
79+ // set the cell here
80+ var id = utils.XYtoA1(x, y);
81+ this.setCell(id, input[y][x].toString());
82+ }
83+ }
84+ };
85+
86+ this.toString = function () {
87+ for (var key in this.matrix.data) {
88+ console.log(this.matrix.data[key].id, this.matrix.data[key].formula, this.matrix.data[key].value);
89+ }
90+ };
91+
92+ parser = FormulaParser(instance);
93+ instance.matrix = new Matrix();
94 this.utils = utils;
95 this.helper = helper;
96 this.parse = parse;
97+ this.setCell = setCell;
98 });
99
100 export {