Package org.apache.poi.ss.usermodel

Examples of org.apache.poi.ss.usermodel.FormulaEvaluator


    }

    public void test49156() throws Exception {
        Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49156.xlsx");
        FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();

        Sheet sheet = wb.getSheetAt(0);
        for(Row row : sheet){
            for(Cell cell : row){
                if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
                    formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells
                }
            }
        }
    }
View Full Code Here


       Cell c = s.getRow(0).getCell(0);
      
       assertEquals("SUM(\n1,2\n)", c.getCellFormula());
       assertEquals(3.0, c.getNumericCellValue());
      
       FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
       formulaEvaluator.evaluateFormulaCell(c);
      
       assertEquals("SUM(\n1,2\n)", c.getCellFormula());
       assertEquals(3.0, c.getNumericCellValue());

       // For 51875
       Cell b3 = s.getRow(2).getCell(1);
       formulaEvaluator.evaluateFormulaCell(b3);
       assertEquals("B1+B2", b3.getCellFormula()); // The newline is lost for shared formulas
       assertEquals(3.0, b3.getNumericCellValue());
    }
View Full Code Here

    /**
     * Bug 53101:
     */
    public void test5301(){
        Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("53101.xlsx");
        FormulaEvaluator evaluator =
                workbook.getCreationHelper().createFormulaEvaluator();
        // A1: SUM(B1: IZ1)
        double a1Value =
                evaluator.evaluate(workbook.getSheetAt(0).getRow(0).getCell(0)).getNumberValue();

        // Assert
        assertEquals(259.0, a1Value, 0.0);

        // KY: SUM(B1: IZ1)
        double ky1Value =
                evaluator.evaluate(workbook.getSheetAt(0).getRow(0).getCell(310)).getNumberValue();

        // Assert
        assertEquals(259.0, a1Value, 0.0);
    }
View Full Code Here

     * @param workbook the workbook that contains the cell
     * @param cell the cell that contains the formula
     * @return the object representation of formula cell value.
     */
    private Object evaluateCellFormula(final HSSFWorkbook workbook, final Cell cell) {
        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
        CellValue cellValue = evaluator.evaluate(cell);
        Object result = null;

        if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            result = cellValue.getBooleanValue();
        } else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
View Full Code Here

   * @param testFocusFunctionName name of a single function/operator to test alone.
   * Typically pass <code>null</code> to test all functions
   */
  private void processFunctionGroup(int startRowIndex, String testFocusFunctionName) {
    FormulaEvaluator evaluator = new FormulaEvaluator(sheet, workbook);

    int rowIndex = startRowIndex;
    while (true) {
      Row r = sheet.getRow(rowIndex);
      String targetFunctionName = getTargetFunctionName(r);
View Full Code Here

     
      Cell c2 = r.createCell(1);
      c2.setCellFormula("10/2");
      assertTrue( Double.isNaN(c2.getNumericCellValue()) );
     
      FormulaEvaluator fe = new FormulaEvaluator(s, wb);
      fe.setCurrentRow(r);
     
      fe.evaluateFormulaCell(c1);
      fe.evaluateFormulaCell(c2);
     
      assertEquals(6.0, c1.getNumericCellValue(), 0.0001);
      assertEquals(5.0, c2.getNumericCellValue(), 0.0001);
    }
View Full Code Here

      c4.setCellFormula("COUNTA(A1:E1)");
      assertTrue( Double.isNaN(c4.getNumericCellValue()) );


      // Evaluate and test
      FormulaEvaluator fe = new FormulaEvaluator(s, wb);
      fe.setCurrentRow(r);
     
      fe.evaluateFormulaCell(c1);
      fe.evaluateFormulaCell(c2);
      fe.evaluateFormulaCell(c3);
      fe.evaluateFormulaCell(c4);
     
      assertEquals(3.6, c1.getNumericCellValue(), 0.0001);
      assertEquals(17.5, c2.getNumericCellValue(), 0.0001);
      assertEquals(1, c3.getNumericCellValue(), 0.0001);
      assertEquals(4, c4.getNumericCellValue(), 0.0001);
View Full Code Here

   * @param testFocusFunctionName name of a single function/operator to test alone.
   * Typically pass <code>null</code> to test all functions
   */
  private void processFunctionGroup(int startRowIndex, String testFocusFunctionName) {
    FormulaEvaluator evaluator = new FormulaEvaluator(sheet, workbook);

    int rowIndex = startRowIndex;
    while (true) {
      Row r = sheet.getRow(rowIndex);
      String targetFunctionName = getTargetFunctionName(r);
View Full Code Here

   */
  public void testResultOutsideRange() {
    Workbook wb = new HSSFWorkbook();
    Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
    cell.setCellFormula("D2:D5"); // IF(TRUE,D2:D5,D2) or  OFFSET(D2:D5,0,0) would work too
    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())) {
        throw new AssertionFailedError("Identified bug in result dereferencing");
      }
      throw new RuntimeException(e);
    }
    assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType());
    assertEquals(ErrorConstants.ERROR_VALUE, cv.getErrorValue());

    // verify circular refs are still detected properly
    fe.clearAllCachedResultValues();
    cell.setCellFormula("OFFSET(A1,0,0)");
    cv = fe.evaluate(cell);
    assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType());
    assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cv.getErrorValue());
  }
View Full Code Here

   */
  public void testResultOutsideRange() {
    Workbook wb = new HSSFWorkbook();
    Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
    cell.setCellFormula("D2:D5"); // IF(TRUE,D2:D5,D2) or  OFFSET(D2:D5,0,0) would work too
    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())) {
        throw new AssertionFailedError("Identified bug in result dereferencing");
      }
      throw new RuntimeException(e);
    }
    assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType());
    assertEquals(ErrorConstants.ERROR_VALUE, cv.getErrorValue());

    // verify circular refs are still detected properly
    fe.clearAllCachedResultValues();
    cell.setCellFormula("OFFSET(A1,0,0)");
    cv = fe.evaluate(cell);
    assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType());
    assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cv.getErrorValue());
  }
View Full Code Here

TOP

Related Classes of org.apache.poi.ss.usermodel.FormulaEvaluator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.