Search in sources :

Example 31 with FormulaEvaluator

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

the class TestBugs method assertFormula.

private void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) {
    assertEquals(CellType.FORMULA, intF.getCellTypeEnum());
    if (null == expectedResultOrNull) {
        assertEquals(CellType.ERROR, intF.getCachedFormulaResultTypeEnum());
        expectedResultOrNull = "#VALUE!";
    } else {
        assertEquals(CellType.NUMERIC, intF.getCachedFormulaResultTypeEnum());
    }
    assertEquals(expectedFormula, intF.getCellFormula());
    // Check we can evaluate it correctly
    FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
    assertEquals(expectedResultOrNull, eval.evaluate(intF).formatAsString());
}
Also used : FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator)

Example 32 with FormulaEvaluator

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

the class TestBugs method test48043.

@Test
public void test48043() throws IOException {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("56325a.xls");
    wb.removeSheetAt(2);
    wb.removeSheetAt(1);
    //Sheet s = wb.createSheet("sheetname");
    Sheet s = wb.getSheetAt(0);
    Row row = s.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellFormula("IF(AND(ISBLANK(A10)," + "ISBLANK(B10)),\"\"," + "CONCATENATE(A10,\"-\",B10))");
    FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
    eval.evaluateAll();
    /*OutputStream out = new FileOutputStream("C:\\temp\\48043.xls");
        try {
          wb.write(out);
        } finally {
          out.close();
        }*/
    Workbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
    assertNotNull(wbBack);
    wbBack.close();
    wb.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) Test(org.junit.Test)

Example 33 with FormulaEvaluator

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

the class BaseFormulaEvaluator method evaluateAllFormulaCells.

/**
     * Loops over all cells in all sheets of the supplied
     *  workbook.
     * For cells that contain formulas, their formulas are
     *  evaluated, and the results are saved. These cells
     *  remain as formula cells.
     * For cells that do not contain formulas, no changes
     *  are made.
     * This is a helpful wrapper around looping over all
     *  cells, and calling evaluateFormulaCell on each one.
     */
public static void evaluateAllFormulaCells(Workbook wb) {
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    evaluateAllFormulaCells(wb, evaluator);
}
Also used : FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator)

Example 34 with FormulaEvaluator

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

the class CollaboratingWorkbooksEnvironment method setupFormulaEvaluator.

public static void setupFormulaEvaluator(Map<String, FormulaEvaluator> evaluators) {
    Map<String, WorkbookEvaluator> evaluatorsByName = new HashMap<String, WorkbookEvaluator>(evaluators.size());
    for (Map.Entry<String, FormulaEvaluator> swb : evaluators.entrySet()) {
        String wbName = swb.getKey();
        FormulaEvaluator eval = swb.getValue();
        if (eval instanceof WorkbookEvaluatorProvider) {
            evaluatorsByName.put(wbName, ((WorkbookEvaluatorProvider) eval)._getWorkbookEvaluator());
        } else {
            throw new IllegalArgumentException("Formula Evaluator " + eval + " provides no WorkbookEvaluator access");
        }
    }
    setup(evaluatorsByName);
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) HashMap(java.util.HashMap) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator)

Example 35 with FormulaEvaluator

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

the class TestXSSFFormulaParser method test58648FormulaParsing.

@Test
public void test58648FormulaParsing() throws IOException {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("58648.xlsx");
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet xsheet = wb.getSheetAt(i);
        for (Row row : xsheet) {
            for (Cell cell : row) {
                if (cell.getCellTypeEnum() == CellType.FORMULA) {
                    try {
                        evaluator.evaluateFormulaCellEnum(cell);
                    } catch (Exception e) {
                        CellReference cellRef = new CellReference(cell.getRowIndex(), cell.getColumnIndex());
                        throw new RuntimeException("error at: " + cellRef, e);
                    }
                }
            }
        }
    }
    Sheet sheet = wb.getSheet("my-sheet");
    Cell cell = sheet.getRow(1).getCell(4);
    assertEquals(5d, cell.getNumericCellValue(), 0d);
    wb.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) CellReference(org.apache.poi.ss.util.CellReference) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) HSSFEvaluationWorkbook(org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook) FormulaParsingWorkbook(org.apache.poi.ss.formula.FormulaParsingWorkbook) FormulaRenderingWorkbook(org.apache.poi.ss.formula.FormulaRenderingWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) IOException(java.io.IOException) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) Test(org.junit.Test)

Aggregations

FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)56 Cell (org.apache.poi.ss.usermodel.Cell)40 Test (org.junit.Test)34 Workbook (org.apache.poi.ss.usermodel.Workbook)30 Sheet (org.apache.poi.ss.usermodel.Sheet)28 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)20 Row (org.apache.poi.ss.usermodel.Row)14 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)12 CellValue (org.apache.poi.ss.usermodel.CellValue)11 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)8 CellReference (org.apache.poi.ss.util.CellReference)7 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)6 BaseTestFormulaEvaluator (org.apache.poi.ss.usermodel.BaseTestFormulaEvaluator)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)3 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)3 DecimalFormat (java.text.DecimalFormat)2 ArrayList (java.util.ArrayList)2 InternalSheet (org.apache.poi.hssf.model.InternalSheet)2 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)2