Search in sources :

Example 56 with CellValue

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

the class TestRangeEval method testRangeUsingOffsetFunc_bug46948.

public void testRangeUsingOffsetFunc_bug46948() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFRow row = wb.createSheet("Sheet1").createRow(0);
    HSSFCell cellA1 = row.createCell(0);
    HSSFCell cellB1 = row.createCell(1);
    // C1
    row.createCell(2).setCellValue(5.0);
    // D1
    row.createCell(3).setCellValue(7.0);
    // E1
    row.createCell(4).setCellValue(9.0);
    cellA1.setCellFormula("SUM(C1:OFFSET(C1,0,B1))");
    // range will be C1:D1
    cellB1.setCellValue(1.0);
    HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
    CellValue cv;
    try {
        cv = fe.evaluate(cellA1);
    } catch (IllegalArgumentException e) {
        if (e.getMessage().equals("Unexpected ref arg class (org.apache.poi.ss.formula.LazyAreaEval)")) {
            throw new AssertionFailedError("Identified bug 46948");
        }
        throw e;
    }
    assertEquals(12.0, cv.getNumberValue(), 0.0);
    // range will be C1:E1
    cellB1.setCellValue(2.0);
    fe.notifyUpdateCell(cellB1);
    cv = fe.evaluate(cellA1);
    assertEquals(21.0, cv.getNumberValue(), 0.0);
    // range will be C1:C1
    cellB1.setCellValue(0.0);
    fe.notifyUpdateCell(cellB1);
    cv = fe.evaluate(cellA1);
    assertEquals(5.0, cv.getNumberValue(), 0.0);
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) CellValue(org.apache.poi.ss.usermodel.CellValue) AssertionFailedError(junit.framework.AssertionFailedError) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 57 with CellValue

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

the class BaseTestCircularReferences method testIndexFormula.

/**
     * ASF Bugzilla Bug 44413
     * "INDEX() formula cannot contain its own location in the data array range"
     */
@Test
public void testIndexFormula() throws IOException {
    Workbook wb = _testDataProvider.createWorkbook();
    Sheet sheet = wb.createSheet("Sheet1");
    int colB = 1;
    sheet.createRow(0).createCell(colB).setCellValue(1);
    sheet.createRow(1).createCell(colB).setCellValue(2);
    sheet.createRow(2).createCell(colB).setCellValue(3);
    Row row4 = sheet.createRow(3);
    Cell testCell = row4.createCell(0);
    // This formula should evaluate to the contents of B2,
    testCell.setCellFormula("INDEX(A1:B4,2,2)");
    // However the range A1:B4 also includes the current cell A4.  If the other parameters
    // were 4 and 1, this would represent a circular reference.  Prior to v3.2 POI would
    // 'fully' evaluate ref arguments before invoking operators, which raised the possibility of
    // cycles / StackOverflowErrors.
    CellValue cellValue = evaluateWithCycles(wb, testCell);
    assertTrue(cellValue.getCellTypeEnum() == CellType.NUMERIC);
    assertEquals(2, cellValue.getNumberValue(), 0);
    wb.close();
}
Also used : CellValue(org.apache.poi.ss.usermodel.CellValue) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 58 with CellValue

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

the class BaseTestCircularReferences method testSimpleCircularReference.

/**
     * Cell A1 has formula "=A1"
     */
@Test
public void testSimpleCircularReference() throws IOException {
    Workbook wb = _testDataProvider.createWorkbook();
    Sheet sheet = wb.createSheet("Sheet1");
    Row row = sheet.createRow(0);
    Cell testCell = row.createCell(0);
    testCell.setCellFormula("A1");
    CellValue cellValue = evaluateWithCycles(wb, testCell);
    confirmCycleErrorCode(cellValue);
    wb.close();
}
Also used : CellValue(org.apache.poi.ss.usermodel.CellValue) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 59 with CellValue

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

the class TestMultiSheetEval method processFunctionRow.

/**
	 *
	 * @return a constant from the local Result class denoting whether there were any evaluation
	 * cases, and whether they all succeeded.
	 */
private int processFunctionRow(HSSFFormulaEvaluator evaluator, String targetFunctionName, String targetTestName, Row formulasRow, Cell expectedValueCell) {
    // so far
    int result = Result.NO_EVALUATIONS_FOUND;
    Cell c = formulasRow.getCell(SS.COLUMN_INDEX_ACTUAL_VALUE);
    if (c == null || c.getCellTypeEnum() != CellType.FORMULA) {
        return result;
    }
    CellValue actualValue = evaluator.evaluate(c);
    try {
        confirmExpectedResult("Function '" + targetFunctionName + "': Test: '" + targetTestName + "' Formula: " + c.getCellFormula() + " @ " + formulasRow.getRowNum() + ":" + SS.COLUMN_INDEX_ACTUAL_VALUE, expectedValueCell, actualValue);
        _evaluationSuccessCount++;
        if (result != Result.SOME_EVALUATIONS_FAILED) {
            result = Result.ALL_EVALUATIONS_SUCCEEDED;
        }
    } catch (AssertionFailedError e) {
        _evaluationFailureCount++;
        printShortStackTrace(System.err, e);
        result = Result.SOME_EVALUATIONS_FAILED;
    }
    return result;
}
Also used : CellValue(org.apache.poi.ss.usermodel.CellValue) AssertionFailedError(junit.framework.AssertionFailedError) Cell(org.apache.poi.ss.usermodel.Cell)

Example 60 with CellValue

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

the class TestPercentEval method testInSpreadSheet.

public void testInSpreadSheet() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellFormula("B1%");
    row.createCell(1).setCellValue(50.0);
    HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
    CellValue cv;
    try {
        cv = fe.evaluate(cell);
    } catch (RuntimeException e) {
        if (e.getCause() instanceof NullPointerException) {
            throw new AssertionFailedError("Identified bug 44608");
        }
        // else some other unexpected error
        throw e;
    }
    assertEquals(CellType.NUMERIC, cv.getCellTypeEnum());
    assertEquals(0.5, cv.getNumberValue(), 0.0);
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) CellValue(org.apache.poi.ss.usermodel.CellValue) AssertionFailedError(junit.framework.AssertionFailedError) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

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