aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/Sumif.java51
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java2
2 files changed, 26 insertions, 27 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Sumif.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Sumif.java
index e9cd37fdcc..e9a9f2c9c9 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Sumif.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Sumif.java
@@ -66,45 +66,46 @@ public final class Sumif extends Var2or3ArgFunction {
return eval(srcRowIndex, srcColumnIndex, arg1, aeRange, aeSum);
}
- private static ValueEval eval(int srcRowIndex, int srcColumnIndex, ValueEval arg1, AreaEval aeRange,
- AreaEval aeSum) {
- // TODO - junit to prove last arg must be srcColumnIndex and not srcRowIndex
+ private static ValueEval eval(int srcRowIndex, int srcColumnIndex, ValueEval arg1, AreaEval aeRange, AreaEval aeSum) {
I_MatchPredicate mp = Countif.createCriteriaPredicate(arg1, srcRowIndex, srcColumnIndex);
-
- // handle empty cells
- if(mp == null) {
+ if (mp == null) {
return NumberEval.ZERO;
- }
+ } else {
+ try {
+ double result = sumMatchingCells(aeRange, mp, aeSum);
+ return new NumberEval(result);
+ } catch (EvaluationException var) {
+ return var.getErrorEval();
+ }
- double result = sumMatchingCells(aeRange, mp, aeSum);
- return new NumberEval(result);
+ }
}
- private static double sumMatchingCells(AreaEval aeRange, I_MatchPredicate mp, AreaEval aeSum) {
- int height=aeRange.getHeight();
- int width= aeRange.getWidth();
+ private static double sumMatchingCells(AreaEval aeRange, I_MatchPredicate mp, AreaEval aeSum) throws EvaluationException {
+ int height = aeRange.getHeight();
+ int width = aeRange.getWidth();
+ double result = 0.0D;
- double result = 0.0;
- for (int r=0; r<height; r++) {
- for (int c=0; c<width; c++) {
+ for(int r = 0; r < height; ++r) {
+ for(int c = 0; c < width; ++c) {
result += accumulate(aeRange, mp, aeSum, r, c);
}
}
+
return result;
}
- private static double accumulate(AreaEval aeRange, I_MatchPredicate mp, AreaEval aeSum, int relRowIndex,
- int relColIndex) {
-
+ private static double accumulate(AreaEval aeRange, I_MatchPredicate mp, AreaEval aeSum, int relRowIndex, int relColIndex) throws EvaluationException {
if (!mp.matches(aeRange.getRelativeValue(relRowIndex, relColIndex))) {
- return 0.0;
- }
- ValueEval addend = aeSum.getRelativeValue(relRowIndex, relColIndex);
- if (addend instanceof NumberEval) {
- return ((NumberEval)addend).getNumberValue();
+ return 0.0D;
+ } else {
+ ValueEval addend = aeSum.getRelativeValue(relRowIndex, relColIndex);
+ if (addend instanceof NumberEval) {
+ return ((NumberEval) addend).getNumberValue();
+ } else {
+ throw new EvaluationException(ErrorEval.NA);
+ }
}
- // everything else (including string and boolean values) counts as zero
- return 0.0;
}
/**
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java
index 581ea8fd2b..d960224c4e 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java
@@ -30,7 +30,6 @@ import org.apache.poi.ss.formula.eval.NumericValueEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.usermodel.*;
-import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.io.IOException;
@@ -118,7 +117,6 @@ final class TestSumif {
}
}
- @Disabled("https://bz.apache.org/bugzilla/show_bug.cgi?id=65475")
@Test
void testMicrosoftExample1WithNA() throws IOException {
try (HSSFWorkbook wb = initWorkbook1WithNA()) {