spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Adding tests for individual formulas
author
Ben Vogt <[email protected]>
date
2017-01-06 01:33:27
stats
3 file(s) changed, 67 insertions(+), 1 deletions(-)
files
src/Sheet.ts
src/SupportedFormulas.ts
tests/SheetFormulaTest.ts
  1diff --git a/src/Sheet.ts b/src/Sheet.ts
  2index ed87e42..2a5b8a7 100644
  3--- a/src/Sheet.ts
  4+++ b/src/Sheet.ts
  5@@ -1,6 +1,6 @@
  6 /// <reference path="parser.d.ts"/>
  7 import { Parser } from "./Parser";
  8-import { SUPPORTED_FORMULAS } from "./SupportedFormulas"
  9+import {SUPPORTED_FORMULAS, OverrideFormulas} from "./SupportedFormulas"
 10 import { Cell } from "./Cell"
 11 import { Errors } from "./Errors"
 12 import * as Formula from "formulajs"
 13@@ -438,7 +438,9 @@ var Sheet = (function () {
 14     callFunction: function (fn, args) {
 15       fn = fn.toUpperCase();
 16       args = args || [];
 17-
 18+      if (fn in OverrideFormulas) {
 19+        return OverrideFormulas[fn].apply(this, args);
 20+      }
 21       if (SUPPORTED_FORMULAS.indexOf(fn) > -1) {
 22         if (Formula[fn]) {
 23           return Formula[fn].apply(this, args);
 24diff --git a/src/SupportedFormulas.ts b/src/SupportedFormulas.ts
 25index 8ff2865..350368f 100644
 26--- a/src/SupportedFormulas.ts
 27+++ b/src/SupportedFormulas.ts
 28@@ -16,7 +16,13 @@ const SUPPORTED_FORMULAS = [
 29   'TAN', 'TANH', 'TRUE', 'TRUNC',
 30   'XOR'
 31 ];
 32+const OverrideFormulas = {
 33+  ATAN2: function (x, y) {
 34+    return Math.atan2(y, x);
 35+  }
 36+};
 37 
 38 export {
 39-  SUPPORTED_FORMULAS
 40+  SUPPORTED_FORMULAS,
 41+  OverrideFormulas
 42 }
 43\ No newline at end of file
 44diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
 45index 75c0dce..43f8115 100644
 46--- a/tests/SheetFormulaTest.ts
 47+++ b/tests/SheetFormulaTest.ts
 48@@ -1,6 +1,14 @@
 49 import { Sheet } from "../src/Sheet"
 50 import {assertEquals, assertArrayEquals} from "./utils/Asserts"
 51 
 52+function testFormula(formula: string, expectation: any) {
 53+  var sheet  = new Sheet();
 54+  sheet.setCell("A1", formula);
 55+  var cell = sheet.getCell("A1");
 56+  assertEquals(null, cell.getError());
 57+  assertEquals(expectation, cell.getValue());
 58+}
 59+
 60 //Test CONCATENATE formula
 61 var sheet  = new Sheet();
 62 sheet.setCell("A1", "Hello, ");
 63@@ -16,3 +24,50 @@ var cell = sheet.getCell("B2");
 64 assertEquals("Hello, 1000", cell.getValue());
 65 assertEquals(null, cell.getError());
 66 assertArrayEquals(['A1', 'B1'], cell.getDependencies());
 67+
 68+// Test ABS formula
 69+testFormula("=ABS(-10)", 10);
 70+testFormula("=ABS(0)", 0);
 71+
 72+// Test ACCRINT
 73+// TODO: this
 74+
 75+// Test ACOS
 76+testFormula("=ACOS(0)", 1.5707963267948966);
 77+
 78+// Test ACOSH
 79+testFormula("=ACOSH(22)", 3.783672704329451);
 80+
 81+// Test ACOTH
 82+testFormula("=ACOTH(22)", 0.04548588910286339);
 83+
 84+// Test AND
 85+testFormula("=AND(10, 10)", true);
 86+testFormula("=AND(10, 0)", false);
 87+
 88+// Test ARABIC
 89+testFormula('=ARABIC("XIV")', 14);
 90+
 91+// Test ASIN
 92+testFormula("=ASIN(0.1)", 0.1001674211615598);
 93+
 94+// Test ASINH
 95+testFormula("=ASINH(0.1)", 0.09983407889920758);
 96+
 97+// Test ATAN
 98+testFormula("=ATAN(0.1)", 0.09966865249116204);
 99+
100+// Test ATAN2
101+testFormula("=ATAN2(4, 3)", 0.6435011087932844);
102+
103+// Test ATANH
104+testFormula("=ATANH(0.44)", 0.47223080442042564);
105+
106+// Test AVEDEV
107+testFormula("=AVEDEV(1, 2, 4, 56.7)", 20.3875);
108+
109+// Test AVERAGE
110+testFormula("=AVERAGE(10, 20, 4.1)", 11.366666666666667);
111+
112+// Test AVERAGEA
113+testFormula("=AVERAGEA(10, 20, 4.1)", 11.366666666666667);