use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class Lookup method evaluate.
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) {
try {
ValueEval lookupValue = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
TwoDEval aeLookupVector = LookupUtils.resolveTableArrayArg(arg1);
TwoDEval aeResultVector = LookupUtils.resolveTableArrayArg(arg2);
ValueVector lookupVector = createVector(aeLookupVector);
ValueVector resultVector = createVector(aeResultVector);
if (lookupVector.getSize() > resultVector.getSize()) {
// Excel seems to handle this by accessing past the end of the result vector.
throw new RuntimeException("Lookup vector and result vector of differing sizes not supported yet");
}
int index = LookupUtils.lookupIndexOfValue(lookupValue, lookupVector, true);
return resultVector.getItem(index);
} catch (EvaluationException e) {
return e.getErrorEval();
}
}
use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class TestHSSFFormulaEvaluator method testShortCircuitIfEvaluation.
/**
* The HSSFFormula evaluator performance benefits greatly from caching of intermediate cell values
*/
@Test
public void testShortCircuitIfEvaluation() throws IOException {
// Set up a simple IF() formula that has measurable evaluation cost for its operands.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCell cellA1 = row.createCell(0);
cellA1.setCellFormula("if(B1,C1,D1+E1+F1)");
// EvaluationListener to check which parts of the first formula get evaluated
for (int i = 1; i < 6; i++) {
// formulas are just literal constants "1".."5"
row.createCell(i).setCellFormula(String.valueOf(i));
}
EvalCountListener evalListener = new EvalCountListener();
WorkbookEvaluator evaluator = WorkbookEvaluatorTestHelper.createEvaluator(wb, evalListener);
ValueEval ve = evaluator.evaluate(HSSFEvaluationTestHelper.wrapCell(cellA1));
int evalCount = evalListener.getEvalCount();
if (evalCount == 6) {
// Without short-circuit-if evaluation, evaluating cell 'A1' takes 3 extra evaluations (for D1,E1,F1)
fail("Identifed bug 48195 - Formula evaluator should short-circuit IF() calculations.");
}
assertEquals(3, evalCount);
assertEquals(2.0, ((NumberEval) ve).getNumberValue(), 0D);
wb.close();
}
use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class TestSubtotal method confirmSubtotal.
private static void confirmSubtotal(int function, double expected) {
ValueEval[] values = new ValueEval[TEST_VALUES0.length];
for (int i = 0; i < TEST_VALUES0.length; i++) {
values[i] = new NumberEval(TEST_VALUES0[i]);
}
AreaEval arg1 = EvalFactory.createAreaEval("C1:D5", values);
ValueEval[] args = { new NumberEval(function), arg1 };
ValueEval result = new Subtotal().evaluate(args, 0, 0);
assertEquals(NumberEval.class, result.getClass());
assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0);
}
use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class TestSumif method testCriteriaArgRange.
/**
* test for bug observed near svn r882931
*/
public void testCriteriaArgRange() {
ValueEval[] arg0values = new ValueEval[] { _50, _60, _50, _50, _50, _30 };
ValueEval[] arg1values = new ValueEval[] { _30, _40, _50, _60 };
AreaEval arg0;
AreaEval arg1;
ValueEval ve;
arg0 = EvalFactory.createAreaEval("A3:B5", arg0values);
// single row range
arg1 = EvalFactory.createAreaEval("A2:D2", arg1values);
// invoking from cell C1
ve = invokeSumif(0, 2, arg0, arg1);
if (ve instanceof NumberEval) {
NumberEval ne = (NumberEval) ve;
if (ne.getNumberValue() == 30.0) {
throw new AssertionFailedError("identified error in SUMIF - criteria arg not evaluated properly");
}
}
confirmDouble(200, ve);
arg0 = EvalFactory.createAreaEval("C1:D3", arg0values);
// single column range
arg1 = EvalFactory.createAreaEval("B1:B4", arg1values);
// invoking from cell A4
ve = invokeSumif(3, 0, arg0, arg1);
confirmDouble(60, ve);
}
use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class TestSumif method testBasic.
public void testBasic() {
ValueEval[] arg0values = new ValueEval[] { _30, _30, _40, _40, _50, _50 };
ValueEval[] arg2values = new ValueEval[] { _30, _40, _50, _60, _60, _60 };
AreaEval arg0;
AreaEval arg2;
arg0 = EvalFactory.createAreaEval("A3:B5", arg0values);
arg2 = EvalFactory.createAreaEval("D1:E3", arg2values);
confirm(60.0, arg0, new NumberEval(30.0));
confirm(70.0, arg0, new NumberEval(30.0), arg2);
confirm(100.0, arg0, new StringEval(">45"));
confirm(100.0, arg0, new StringEval(">=45"));
confirm(100.0, arg0, new StringEval(">=50.0"));
confirm(140.0, arg0, new StringEval("<45"));
confirm(140.0, arg0, new StringEval("<=45"));
confirm(140.0, arg0, new StringEval("<=40.0"));
confirm(160.0, arg0, new StringEval("<>40.0"));
confirm(80.0, arg0, new StringEval("=40.0"));
}
Aggregations