import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.functions.LookupUtils;
-import java.util.Optional;
-
/**
* Implementation of Excel function XLOOKUP()
*
if (args.length < 3) {
return ErrorEval.VALUE_INVALID;
}
- Optional<String> notFound = Optional.empty();
+ ValueEval notFound = BlankEval.instance;
if (args.length > 3) {
try {
ValueEval notFoundValue = OperandResolver.getSingleValue(args[3], srcRowIndex, srcColumnIndex);
- String notFoundText = laxValueToString(notFoundValue);
- if (notFoundText != null) {
- String trimmedText = notFoundText.trim();
- if (trimmedText.length() > 0) {
- notFound = Optional.of(trimmedText);
- }
+ if (notFoundValue != null) {
+ notFound = notFoundValue;
}
} catch (EvaluationException e) {
return e.getErrorEval();
}
private ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval lookupEval, ValueEval indexEval,
- ValueEval returnEval, Optional<String> notFound, LookupUtils.MatchMode matchMode,
+ ValueEval returnEval, ValueEval notFound, LookupUtils.MatchMode matchMode,
LookupUtils.SearchMode searchMode, boolean isSingleValue) {
try {
ValueEval lookupValue = OperandResolver.getSingleValue(lookupEval, srcRowIndex, srcColumnIndex);
matchedRow = LookupUtils.xlookupIndexOfValue(lookupValue, LookupUtils.createColumnVector(tableArray, 0), matchMode, searchMode);
} catch (EvaluationException e) {
if (ErrorEval.NA.equals(e.getErrorEval())) {
- if (notFound.isPresent()) {
+ if (notFound != BlankEval.instance) {
if (returnEval instanceof AreaEval) {
AreaEval area = (AreaEval)returnEval;
int width = area.getWidth();
if (isSingleValue || width <= 1) {
- return new StringEval(notFound.get());
+ return notFound;
}
- return notFoundAreaEval(notFound.get(), width);
+ return notFoundAreaEval(notFound, width);
} else {
- return new StringEval(notFound.get());
+ return notFound;
}
}
return ErrorEval.NA;
}
}
- private String laxValueToString(ValueEval eval) {
- return (eval instanceof MissingArgEval) ? "" : OperandResolver.coerceValueToString(eval);
- }
-
- private AreaEval notFoundAreaEval(String notFound, int width) {
+ private AreaEval notFoundAreaEval(ValueEval notFound, int width) {
return new AreaEval() {
@Override
public int getFirstRow() {
@Override
public ValueEval getAbsoluteValue(int row, int col) {
if (col == 0) {
- return new StringEval(notFound);
+ return notFound;
}
return new StringEval("");
}
}
}
+ @Test
+ void testReverseBinarySearchWithInvalidValues() throws IOException {
+ try (HSSFWorkbook wb = initReverseWorkbook4WithInvalidIncomes()) {
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ HSSFCell cell = wb.getSheetAt(0).getRow(1).createCell(6);
+ assertDouble(fe, cell, "XLOOKUP(E2,C2:C7,B2:B7,0,1,-2)", 0.37);
+ //TODO next assertion is not working and needs investigation
+ //assertDouble(fe, cell, "XLOOKUP(9700,C2:C7,B2:B7,0,0,-2)", 0.1);
+ assertDouble(fe, cell, "XLOOKUP(39474,C2:C7,B2:B7,0,0,-2)", 0);
+ }
+ }
+
private HSSFWorkbook initWorkbook1() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
return wb;
}
+ private HSSFWorkbook initReverseWorkbook4WithInvalidIncomes() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ addRow(sheet, 0, null, "Tax Rate", "Max Income", null, "Income", "Tax Rate");
+ addRow(sheet, 1, null, 0.37, 510300, null, 46523);
+ addRow(sheet, 2, null, 0.35, "invalid");
+ addRow(sheet, 3, null, 0.32, "invalid");
+ addRow(sheet, 4, null, 0.24, "invalid");
+ addRow(sheet, 5, null, 0.22, "invalid");
+ addRow(sheet, 6, null, 0.10, 9700);
+ return wb;
+ }
+
}