commit
message
[SUMIF] Formula now accepts ranges/arrays
author
Ben Vogt <[email protected]>
date
2017-05-01 23:45:29
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 80a582f..dd69b4e 100644
3--- a/src/Formulas/Math.ts
4+++ b/src/Formulas/Math.ts
5@@ -714,7 +714,6 @@ var ROUNDUP = function (...values) {
6 * @param values[2] (optional) The range to be summed, if different from range.
7 * @returns {number}
8 * @constructor
9- * TODO: This needs to take nested range values.
10 */
11 var SUMIF = function (...values) {
12 ArgsChecker.checkLengthWithin(values, 2, 3);
13@@ -730,13 +729,17 @@ var SUMIF = function (...values) {
14 var sum = 0;
15 for (var i = 0; i < range.length; i++) {
16 var x = range[i];
17- if (sumRange && i > sumRange.length-1) {
18- continue;
19- }
20- if (values.length === 2 && TypeCaster.canCoerceToNumber(x) && criteriaEvaluation(x)) {
21- sum = sum + TypeCaster.valueToNumber(x);
22- } else if (values.length === 3 && TypeCaster.canCoerceToNumber(sumRange[i]) && criteriaEvaluation(x)) {
23- sum = sum + TypeCaster.valueToNumber(sumRange[i]);
24+ if (x instanceof Array) {
25+ sum += SUMIF.apply(this, [x, criteria]);
26+ } else {
27+ if (sumRange && i > sumRange.length-1) {
28+ continue;
29+ }
30+ if (values.length === 2 && TypeCaster.canCoerceToNumber(x) && criteriaEvaluation(x)) {
31+ sum = sum + TypeCaster.valueToNumber(x);
32+ } else if (values.length === 3 && TypeCaster.canCoerceToNumber(sumRange[i]) && criteriaEvaluation(x)) {
33+ sum = sum + TypeCaster.valueToNumber(sumRange[i]);
34+ }
35 }
36 }
37 return sum;
38diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
39index 6ae5f6b..4606a89 100644
40--- a/tests/Formulas/MathTest.ts
41+++ b/tests/Formulas/MathTest.ts
42@@ -935,6 +935,9 @@ test("SUM", function(){
43
44
45 test("SUMIF", function(){
46+ assertEquals(SUMIF([1, 5, 10, [1, 1, 1, 1, 1], []], 1), 6);
47+ assertEquals(SUMIF([], 1), 0);
48+ assertEquals(SUMIF([1, 5, 10, [1, 1, "1.0", "1", 1]], 1), 4);
49 assertEquals(SUMIF([1, 5, 10], 5), 5);
50 assertEquals(SUMIF([1, 5, 5, 5, 10, 5], 5), 20);
51 assertEquals(SUMIF([1, 5, 5, 5, 10, 5], 10), 10);