spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[COUNTIF] Now accepts nested ranges/arrays
author
Ben Vogt <[email protected]>
date
2017-05-01 23:19:51
stats
2 file(s) changed, 14 insertions(+), 7 deletions(-)
files
src/Formulas/Math.ts
tests/Formulas/MathTest.ts
 1diff --git a/src/Formulas/Math.ts b/src/Formulas/Math.ts
 2index 1a0baf4..2653a6b 100644
 3--- a/src/Formulas/Math.ts
 4+++ b/src/Formulas/Math.ts
 5@@ -574,25 +574,28 @@ var IF = function (...values) : any {
 6 /**
 7  * Returns a conditional count across a range.
 8  * @param values[0] range - The range that is tested against criterion., value[1];
 9- * @param values[1] criterion - The pattern or test to apply to range. If the range to check against contains text, this must be a
10- * string. It can be a comparison based string (e.g. "=1", "<1", ">=1") or it can be a wild-card string, in which *
11- * matches any number of characters, and ? matches the next character. Both ? and * can be escaped by placing a ~ in
12- * front of them. If it is neither, it will compared with values in the range using equality comparison.
13+ * @param values[1] criterion - The pattern or test to apply to range. If the range to check against contains text,
14+ * this must be a string. It can be a comparison based string (e.g. "=1", "<1", ">=1") or it can be a wild-card string,
15+ * in which * matches any number of characters, and ? matches the next character. Both ? and * can be escaped by placing
16+ * a ~ in front of them. If it is neither, it will compared with values in the range using equality comparison.
17  * @returns {number}
18  * @constructor
19- * TODO: This needs to take nested range values.
20  */
21 var COUNTIF = function (...values) {
22   ArgsChecker.checkLength(values, 2);
23   var range = values[0];
24+  if (!(range instanceof Array)) {
25+    range = [range];
26+  }
27   var criteria = values[1];
28-
29   var criteriaEvaluation = CriteriaFunctionFactory.createCriteriaFunction(criteria);
30 
31   var count = 0;
32   for (var i = 0; i < range.length; i++) {
33     var x = range[i];
34-    if (criteriaEvaluation(x)) {
35+    if (x instanceof Array) {
36+      count = count + COUNTIF.apply(this, [x, criteria]);
37+    } else if (criteriaEvaluation(x)) {
38       count++;
39     }
40   }
41diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
42index 2427847..65081a0 100644
43--- a/tests/Formulas/MathTest.ts
44+++ b/tests/Formulas/MathTest.ts
45@@ -337,6 +337,10 @@ test("COTH", function(){
46 
47 
48 test("COUNTIF", function(){
49+  assertEquals(COUNTIF(10, "= 10"), 1);
50+  assertEquals(COUNTIF([1, 5, 5, [5, 5, 5, 5], 10, 5], "= 5"), 7);
51+  assertEquals(COUNTIF([1, 5, 5, [5, 5, 5, 5], [], 10, 5], "= 5"), 7);
52+  assertEquals(COUNTIF([1, 5, 5, [5, 5, "5", "5.000"], [], 10, 5], "= 5"), 7);
53   assertEquals(COUNTIF([1, 5, 10], ">4"), 2);
54   assertEquals(COUNTIF([1, 2, 2, 2, 2, 2, 2, 2], ">1"), 7);
55   assertEquals(COUNTIF([1, 5, 10], 5), 1);