spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Adding Formula.COUNTA
author
Ben Vogt <[email protected]>
date
2017-02-07 00:52:32
stats
2 file(s) changed, 40 insertions(+), 0 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 2index 4ba5bf7..51856e9 100644
 3--- a/src/RawFormulas/RawFormulas.ts
 4+++ b/src/RawFormulas/RawFormulas.ts
 5@@ -116,7 +116,31 @@ var COUNT = function (...values) : number {
 6   return count;
 7 };
 8 
 9-var COUNTA = Formula["COUNTA"];
10+
11+/**
12+ * Returns the a count of the number of values in a dataset.
13+ * @param values The values or ranges to consider when counting.
14+ * @returns {number} number of values in a dataset.
15+ * @constructor
16+ */
17+var COUNTA = function (...values) : number {
18+  checkArgumentsAtLeastLength(values, 1);
19+  var count = 0;
20+  for (var i = 0; i < values.length; i++) {
21+    if (values[i] instanceof Array) {
22+      if (values[i].length > 0) {
23+        count += COUNTA.apply(this, values[i]);
24+      } else {
25+        count++;
26+      }
27+    } else {
28+      count++;
29+    }
30+  }
31+  return count;
32+};
33+
34+
35 var COUNTIF = Formula["COUNTIF"];
36 var COUNTIFS = Formula["COUNTIFS"];
37 var COUNTIN = Formula["COUNTIN"];
38diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
39index fd772dd..aff0b6d 100644
40--- a/tests/FormulasTest.ts
41+++ b/tests/FormulasTest.ts
42@@ -458,7 +458,20 @@ catchAndAssertEquals(function() {
43 }, ERRORS.NA_ERROR);
44 
45 
46-assertEquals(COUNTA(10, 10, 22), 3);
47+// Test COUNTA
48+assertEquals(COUNTA(1, 2, 3), 3);
49+assertEquals(COUNTA(0, 1, 2, 3), 4);
50+assertEquals(COUNTA(0, 1, 2, 3, [], []), 6);
51+assertEquals(COUNTA(0, 1, 2, 3, [], ""), 6);
52+assertEquals(COUNTA(1, 2, "3"), 3);
53+assertEquals(COUNTA(1, 2, "3", ["str"]), 4);
54+assertEquals(COUNTA(1, 2, false), 3);
55+assertEquals(COUNTA(1, 2, true), 3);
56+assertEquals(COUNTA([]), 1);
57+catchAndAssertEquals(function() {
58+  COUNTA();
59+}, ERRORS.NA_ERROR);
60+
61 
62 assertEquals(COUNTIF([1, 5, 10], ">4"), 2);
63