spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.SUM written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-19 00:21:08
stats
2 file(s) changed, 36 insertions(+), 0 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
 2index 6f83320..d653e03 100644
 3--- a/src/RawFormulas.ts
 4+++ b/src/RawFormulas.ts
 5@@ -429,7 +429,25 @@ var SINH = Formula["SINH"];
 6 var SPLIT = Formula["SPLIT"];
 7 var SQRT = Formula["SQRT"];
 8 var SQRTPI = Formula["SQRTPI"];
 9-var SUM = Formula["SUM"];
10+
11+/**
12+ * Returns the sum of a series of numbers and/or cells.
13+ * @param values The first number or range to add together.
14+ * @returns {number} The sum of the series
15+ * @constructor
16+ */
17+var SUM = function (...values) : number {
18+  checkArgumentsAtLeastLength(values, 1);
19+  var result = 0;
20+  for (var i = 0; i < values.length; i++) {
21+    if (values[i] instanceof Array) {
22+      result = result + SUM.apply(this, values[i]);
23+    } else {
24+      result = result + valueToNumber(values[i]);
25+    }
26+  }
27+  return result;
28+};
29 var SUMIF = Formula["SUMIF"];
30 var SUMPRODUCT = Formula["SUMPRODUCT"];
31 var SUMSQ = Formula["SUMSQ"];
32diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
33index a312b02..ac53cb2 100644
34--- a/tests/FormulasTest.ts
35+++ b/tests/FormulasTest.ts
36@@ -401,7 +401,24 @@ assertEquals(SQRT(9), 3);
37 
38 assertEquals(SQRTPI(9), 5.317361552716548);
39 
40+
41+// Test SUM
42+assertEquals(SUM(10), 10);
43 assertEquals(SUM(10, 10), 20);
44+assertEquals(SUM(10, [5, 5]), 20);
45+assertEquals(SUM("10", [5, 5]), 20);
46+assertEquals(SUM(false, [10, 10]), 20);
47+assertEquals(SUM(true, [10, 10]), 21);
48+catchAndAssertEquals(function() {
49+  return SUM([10, 10], "");
50+}, ERRORS.VALUE_ERROR);
51+catchAndAssertEquals(function() {
52+  return SUM([10, 10], "str");
53+}, ERRORS.VALUE_ERROR);
54+catchAndAssertEquals(function() {
55+  return SUM();
56+}, ERRORS.NA_ERROR);
57+
58 
59 assertEquals(SUMIF([1, 5, 10], ">2"), 15);
60