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.SUMX2PY2
author
Ben Vogt <[email protected]>
date
2017-02-18 22:29:25
stats
3 file(s) changed, 52 insertions(+), 7 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
tests/utils/Asserts.ts
  1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
  2index a1f4294..4a1cd1a 100644
  3--- a/src/RawFormulas/RawFormulas.ts
  4+++ b/src/RawFormulas/RawFormulas.ts
  5@@ -128,10 +128,35 @@ var __COMPLEX = {
  6   "F.DIST": Formula["FDIST"],
  7   "F.INV": Formula["FINV"]
  8 };
  9-var SUMX2PY2 = Formula["SUMX2PY2"];
 10 var YEARFRAC = Formula["YEARFRAC"];
 11 
 12 
 13+/**
 14+ * Calculates the sum of the sums of the squares of values in two arrays.
 15+ * @param values[0] array_x - The array or range of values whose squares will be added to the squares of corresponding
 16+ * entries in array_y and added together.
 17+ * @param values[1] array_y - The array or range of values whose squares will be added to the squares of corresponding
 18+ * entries in array_x and added together.
 19+ * @returns {number} sum of the sums of the squares
 20+ * @constructor
 21+ */
 22+var SUMX2PY2 = function (...values) : number {
 23+  ArgsChecker.checkLength(values, 2);
 24+  var arrOne = Filter.flattenAndThrow(values[0]);
 25+  var arrTwo = Filter.flattenAndThrow(values[1]);
 26+  if (arrOne.length !== arrTwo.length) {
 27+    throw new CellError(ERRORS.NA_ERROR, "Array arguments to SUMX2PY2 are of different size.");
 28+  }
 29+  var result = 0;
 30+  for (var i = 0; i < arrOne.length; i++) {
 31+    // If either values at this index are anything but numbers, skip them. This is the behavior in GS at least.
 32+    if (typeof arrOne[i] === "number" && typeof arrTwo[i] === "number") {
 33+      result += arrOne[i] * arrOne[i] + arrTwo[i] * arrTwo[i];
 34+    }
 35+  }
 36+  return result;
 37+};
 38+
 39 /**
 40  * Calculates the sum of the differences of the squares of values in two arrays.
 41  * @param values[0] array_x - The array or range of values whose squares will be reduced by the squares of corresponding
 42@@ -152,8 +177,7 @@ var SUMX2MY2 = function (...values) : number {
 43   for (var i = 0; i < arrOne.length; i++) {
 44     // If either values at this index are anything but numbers, skip them. This is the behavior in GS at least.
 45     if (typeof arrOne[i] === "number" && typeof arrTwo[i] === "number") {
 46-      result += TypeCaster.firstValueAsNumber(arrOne[i]) * TypeCaster.firstValueAsNumber(arrOne[i])
 47-        - TypeCaster.firstValueAsNumber(arrTwo[i]) * TypeCaster.firstValueAsNumber(arrTwo[i]);
 48+      result += arrOne[i] * arrOne[i] - arrTwo[i] * arrTwo[i];
 49     }
 50   }
 51   return result;
 52diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 53index 99a030f..be3532d 100644
 54--- a/tests/FormulasTest.ts
 55+++ b/tests/FormulasTest.ts
 56@@ -1589,6 +1589,7 @@ catchAndAssertEquals(function() {
 57 
 58 // Test SUMX2MY2
 59 assertEquals(SUMX2MY2([1,2,3],[4,5,6]), -63);
 60+assertEquals(SUMX2MY2([1, 2, 3], [[4, 5], [6]]), -63);
 61 assertEquals(SUMX2MY2(["1",2,3],[4,5,6]), -48);
 62 assertEquals(SUMX2MY2(["",2,3],[4,5,6]), -48);
 63 assertEquals(SUMX2MY2([false,2,3],[4,5,6]), -48);
 64@@ -1604,8 +1605,23 @@ catchAndAssertEquals(function() {
 65 }, ERRORS.NA_ERROR);
 66 
 67 
 68-
 69+// Test SUMX2PY2
 70 assertEquals(SUMX2PY2([1, 2, 3], [4, 5, 6]), 91);
 71+assertEquals(SUMX2PY2([1, 2, 3], [[4, 5], [6]]), 91);
 72+assertEquals(SUMX2PY2(["1",2,3],[4,5,6]), 74);
 73+assertEquals(SUMX2PY2(["",2,3],[4,5,6]), 74);
 74+assertEquals(SUMX2PY2([false,2,3],[4,5,6]), 74);
 75+assertEquals(SUMX2PY2([true,2,3],[4,5,6]), 74);
 76+catchAndAssertEquals(function() {
 77+  SUMX2PY2([1,2,3],[4,5, []]);
 78+}, ERRORS.REF_ERROR);
 79+catchAndAssertEquals(function() {
 80+  SUMX2PY2([1,2,3],[4,5]);
 81+}, ERRORS.NA_ERROR);
 82+catchAndAssertEquals(function() {
 83+  SUMX2PY2();
 84+}, ERRORS.NA_ERROR);
 85+
 86 
 87 // Test TAN
 88 assertEquals(TAN(0), 0);
 89diff --git a/tests/utils/Asserts.ts b/tests/utils/Asserts.ts
 90index 5ada522..c3ff7cc 100644
 91--- a/tests/utils/Asserts.ts
 92+++ b/tests/utils/Asserts.ts
 93@@ -1,18 +1,18 @@
 94-function assertEquals(expected, actual) {
 95+function assertEquals(actual, expected) {
 96   if (expected != actual) {
 97-    console.log(expected, "not equal to", actual);
 98+    console.log("expected:", expected, " actual:", actual);
 99     throw Error();
100   }
101 }
102 
103-function assertArrayEquals(expected: Array<any>, actual: Array<any>) {
104+function assertArrayEquals(actual: Array<any>, expected: Array<any>, ) {
105   if (expected.length != actual.length) {
106-    console.log(expected, "not equal to", actual);
107+    console.log("expected: ", expected, " actual:", actual);
108     throw Error();
109   }
110   for (var index in expected) {
111     if (expected[index] != actual[index]) {
112-      console.log(expected, "not equal to", actual);
113+      console.log("expected: ", expected, " actual:", actual);
114       throw Error();
115     }
116   }