You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2026/09/18 14:29:47 UTC
(poi) branch trunk updated: Functions given an unexpected argument type return an error value instead of throwing (#1293)
This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new b58cf0d256 Functions given an unexpected argument type return an error value instead of throwing (#1293)
b58cf0d256 is described below
commit b58cf0d256943cb33255b2cd8fce812c53eba0ab
Author: PJ Fanning <pj...@users.noreply.github.com>
AuthorDate: Fri Sep 18 15:29:41 2026 +0100
Functions given an unexpected argument type return an error value instead of throwing (#1293)
Found by fuzzing every registered function with scalar, area and array arguments:
- COUNTIF(5,5), COUNTBLANK(5): IllegalArgumentException "Bad range arg type" -> #VALUE!
- INDEX(5,1): IllegalStateException "Incomplete code" -> 5 (a single value is a
1x1 array, as in Excel; an error argument is passed through)
- IFS(1,"a"), IFS("abc","a"): ClassCastException -> "a" / #VALUE! (the logical
test is coerced like IF's, so numbers and "TRUE"/"FALSE" work)
- POISSON(2,3,1): ClassCastException -> cumulative (any value coercible to a
boolean is accepted, "abc" is #VALUE!)
- MMULT(1,{1,2,3}): ClassCastException when the first argument is a scalar and the
result has more than one element -> the result array is anchored on the area
- BIN2DEC, HEX2DEC, OCT2DEC, FACTDOUBLE, COMPLEX (suffix), WORKDAY/WORKDAY.INTL
(days): an area or array argument threw IllegalStateException/IllegalArgumentException
from the coercion -> resolved to a single value first (#VALUE! for a
multi-cell area, the value for a one-cell one)
Co-authored-by: Claude Opus 5 (1M context) <no...@anthropic.com>
---
.../poi/ss/formula/atp/ArgumentsEvaluator.java | 2 +-
.../java/org/apache/poi/ss/formula/atp/Ifs.java | 14 +-
.../apache/poi/ss/formula/functions/Bin2Dec.java | 10 +-
.../apache/poi/ss/formula/functions/Complex.java | 7 +-
.../poi/ss/formula/functions/Countblank.java | 3 +-
.../apache/poi/ss/formula/functions/Countif.java | 12 +-
.../poi/ss/formula/functions/FactDouble.java | 5 +-
.../apache/poi/ss/formula/functions/Hex2Dec.java | 8 +-
.../org/apache/poi/ss/formula/functions/Index.java | 20 +--
.../poi/ss/formula/functions/MatrixFunction.java | 8 +-
.../apache/poi/ss/formula/functions/Oct2Dec.java | 8 +-
.../apache/poi/ss/formula/functions/Poisson.java | 6 +-
.../ss/formula/functions/TestArgumentTypes.java | 149 +++++++++++++++++++++
13 files changed, 215 insertions(+), 37 deletions(-)
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/ArgumentsEvaluator.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/ArgumentsEvaluator.java
index af0e83ec1e..6d1b9ef89a 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/atp/ArgumentsEvaluator.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/ArgumentsEvaluator.java
@@ -121,6 +121,6 @@ final class ArgumentsEvaluator {
return 0f;
}
- return OperandResolver.coerceValueToDouble(arg);
+ return OperandResolver.coerceValueToDouble(OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol));
}
}
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/Ifs.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/Ifs.java
index 61ed34905a..acfae79d18 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/atp/Ifs.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/Ifs.java
@@ -18,8 +18,9 @@
package org.apache.poi.ss.formula.atp;
import org.apache.poi.ss.formula.OperationEvaluationContext;
-import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.EvaluationException;
+import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
@@ -48,8 +49,15 @@ final class Ifs implements FreeRefFunction {
}
for (int i = 0; i < args.length; i = i + 2) {
- BoolEval logicalTest = (BoolEval) args[i];
- if (logicalTest.getBooleanValue()) {
+ boolean logicalTest;
+ try {
+ ValueEval ve = OperandResolver.getSingleValue(args[i], ec.getRowIndex(), ec.getColumnIndex());
+ Boolean b = OperandResolver.coerceValueToBoolean(ve, false);
+ logicalTest = b != null && b;
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ if (logicalTest) {
return args[i + 1];
}
}
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Bin2Dec.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Bin2Dec.java
index e6217c239e..365c288894 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Bin2Dec.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Bin2Dec.java
@@ -20,7 +20,7 @@ import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.OperandResolver;
-import org.apache.poi.ss.formula.eval.RefEval;
+import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.util.MathUtil;
import org.apache.poi.util.StringUtil;
@@ -47,10 +47,10 @@ public class Bin2Dec extends Fixed1ArgFunction implements FreeRefFunction {
@Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) {
final String number;
- if (numberVE instanceof RefEval re) {
- number = OperandResolver.coerceValueToString(re.getInnerValueEval(re.getFirstSheetIndex()));
- } else {
- number = OperandResolver.coerceValueToString(numberVE);
+ try {
+ number = OperandResolver.coerceValueToString(OperandResolver.getSingleValue(numberVE, srcRowIndex, srcColumnIndex));
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
}
if (number.length() > 10) {
return ErrorEval.NUM_ERROR;
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Complex.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Complex.java
index 99362bed8c..b5639f4617 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Complex.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Complex.java
@@ -90,7 +90,12 @@ public class Complex extends Var2or3ArgFunction implements FreeRefFunction {
return ErrorEval.VALUE_INVALID;
}
- String suffixValue = OperandResolver.coerceValueToString(suffix);
+ String suffixValue;
+ try {
+ suffixValue = OperandResolver.coerceValueToString(OperandResolver.getSingleValue(suffix, srcRowIndex, srcColumnIndex));
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
if (suffixValue.isEmpty()) {
suffixValue = DEFAULT_SUFFIX;
}
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Countblank.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Countblank.java
index 9b7ad7c6e8..107289687e 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Countblank.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Countblank.java
@@ -41,7 +41,8 @@ public final class Countblank extends Fixed1ArgFunction {
} else if (arg0 instanceof ThreeDEval threeDEval) {
result = CountUtils.countMatchingCellsInArea(threeDEval, predicate);
} else {
- throw new IllegalArgumentException("Bad range arg type (" + arg0.getClass().getName() + ")");
+ // the range argument must be a reference or an array
+ return ErrorEval.VALUE_INVALID;
}
return new NumberEval(result);
}
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Countif.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Countif.java
index 871dbb4a82..652f60b0a4 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Countif.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Countif.java
@@ -406,20 +406,24 @@ public final class Countif extends Fixed2ArgFunction {
// If the criteria arg is a reference to a blank cell, countif always returns zero.
return NumberEval.ZERO;
}
- double result = countMatchingCellsInArea(arg0, mp);
- return new NumberEval(result);
+ try {
+ return new NumberEval(countMatchingCellsInArea(arg0, mp));
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
}
/**
* @return the number of evaluated cells in the range that match the specified criteria
*/
- private double countMatchingCellsInArea(ValueEval rangeArg, I_MatchPredicate criteriaPredicate) {
+ private double countMatchingCellsInArea(ValueEval rangeArg, I_MatchPredicate criteriaPredicate) throws EvaluationException {
if (rangeArg instanceof RefEval refEval) {
return CountUtils.countMatchingCellsInRef(refEval, criteriaPredicate);
} else if (rangeArg instanceof ThreeDEval threeDEval) {
return CountUtils.countMatchingCellsInArea(threeDEval, criteriaPredicate);
} else {
- throw new IllegalArgumentException("Bad range arg type (" + rangeArg.getClass().getName() + ")");
+ // the range argument must be a reference or an array
+ throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
}
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/FactDouble.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/FactDouble.java
index 9c7c417a76..b030a82c01 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/FactDouble.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/FactDouble.java
@@ -52,9 +52,10 @@ public class FactDouble extends Fixed1ArgFunction implements FreeRefFunction {
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) {
double number;
try {
- number = ExcelArithmetic.truncate(OperandResolver.coerceValueToDouble(numberVE));
+ ValueEval ve = OperandResolver.getSingleValue(numberVE, srcRowIndex, srcColumnIndex);
+ number = ExcelArithmetic.truncate(OperandResolver.coerceValueToDouble(ve));
} catch (EvaluationException e) {
- return ErrorEval.VALUE_INVALID;
+ return e.getErrorEval();
}
// 300!! is about 8.2E307; 301!! and 302!! overflow a double
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Hex2Dec.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Hex2Dec.java
index 16c4469e33..95c140258f 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Hex2Dec.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Hex2Dec.java
@@ -43,10 +43,10 @@ public class Hex2Dec extends Fixed1ArgFunction implements FreeRefFunction {
@Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) {
final String hex;
- if (numberVE instanceof RefEval re) {
- hex = OperandResolver.coerceValueToString(re.getInnerValueEval(re.getFirstSheetIndex()));
- } else {
- hex = OperandResolver.coerceValueToString(numberVE);
+ try {
+ hex = OperandResolver.coerceValueToString(OperandResolver.getSingleValue(numberVE, srcRowIndex, srcColumnIndex));
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
}
try {
return new NumberEval(BaseNumberUtils.convertToDecimal(hex, HEXADECIMAL_BASE, MAX_NUMBER_OF_PLACES));
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Index.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Index.java
index 9f04f70ff2..5a6737761d 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Index.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Index.java
@@ -19,6 +19,7 @@ package org.apache.poi.ss.formula.functions;
import org.apache.poi.ss.formula.eval.BlankEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.CacheAreaEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.MissingArgEval;
import org.apache.poi.ss.formula.eval.OperandResolver;
@@ -46,10 +47,9 @@ public final class Index implements Function2Arg, Function3Arg, Function4Arg, Ar
@Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
- TwoDEval reference = convertFirstArg(arg0);
-
int columnIx = 0;
try {
+ TwoDEval reference = convertFirstArg(arg0, srcRowIndex, srcColumnIndex);
int rowIx = resolveIndexArg(arg1, srcRowIndex, srcColumnIndex);
if (!reference.isColumn()) {
@@ -72,9 +72,8 @@ public final class Index implements Function2Arg, Function3Arg, Function4Arg, Ar
@Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
ValueEval arg2) {
- TwoDEval reference = convertFirstArg(arg0);
-
try {
+ TwoDEval reference = convertFirstArg(arg0, srcRowIndex, srcColumnIndex);
int columnIx = resolveIndexArg(arg2, srcRowIndex, srcColumnIndex);
int rowIx = resolveIndexArg(arg1, srcRowIndex, srcColumnIndex);
return getValueFromArea(reference, rowIx, columnIx);
@@ -93,7 +92,8 @@ public final class Index implements Function2Arg, Function3Arg, Function4Arg, Ar
// The formula parser doesn't seem to support this yet. Not sure if the evaluator does either
}
- private static TwoDEval convertFirstArg(ValueEval arg0) {
+ private static TwoDEval convertFirstArg(ValueEval arg0, int srcRowIndex, int srcColumnIndex)
+ throws EvaluationException {
if (arg0 instanceof RefEval refEval) {
// convert to area ref for simpler code in getValueFromArea()
return refEval.offset(0, 0, 0, 0);
@@ -101,11 +101,11 @@ public final class Index implements Function2Arg, Function3Arg, Function4Arg, Ar
if((arg0 instanceof TwoDEval twoDEval)) {
return twoDEval;
}
- // else the other variation of this function takes an array as the first argument
- // it seems like interface 'ArrayEval' does not even exist yet
- throw new IllegalStateException("Incomplete code - cannot handle first arg of type ("
- + arg0.getClass().getName() + ")");
-
+ if (arg0 instanceof ErrorEval ee) {
+ throw new EvaluationException(ee);
+ }
+ // a single value acts as a 1x1 array, e.g. INDEX(5,1) is 5
+ return new CacheAreaEval(srcRowIndex, srcColumnIndex, srcRowIndex, srcColumnIndex, new ValueEval[]{arg0});
}
@Override
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/MatrixFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/MatrixFunction.java
index 3c05eb5276..ec3a3a8927 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/MatrixFunction.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/MatrixFunction.java
@@ -223,9 +223,11 @@ public abstract class MatrixFunction implements Function{
if (result.length == 1)
return vals[0];
else {
- return new CacheAreaEval(((AreaEval) arg0).getFirstRow(), ((AreaEval) arg0).getFirstColumn(),
- ((AreaEval) arg0).getFirstRow() + height - 1,
- ((AreaEval) arg0).getFirstColumn() + width - 1, vals);
+ // more than one result, so at least one argument was an area: anchor the result on it
+ AreaEval anchor = arg0 instanceof AreaEval ae0 ? ae0 : (AreaEval) arg1;
+ return new CacheAreaEval(anchor.getFirstRow(), anchor.getFirstColumn(),
+ anchor.getFirstRow() + height - 1,
+ anchor.getFirstColumn() + width - 1, vals);
}
}
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Oct2Dec.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Oct2Dec.java
index dd17a63de8..f98b962004 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Oct2Dec.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Oct2Dec.java
@@ -19,6 +19,7 @@ package org.apache.poi.ss.formula.functions;
import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval;
@@ -46,7 +47,12 @@ public class Oct2Dec extends Fixed1ArgFunction implements FreeRefFunction {
@Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) {
- String octal = OperandResolver.coerceValueToString(numberVE);
+ final String octal;
+ try {
+ octal = OperandResolver.coerceValueToString(OperandResolver.getSingleValue(numberVE, srcRowIndex, srcColumnIndex));
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
try {
return new NumberEval(BaseNumberUtils.convertToDecimal(octal, OCTAL_BASE, MAX_NUMBER_OF_PLACES));
} catch (IllegalArgumentException e) {
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Poisson.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Poisson.java
index 29c49e0052..4e1abb008a 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Poisson.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Poisson.java
@@ -19,9 +19,9 @@ package org.apache.poi.ss.formula.functions;
import org.apache.commons.math3.distribution.PoissonDistribution;
import org.apache.poi.ss.formula.OperationEvaluationContext;
-import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
+import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.util.MathUtil;
@@ -86,7 +86,9 @@ public class Poisson implements FreeRefFunction {
checkArgument(mean);
// truncate x : as per excel function def
- boolean cumulative = ((BoolEval)arg2).getBooleanValue();
+ ValueEval cumulativeVE = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex);
+ Boolean cumulativeArg = OperandResolver.coerceValueToBoolean(cumulativeVE, false);
+ boolean cumulative = cumulativeArg != null && cumulativeArg;
PoissonDistribution poissonDistribution = new PoissonDistribution(mean);
double result = cumulative ?
poissonDistribution.cumulativeProbability( MathUtil.safeDoubleToInt(x)) :
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestArgumentTypes.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestArgumentTypes.java
new file mode 100644
index 0000000000..2d01ddbb9f
--- /dev/null
+++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestArgumentTypes.java
@@ -0,0 +1,149 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.FormulaError;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+
+import static org.apache.poi.ss.util.Utils.assertDouble;
+import static org.apache.poi.ss.util.Utils.assertError;
+import static org.apache.poi.ss.util.Utils.assertString;
+
+/**
+ * Functions given an argument of a type they did not expect used to escape the evaluator with a
+ * ClassCastException, IllegalArgumentException or IllegalStateException; Excel either accepts the
+ * value (a number as a logical, a single value as an array, a one-cell area as a value) or reports #VALUE!.
+ */
+final class TestArgumentTypes {
+
+ private static HSSFCell formulaCell(HSSFWorkbook wb) {
+ HSSFSheet sheet = wb.createSheet();
+ HSSFRow row = sheet.createRow(0);
+ row.createCell(0).setCellValue("101");
+ row.createCell(1).setCellValue(5);
+ sheet.createRow(1).createCell(0).setCellValue("11");
+ return sheet.createRow(9).createCell(0);
+ }
+
+ @Test
+ void testCountFunctionsNeedARange() throws IOException {
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ HSSFCell cell = formulaCell(wb);
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ assertError(fe, cell, "COUNTIF(5,5)", FormulaError.VALUE);
+ assertError(fe, cell, "COUNTIF(\"abc\",\"abc\")", FormulaError.VALUE);
+ assertError(fe, cell, "COUNTBLANK(5)", FormulaError.VALUE);
+ assertError(fe, cell, "COUNTBLANK(TRUE)", FormulaError.VALUE);
+ assertDouble(fe, cell, "COUNTIF(A1:B2,5)", 1, 0);
+ assertDouble(fe, cell, "COUNTBLANK(A1:B2)", 1, 0);
+ }
+ }
+
+ @Test
+ void testIndexOfASingleValue() throws IOException {
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ HSSFCell cell = formulaCell(wb);
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ assertDouble(fe, cell, "INDEX(5,1)", 5, 0);
+ assertDouble(fe, cell, "INDEX(5,1,1)", 5, 0);
+ assertDouble(fe, cell, "INDEX(5,0)", 5, 0);
+ assertString(fe, cell, "INDEX(\"abc\",1)", "abc");
+ assertError(fe, cell, "INDEX(5,2)", FormulaError.REF);
+ assertError(fe, cell, "INDEX(5,1,2)", FormulaError.REF);
+ assertError(fe, cell, "INDEX(NA(),1)", FormulaError.NA);
+ }
+ }
+
+ @Test
+ void testIfsCoercesTheLogicalTest() throws IOException {
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ HSSFCell cell = formulaCell(wb);
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ assertString(fe, cell, "IFS(1,\"a\")", "a");
+ assertString(fe, cell, "IFS(0,\"a\",2.5,\"b\")", "b");
+ assertString(fe, cell, "IFS(B1=5,\"a\")", "a");
+ assertString(fe, cell, "IFS(\"TRUE\",\"a\")", "a");
+ assertError(fe, cell, "IFS(0,\"a\")", FormulaError.NA);
+ assertError(fe, cell, "IFS(FALSE,\"a\")", FormulaError.NA);
+ assertError(fe, cell, "IFS(\"abc\",\"a\")", FormulaError.VALUE);
+ assertError(fe, cell, "IFS(NA(),\"a\")", FormulaError.NA);
+ }
+ }
+
+ @Test
+ void testPoissonCumulativeFlag() throws IOException {
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ HSSFCell cell = formulaCell(wb);
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ assertDouble(fe, cell, "POISSON(2,3,TRUE)", 0.423190081126869, 1E-14);
+ assertDouble(fe, cell, "POISSON(2,3,1)", 0.423190081126869, 1E-14);
+ assertDouble(fe, cell, "POISSON(2,3,0)", 0.224041807655388, 1E-14);
+ assertDouble(fe, cell, "POISSON.DIST(2,3,1)", 0.423190081126869, 1E-14);
+ assertError(fe, cell, "POISSON(2,3,\"abc\")", FormulaError.VALUE);
+ assertError(fe, cell, "POISSON(2,3,NA())", FormulaError.NA);
+ }
+ }
+
+ @Test
+ void testMatrixFunctionsWithAScalarFirstArgument() throws IOException {
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ HSSFCell cell = formulaCell(wb);
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ // 1x1 times 1x3 is 1x3; a plain cell shows its first element rather than throwing
+ assertDouble(fe, cell, "MMULT(2,{1,2,3})", 2, 0);
+ assertDouble(fe, cell, "MMULT(2,3)", 6, 0);
+ }
+ }
+
+ @Test
+ void testScalarArgumentsGivenAsAreaOrArray() throws IOException {
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ HSSFCell cell = formulaCell(wb);
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ // a one-element array is its value
+ assertDouble(fe, cell, "BIN2DEC({101})", 5, 0);
+ assertDouble(fe, cell, "HEX2DEC({\"FF\"})", 255, 0);
+ assertDouble(fe, cell, "OCT2DEC({\"17\"})", 15, 0);
+ assertDouble(fe, cell, "FACTDOUBLE({5})", 15, 0);
+ assertString(fe, cell, "COMPLEX(1,1,{\"j\"})", "1+j");
+ assertDouble(fe, cell, "WORKDAY(1,{5})", 8, 0);
+ assertDouble(fe, cell, "WORKDAY.INTL(1,{5})", 8, 0);
+ // a reference to one cell is its value
+ assertDouble(fe, cell, "BIN2DEC(A1)", 5, 0);
+ assertDouble(fe, cell, "FACTDOUBLE(B1)", 15, 0);
+ // a multi-cell area that does not intersect the formula's row is #VALUE!
+ assertError(fe, cell, "BIN2DEC(A1:A2)", FormulaError.VALUE);
+ assertError(fe, cell, "HEX2DEC(A1:A2)", FormulaError.VALUE);
+ assertError(fe, cell, "OCT2DEC(A1:A2)", FormulaError.VALUE);
+ assertError(fe, cell, "FACTDOUBLE(A1:A2)", FormulaError.VALUE);
+ assertError(fe, cell, "COMPLEX(1,1,A1:A2)", FormulaError.VALUE);
+ assertError(fe, cell, "WORKDAY(1,A1:A2)", FormulaError.VALUE);
+ assertError(fe, cell, "WORKDAY.INTL(1,A1:A2)", FormulaError.VALUE);
+ // an error argument is passed through
+ assertError(fe, cell, "FACTDOUBLE(NA())", FormulaError.NA);
+ assertError(fe, cell, "BIN2DEC(NA())", FormulaError.NA);
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org