spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.MAX written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-21 23:46:32
stats
2 file(s) changed, 47 insertions(+), 0 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
 2index 67e0e74..275d39d 100644
 3--- a/src/RawFormulas.ts
 4+++ b/src/RawFormulas.ts
 5@@ -55,13 +55,13 @@ function valueToNumber(value: any) : number {
 6     if (value.indexOf(".") > -1) {
 7       var fl = parseFloat(value);
 8       if (isNaN(fl)) {
 9-        throw new CellError(ERRORS.VALUE_ERROR, "Function ____ parameter 1 expects number values, but is text and cannot be coerced to a number.");
10+        throw new CellError(ERRORS.VALUE_ERROR, "Function ____ expects number values, but is text and cannot be coerced to a number.");
11       }
12       return fl;
13     }
14     var fl = parseInt(value);
15     if (isNaN(fl)) {
16-      throw new CellError(ERRORS.VALUE_ERROR, "Function ____ parameter 1 expects number values, but is text and cannot be coerced to a number.");
17+      throw new CellError(ERRORS.VALUE_ERROR, "Function ____ expects number values, but is text and cannot be coerced to a number.");
18     }
19     return fl;
20   } else if (typeof value === "boolean") {
21@@ -449,7 +449,29 @@ var ISODD = Formula["ISODD"];
22 var LN = Formula["LN"];
23 var LOG = Formula["LOG"];
24 var LOG10 = Formula["LOG10"];
25-var MAX = Formula["MAX"];
26+
27+/**
28+ * Returns the maximum value in a numeric dataset.
29+ * @param values The values or range(s) to consider when calculating the maximum value.
30+ * @returns {number} the maximum value of the dataset
31+ * @constructor
32+ */
33+var MAX = function (...values) {
34+  checkArgumentsAtLeastLength(values, 1);
35+  var maxSoFar = -Infinity;
36+  for (var i = 0; i < values.length; i++) {
37+    if (values[i] instanceof Array) {
38+      if (values[i].length === 0) {
39+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
40+      }
41+      maxSoFar = Math.max(MAX.apply(this, values[i]), maxSoFar);
42+    } else {
43+      maxSoFar = Math.max(valueToNumber(values[i]), maxSoFar);
44+    }
45+  }
46+  return maxSoFar;
47+};
48+
49 var MAXA = Formula["MAXA"];
50 var MEDIAN = Formula["MEDIAN"];
51 var MIN = Formula["MIN"];
52diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
53index a2ff686..ff87a2d 100644
54--- a/tests/FormulasTest.ts
55+++ b/tests/FormulasTest.ts
56@@ -187,6 +187,8 @@ catchAndAssertEquals(function() {
57 
58 assertEquals(AVEDEV(1, 2, 4, 56.7), 20.3875);
59 
60+
61+// Test AVERAGE
62 assertEquals(AVERAGE(1, 2, 4, 55), 15.5);
63 assertEquals(AVERAGE(1, 2, 4, "55"), 15.5);
64 assertEquals(AVERAGE(1, 2, 4, 55, false), 12.4);
65@@ -377,7 +379,26 @@ assertEquals(LOG(256, 2), 8);
66 
67 assertEquals(LOG10(100), 2);
68 
69+
70+// Test MAX
71 assertEquals(MAX(100, 22), 100);
72+assertEquals(MAX(100, "22"), 100);
73+assertEquals(MAX(-100, false), 0);
74+assertEquals(MAX(-100, true), 1);
75+assertEquals(MAX(100, [101, 2]), 101);
76+catchAndAssertEquals(function() {
77+  return MAX(100, []);
78+}, ERRORS.REF_ERROR);
79+catchAndAssertEquals(function() {
80+  return MAX([]);
81+}, ERRORS.REF_ERROR);
82+catchAndAssertEquals(function() {
83+  return MAX();
84+}, ERRORS.NA_ERROR);
85+catchAndAssertEquals(function() {
86+  return MAX(100, "str");
87+}, ERRORS.VALUE_ERROR);
88+
89 
90 assertEquals(MAXA(100, 22, 44), 100);
91