Search in sources :

Example 31 with ValueEval

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();
    }
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) TwoDEval(org.apache.poi.ss.formula.TwoDEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 32 with ValueEval

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();
}
Also used : WorkbookEvaluator(org.apache.poi.ss.formula.WorkbookEvaluator) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) Test(org.junit.Test)

Example 33 with ValueEval

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);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 34 with ValueEval

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);
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) AssertionFailedError(junit.framework.AssertionFailedError) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 35 with ValueEval

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"));
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Aggregations

ValueEval (org.apache.poi.ss.formula.eval.ValueEval)223 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)101 StringEval (org.apache.poi.ss.formula.eval.StringEval)48 Test (org.junit.Test)39 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)36 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)29 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)28 OperationEvaluationContext (org.apache.poi.ss.formula.OperationEvaluationContext)27 Calendar (java.util.Calendar)16 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)13 Date (java.util.Date)11 RefEval (org.apache.poi.ss.formula.eval.RefEval)9 TwoDEval (org.apache.poi.ss.formula.TwoDEval)7 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)4 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)4 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)4 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)4 ArrayList (java.util.ArrayList)3 AssertionFailedError (junit.framework.AssertionFailedError)3 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)3