diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-07-30 11:03:33 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-07-30 11:03:33 +0000 |
commit | 979444134ef4be0f36b6f578cec9c401178282e9 (patch) | |
tree | 8ad42ace12306f9dcc8946ae383713d3d47972b1 /poi | |
parent | c13bb182c7d6d323141a88dace3247bbed1cc133 (diff) | |
download | poi-979444134ef4be0f36b6f578cec9c401178282e9.tar.gz poi-979444134ef4be0f36b6f578cec9c401178282e9.zip |
[bug-65475] SUMIF should return #N/A if any of the amounts to sum is #N/A. Thanks to zhangyajun.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1891896 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r-- | poi/src/main/java/org/apache/poi/ss/formula/functions/Sumif.java | 51 | ||||
-rw-r--r-- | poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java | 2 |
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()) { |