spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Added Formulas.SUMSQ
author
Ben Vogt <[email protected]>
date
2017-02-16 02:10:29
stats
3 file(s) changed, 58 insertions(+), 1 deletions(-)
files
src/RawFormulas/RawFormulas.ts
src/RawFormulas/Utils.ts
tests/FormulasTest.ts
  1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
  2index a9c24ef..3c7decf 100644
  3--- a/src/RawFormulas/RawFormulas.ts
  4+++ b/src/RawFormulas/RawFormulas.ts
  5@@ -76,6 +76,7 @@ import {
  6   CriteriaFunctionFactory,
  7   valueToNumber,
  8   checkArgumentsLength,
  9+  filterOutNonNumberValues,
 10   firstValueAsNumber,
 11   firstValueAsString,
 12   valueToBoolean,
 13@@ -136,11 +137,35 @@ var FISHERINV = Formula["FISHERINV"];
 14 var SPLIT = Formula["SPLIT"];
 15 var SQRTPI = Formula["SQRTPI"];
 16 var SUMPRODUCT = Formula["SUMPRODUCT"];
 17-var SUMSQ = Formula["SUMSQ"];
 18 var SUMX2MY2 = Formula["SUMX2MY2"];
 19 var SUMX2PY2 = Formula["SUMX2PY2"];
 20 var YEARFRAC = Formula["YEARFRAC"];
 21 
 22+
 23+/**
 24+ * Returns the sum of the squares of a series of numbers and/or cells.
 25+ * @param values  The values or range(s) whose squares to add together.
 26+ * @returns {number} the sum of the squares if the input.
 27+ * @constructor
 28+ */
 29+var SUMSQ = function (...values) {
 30+  checkArgumentsAtLeastLength(values, 1);
 31+  var result = 0;
 32+  for (var i = 0; i < values.length; i++) {
 33+    if (values[i] instanceof Array) {
 34+      if (values[i].length === 0) {
 35+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 36+      }
 37+      result = result + SUMSQ.apply(this, filterOutNonNumberValues(values[i]));
 38+    } else {
 39+      var n = valueToNumber(values[i]);
 40+      result = result + (n * n);
 41+    }
 42+  }
 43+  return result;
 44+};
 45+
 46+
 47 /**
 48  * Appends strings to one another.
 49  * @param values to append to one another. Must contain at least one value
 50diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
 51index d5d0145..c8956fc 100644
 52--- a/src/RawFormulas/Utils.ts
 53+++ b/src/RawFormulas/Utils.ts
 54@@ -50,6 +50,16 @@ function filterOutStringValues(arr: Array<any>) : Array<any> {
 55   return toReturn;
 56 }
 57 
 58+function filterOutNonNumberValues(arr: Array<any>) : Array<any> {
 59+  var toReturn = [];
 60+  for (var i = 0; i < arr.length; i++) {
 61+    if (typeof arr[i] !== "string" && typeof arr[i] !== "boolean") {
 62+      toReturn.push(arr[i]);
 63+    }
 64+  }
 65+  return toReturn;
 66+}
 67+
 68 /**
 69  * Convert a value to string.
 70  * @param value of any type, including array. array cannot be empty.
 71@@ -277,6 +287,7 @@ class CriteriaFunctionFactory {
 72 
 73 export {
 74   stringValuesToZeros,
 75+  filterOutNonNumberValues,
 76   flatten,
 77   valueCanCoerceToNumber,
 78   valueToNumber,
 79diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 80index af7d3d8..da32c19 100644
 81--- a/tests/FormulasTest.ts
 82+++ b/tests/FormulasTest.ts
 83@@ -1276,7 +1276,23 @@ catchAndAssertEquals(function() {
 84 
 85 assertEquals(SUMPRODUCT([1, 5, 10]), 16);
 86 
 87+// Test SUMSQ
 88 assertEquals(SUMSQ([1, 5, 10], 10), 226);
 89+assertEquals(SUMSQ([10, 10, 22, ""]), 684);
 90+assertEquals(SUMSQ(10, 10, 22), 684);
 91+assertEquals(SUMSQ(10, 10, "22", true), 685);
 92+assertEquals(SUMSQ(10, 10, "22", false), 684);
 93+assertEquals(SUMSQ([10, 10, 22, true]), 684);
 94+catchAndAssertEquals(function() {
 95+  SUMSQ([10, 10, 22, "", []]);
 96+}, ERRORS.REF_ERROR);
 97+catchAndAssertEquals(function() {
 98+  SUMSQ([]);
 99+}, ERRORS.REF_ERROR);
100+catchAndAssertEquals(function() {
101+  SUMSQ();
102+}, ERRORS.NA_ERROR);
103+
104 
105 assertEquals(SUMX2MY2([1,2,3],[4,5,6]), -63);
106