spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.AVEDEV written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-22 19:06:49
stats
3 file(s) changed, 75 insertions(+), 2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
  2index cb1f3aa..385da38 100644
  3--- a/src/RawFormulas/RawFormulas.ts
  4+++ b/src/RawFormulas/RawFormulas.ts
  5@@ -29,9 +29,58 @@ import {
  6   TRUE,
  7   NOT
  8 } from "./Logical"
  9+import {checkArgumentsAtLeastLength, filterOutStringValues, valueToNumber} from "./Utils";
 10+import { CellError } from "../Errors"
 11+import * as ERRORS from "../Errors"
 12+
 13+
 14+function flatten(values: Array<any>) : Array<any> {
 15+  return values.reduce(function (flat, toFlatten) {
 16+    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
 17+  }, []);
 18+}
 19+
 20+var AVEDEV = function (...values) {
 21+  checkArgumentsAtLeastLength(values, 1);
 22+
 23+  // Sort to array-values, and non-array-values
 24+  var arrayValues = [];
 25+  var nonArrayValues = [];
 26+  for (var i = 0; i < values.length; i++) {
 27+    var X = values[i];
 28+    if (X instanceof Array) {
 29+      if (X.length === 0) {
 30+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
 31+      }
 32+      arrayValues.push(X);
 33+    } else {
 34+      nonArrayValues.push(valueToNumber(X));
 35+    }
 36+  }
 37+
 38+  // Remove string values from array-values, but not from non-array-values, and concat.
 39+  var flatValues = filterOutStringValues(flatten(arrayValues)).map(function (value) {
 40+    return valueToNumber(value);
 41+  }).concat(nonArrayValues);
 42+
 43+
 44+  // Calculating mean
 45+  var result = 0;
 46+  var count = 0;
 47+  for (var i = 0; i < flatValues.length; i++) {
 48+    result = result + valueToNumber(flatValues[i]);
 49+    count++;
 50+  }
 51+  var mean = result / count;
 52+
 53+  for (var i = 0; i < flatValues.length; i++) {
 54+    flatValues[i] = ABS(valueToNumber(flatValues[i]) - mean);
 55+  }
 56+  return SUM(flatValues) / flatValues.length;
 57+};
 58+
 59 
 60 var ACCRINT = Formula["ACCRINT"];
 61-var AVEDEV = Formula["AVEDEV"];
 62 var AVERAGEA = Formula["AVERAGEA"];
 63 var AVERAGEIF = Formula["AVERAGEIF"];
 64 var BASE = Formula["BASE"];
 65diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
 66index bb87b00..90ebb5c 100644
 67--- a/tests/FormulasTest.ts
 68+++ b/tests/FormulasTest.ts
 69@@ -185,7 +185,29 @@ catchAndAssertEquals(function() {
 70 }, ERRORS.VALUE_ERROR);
 71 
 72 
 73-assertEquals(AVEDEV(1, 2, 4, 56.7), 20.3875);
 74+// Test AVEDEV
 75+assertEquals(AVEDEV(1, 2, 4, 55), 19.75);
 76+assertEquals(AVEDEV(1, 2, 4, "55"), 19.75);
 77+assertEquals(AVEDEV([1, 2, 4, "55"]), 1.1111111111111112);
 78+assertEquals(AVEDEV([1, 2, 4, "55"], [10, 10, "str"]), 3.6799999999999997);
 79+assertEquals(AVEDEV([1, 2, 4, "55"], [10, 10]), 3.6799999999999997);
 80+assertEquals(AVEDEV(1, 2, 4, "55", [10, [10]]), 13.777777777777777);
 81+assertEquals(AVEDEV(1, 2, 4, "55", 10, 10), 13.77777777777778);
 82+assertEquals(AVEDEV(1, 2, 4, 55, false), 17.040000000000003);
 83+assertEquals(AVEDEV(1, 2, 4, 55, 0), 17.040000000000003);
 84+assertEquals(AVEDEV(1, 2, 4, 55, true), 16.959999999999997);
 85+assertEquals(AVEDEV(1, 2, 4, 55, 1), 16.959999999999997);
 86+assertEquals(AVEDEV([1, 2, 4, 55, 0]), 17.040000000000003);
 87+assertEquals(AVEDEV([1, 2, 4, 55], 0), 17.040000000000003);
 88+catchAndAssertEquals(function() {
 89+  AVEDEV();
 90+}, ERRORS.NA_ERROR);
 91+catchAndAssertEquals(function() {
 92+  AVEDEV(10, 10, "str");
 93+}, ERRORS.VALUE_ERROR);
 94+catchAndAssertEquals(function() {
 95+  AVEDEV(10, 10, {});
 96+}, ERRORS.REF_ERROR);
 97 
 98 
 99 // Test AVERAGE
100diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
101index 8c311a8..6de5fdd 100644
102--- a/tests/SheetFormulaTest.ts
103+++ b/tests/SheetFormulaTest.ts
104@@ -69,7 +69,7 @@ testFormula("=ATAN2(4, 3)", 0.6435011087932844);
105 testFormula("=ATANH(0.51)", 0.5627297693521489);
106 
107 // Test AVEDEV
108-testFormula("=AVEDEV(1, 2, 4, 56.7)", 20.3875);
109+testFormula("=AVEDEV(1, 2, 4, 56.7)", 20.387500000000003);
110 
111 // Test AVERAGE
112 testFormula("=AVERAGE(10, 20, 4.1)", 11.366666666666667);