Search in sources :

Example 56 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class TestTrunc method testTruncWithDecimalNumber.

public void testTruncWithDecimalNumber() {
    ValueEval[] args = { new NumberEval(2.612777), new NumberEval(3) };
    @SuppressWarnings("static-access") ValueEval result = F.TRUNC.evaluate(args, -1, (short) -1);
    assertEquals("TRUNC", (new NumberEval(2.612d)).getNumberValue(), ((NumberEval) result).getNumberValue());
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 57 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class TestValue method confirmValue.

private static void confirmValue(String strText, double expected) {
    ValueEval result = invokeValue(strText);
    assertEquals(NumberEval.class, result.getClass());
    assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval)

Example 58 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class TestXYNumericFunction method testErrors.

public void testErrors() {
    ValueEval[] xValues = { ErrorEval.REF_INVALID, new NumberEval(2) };
    ValueEval areaEvalX = createAreaEval(xValues);
    ValueEval[] yValues = { new NumberEval(2), ErrorEval.NULL_INTERSECTION };
    ValueEval areaEvalY = createAreaEval(yValues);
    ValueEval[] zValues = { // wrong size
    new NumberEval(2) };
    ValueEval areaEvalZ = createAreaEval(zValues);
    // if either arg is an error, that error propagates
    confirmError(ErrorEval.REF_INVALID, ErrorEval.NAME_INVALID, ErrorEval.REF_INVALID);
    confirmError(areaEvalX, ErrorEval.NAME_INVALID, ErrorEval.NAME_INVALID);
    confirmError(ErrorEval.NAME_INVALID, areaEvalX, ErrorEval.NAME_INVALID);
    // array sizes must match
    confirmError(areaEvalX, areaEvalZ, ErrorEval.NA);
    confirmError(areaEvalZ, areaEvalY, ErrorEval.NA);
    // any error in an array item propagates up
    confirmError(areaEvalX, areaEvalX, ErrorEval.REF_INVALID);
    // search for errors array by array, not pair by pair
    confirmError(areaEvalX, areaEvalY, ErrorEval.REF_INVALID);
    confirmError(areaEvalY, areaEvalX, ErrorEval.NULL_INTERSECTION);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 59 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class TestFunctionRegistry method testRegisterInRuntime.

public void testRegisterInRuntime() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");
    HSSFRow row = sheet.createRow(0);
    HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
    HSSFCell cellA = row.createCell(0);
    cellA.setCellFormula("FISHER(A5)");
    CellValue cv;
    try {
        cv = fe.evaluate(cellA);
        fail("expectecd exception");
    } catch (NotImplementedException e) {
    }
    FunctionEval.registerFunction("FISHER", new Function() {

        @Override
        public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
            return ErrorEval.NA;
        }
    });
    cv = fe.evaluate(cellA);
    assertEquals(ErrorEval.NA.getErrorCode(), cv.getErrorValue());
    HSSFCell cellB = row.createCell(1);
    cellB.setCellFormula("CUBEMEMBERPROPERTY(A5)");
    try {
        cv = fe.evaluate(cellB);
        fail("expectecd exception");
    } catch (NotImplementedException e) {
    }
    AnalysisToolPak.registerFunction("CUBEMEMBERPROPERTY", new FreeRefFunction() {

        @Override
        public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
            return ErrorEval.NUM_ERROR;
        }
    });
    cv = fe.evaluate(cellB);
    assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), cv.getErrorValue());
}
Also used : NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) Function(org.apache.poi.ss.formula.functions.Function) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) CellValue(org.apache.poi.ss.usermodel.CellValue)

Example 60 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class TestWorkbookEvaluator method testAttrSum.

/**
     * Make sure that the evaluator can directly handle tAttrSum (instead of relying on re-parsing
     * the whole formula which converts tAttrSum to tFuncVar("SUM") )
     */
@Test
public void testAttrSum() {
    Ptg[] ptgs = { new IntPtg(42), AttrPtg.SUM };
    ValueEval result = evaluateFormula(ptgs);
    assertEquals(42, ((NumberEval) result).getNumberValue(), 0.0);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) Test(org.junit.Test)

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