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.SUMX2MY2
author
Ben Vogt <[email protected]>
date
2017-02-18 22:13:27
stats
2 file(s) changed, 46 insertions(+), 2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 2index 4343863..a1f4294 100644
 3--- a/src/RawFormulas/RawFormulas.ts
 4+++ b/src/RawFormulas/RawFormulas.ts
 5@@ -128,11 +128,38 @@ var __COMPLEX = {
 6   "F.DIST": Formula["FDIST"],
 7   "F.INV": Formula["FINV"]
 8 };
 9-var SUMX2MY2 = Formula["SUMX2MY2"];
10 var SUMX2PY2 = Formula["SUMX2PY2"];
11 var YEARFRAC = Formula["YEARFRAC"];
12 
13 
14+/**
15+ * Calculates the sum of the differences of the squares of values in two arrays.
16+ * @param values[0] array_x - The array or range of values whose squares will be reduced by the squares of corresponding
17+ * entries in array_y and added together.
18+ * @param values[1] array_y - The array or range of values whose squares will be subtracted from the squares of
19+ * corresponding entries in array_x and added together.
20+ * @returns {number} sum of the differences of the squares
21+ * @constructor
22+ */
23+var SUMX2MY2 = function (...values) : number {
24+  ArgsChecker.checkLength(values, 2);
25+  var arrOne = Filter.flattenAndThrow(values[0]);
26+  var arrTwo = Filter.flattenAndThrow(values[1]);
27+  if (arrOne.length !== arrTwo.length) {
28+    throw new CellError(ERRORS.NA_ERROR, "Array arguments to SUMX2MY2 are of different size.");
29+  }
30+  var result = 0;
31+  for (var i = 0; i < arrOne.length; i++) {
32+    // If either values at this index are anything but numbers, skip them. This is the behavior in GS at least.
33+    if (typeof arrOne[i] === "number" && typeof arrTwo[i] === "number") {
34+      result += TypeCaster.firstValueAsNumber(arrOne[i]) * TypeCaster.firstValueAsNumber(arrOne[i])
35+        - TypeCaster.firstValueAsNumber(arrTwo[i]) * TypeCaster.firstValueAsNumber(arrTwo[i]);
36+    }
37+  }
38+  return result;
39+};
40+
41+
42 /**
43  * Counts the number of unique values in a list of specified values and ranges.
44  * @param values The values or ranges to consider for uniqueness. Supports an arbitrary number of arguments for this
45diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
46index 396d8c2..99a030f 100644
47--- a/tests/FormulasTest.ts
48+++ b/tests/FormulasTest.ts
49@@ -1587,7 +1587,23 @@ catchAndAssertEquals(function() {
50 }, ERRORS.NA_ERROR);
51 
52 
53+// Test SUMX2MY2
54 assertEquals(SUMX2MY2([1,2,3],[4,5,6]), -63);
55+assertEquals(SUMX2MY2(["1",2,3],[4,5,6]), -48);
56+assertEquals(SUMX2MY2(["",2,3],[4,5,6]), -48);
57+assertEquals(SUMX2MY2([false,2,3],[4,5,6]), -48);
58+assertEquals(SUMX2MY2([true,2,3],[4,5,6]), -48);
59+catchAndAssertEquals(function() {
60+  SUMX2MY2([1,2,3],[4,5, []]);
61+}, ERRORS.REF_ERROR);
62+catchAndAssertEquals(function() {
63+  SUMX2MY2([1,2,3],[4,5]);
64+}, ERRORS.NA_ERROR);
65+catchAndAssertEquals(function() {
66+  SUMX2MY2();
67+}, ERRORS.NA_ERROR);
68+
69+
70 
71 assertEquals(SUMX2PY2([1, 2, 3], [4, 5, 6]), 91);
72