Search in sources :

Example 16 with CellValue

use of org.apache.poi.ss.usermodel.CellValue in project poi by apache.

the class TestSubtotal method confirmExpectedResult.

private static void confirmExpectedResult(FormulaEvaluator evaluator, String msg, Cell cell, double expected) {
    CellValue value = evaluator.evaluate(cell);
    if (value.getErrorValue() != 0)
        throw new RuntimeException(msg + ": " + value.formatAsString());
    assertEquals(msg, expected, value.getNumberValue(), 0);
}
Also used : CellValue(org.apache.poi.ss.usermodel.CellValue)

Example 17 with CellValue

use of org.apache.poi.ss.usermodel.CellValue in project poi by apache.

the class TestNpv method testNpvFromSpreadsheet.

/**
     * evaluate formulas with NPV and compare the result with
     * the cached formula result pre-calculated by Excel
     */
public void testNpvFromSpreadsheet() {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("IrrNpvTestCaseData.xls");
    HSSFSheet sheet = wb.getSheet("IRR-NPV");
    HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
    StringBuffer failures = new StringBuffer();
    int failureCount = 0;
    // FormulaEvaluator as of r1041407 throws "Unexpected ptg class (org.apache.poi.ss.formula.ptg.ArrayPtg)"
    for (int rownum = 9; rownum <= 15; rownum++) {
        HSSFRow row = sheet.getRow(rownum);
        HSSFCell cellB = row.getCell(1);
        try {
            CellValue cv = fe.evaluate(cellB);
            assertFormulaResult(cv, cellB);
        } catch (Throwable e) {
            if (failures.length() > 0)
                failures.append('\n');
            failures.append("Row[" + (cellB.getRowIndex() + 1) + "]: " + cellB.getCellFormula() + " ");
            failures.append(e.getMessage());
            failureCount++;
        }
    }
    if (failures.length() > 0) {
        throw new AssertionFailedError(failureCount + " IRR evaluations failed:\n" + failures);
    }
}
Also used : CellValue(org.apache.poi.ss.usermodel.CellValue) AssertionFailedError(junit.framework.AssertionFailedError)

Example 18 with CellValue

use of org.apache.poi.ss.usermodel.CellValue in project poi by apache.

the class TestExternalFunctionFormulas method confirmCellEval.

private static void confirmCellEval(HSSFSheet sheet, int rowIx, int colIx, HSSFFormulaEvaluator fe, String expectedFormula, double expectedResult) {
    HSSFCell cell = sheet.getRow(rowIx).getCell(colIx);
    assertEquals(expectedFormula, cell.getCellFormula());
    CellValue cv = fe.evaluate(cell);
    assertEquals(expectedResult, cv.getNumberValue(), 0.0);
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) CellValue(org.apache.poi.ss.usermodel.CellValue)

Example 19 with CellValue

use of org.apache.poi.ss.usermodel.CellValue 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 20 with CellValue

use of org.apache.poi.ss.usermodel.CellValue in project poi by apache.

the class TestWorkbookEvaluator method testResultOutsideRange.

/**
     * Functions like IF, INDIRECT, INDEX, OFFSET etc can return AreaEvals which
     * should be dereferenced by the evaluator
     */
@Test
public void testResultOutsideRange() throws IOException {
    Workbook wb = new HSSFWorkbook();
    try {
        Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
        // IF(TRUE,D2:D5,D2) or  OFFSET(D2:D5,0,0) would work too
        cell.setCellFormula("D2:D5");
        FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
        CellValue cv;
        try {
            cv = fe.evaluate(cell);
        } catch (IllegalArgumentException e) {
            if ("Specified row index (0) is outside the allowed range (1..4)".equals(e.getMessage())) {
                fail("Identified bug in result dereferencing");
            }
            throw new RuntimeException(e);
        }
        assertEquals(CellType.ERROR, cv.getCellTypeEnum());
        assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), cv.getErrorValue());
        // verify circular refs are still detected properly
        fe.clearAllCachedResultValues();
        cell.setCellFormula("OFFSET(A1,0,0)");
        cv = fe.evaluate(cell);
        assertEquals(CellType.ERROR, cv.getCellTypeEnum());
        assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cv.getErrorValue());
    } finally {
        wb.close();
    }
}
Also used : CellValue(org.apache.poi.ss.usermodel.CellValue) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) Test(org.junit.Test)

Aggregations

CellValue (org.apache.poi.ss.usermodel.CellValue)63 Test (org.junit.Test)24 Cell (org.apache.poi.ss.usermodel.Cell)18 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)15 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 AssertionFailedError (junit.framework.AssertionFailedError)12 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)11 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)10 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)10 Row (org.apache.poi.ss.usermodel.Row)10 Workbook (org.apache.poi.ss.usermodel.Workbook)8 Sheet (org.apache.poi.ss.usermodel.Sheet)7 CellType (org.apache.poi.ss.usermodel.CellType)4 CellReference (org.apache.poi.ss.util.CellReference)4 CellReference (org.apache.poi.hssf.util.CellReference)2 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)2 FreeRefFunction (org.apache.poi.ss.formula.functions.FreeRefFunction)2 File (java.io.File)1 EmptyStackException (java.util.EmptyStackException)1