/**
   * When evaluating defined names, POI has to decide whether it is capable. Currently (May2009) POI
   * only supports simple cell and area refs.<br>
   * The sample spreadsheet (bugzilla attachment 23508) had a name flagged as 'complex' which
   * contained a simple area ref. It is not clear what the 'complex' flag is used for but POI should
   * look elsewhere to decide whether it can evaluate the name.
   */
  public void testDefinedNameWithComplexFlag_bug47048() {
    // Mock up a spreadsheet to match the critical details of the sample
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Input");
    HSSFName definedName = wb.createName();
    definedName.setNameName("Is_Multicar_Vehicle");
    definedName.setRefersToFormula("Input!$B$17:$G$17");

    // Set up some data and the formula
    HSSFRow row17 = sheet.createRow(16);
    row17.createCell(0).setCellValue(25.0);
    row17.createCell(1).setCellValue(1.33);
    row17.createCell(2).setCellValue(4.0);

    HSSFRow row = sheet.createRow(0);
    HSSFCell cellA1 = row.createCell(0);
    cellA1.setCellFormula("SUM(Is_Multicar_Vehicle)");

    // Set the complex flag - POI doesn't usually manipulate this flag
    NameRecord nameRec = TestHSSFName.getNameRecord(definedName);
    nameRec.setOptionFlag((short) 0x10); // 0x10 -> complex

    HSSFFormulaEvaluator hsf = new HSSFFormulaEvaluator(wb);
    CellValue value;
    try {
      value = hsf.evaluate(cellA1);
    } catch (RuntimeException e) {
      if (e.getMessage().equals("Don't now how to evalate name 'Is_Multicar_Vehicle'")) {
        throw new AssertionFailedError("Identified bug 47048a");
      }
      throw e;
    }

    assertEquals(Cell.CELL_TYPE_NUMERIC, value.getCellType());
    assertEquals(5.33, value.getNumberValue(), 0.0);
  }
Beispiel #2
0
  /** Test creation / evaluation of formulas with sheet-level names */
  public void testSheetLevelFormulas() {
    HSSFWorkbook wb = new HSSFWorkbook();

    HSSFRow row;
    HSSFSheet sh1 = wb.createSheet("Sheet1");
    HSSFName nm1 = wb.createName();
    nm1.setNameName("sales_1");
    nm1.setSheetIndex(0);
    nm1.setRefersToFormula("Sheet1!$A$1");
    row = sh1.createRow(0);
    row.createCell(0).setCellValue(3);
    row.createCell(1).setCellFormula("sales_1");
    row.createCell(2).setCellFormula("sales_1*2");

    HSSFSheet sh2 = wb.createSheet("Sheet2");
    HSSFName nm2 = wb.createName();
    nm2.setNameName("sales_1");
    nm2.setSheetIndex(1);
    nm2.setRefersToFormula("Sheet2!$A$1");

    row = sh2.createRow(0);
    row.createCell(0).setCellValue(5);
    row.createCell(1).setCellFormula("sales_1");
    row.createCell(2).setCellFormula("sales_1*3");

    // check that NamePtg refers to the correct NameRecord
    Ptg[] ptgs1 = HSSFFormulaParser.parse("sales_1", wb, FormulaType.CELL, 0);
    NamePtg nPtg1 = (NamePtg) ptgs1[0];
    assertSame(nm1, wb.getNameAt(nPtg1.getIndex()));

    Ptg[] ptgs2 = HSSFFormulaParser.parse("sales_1", wb, FormulaType.CELL, 1);
    NamePtg nPtg2 = (NamePtg) ptgs2[0];
    assertSame(nm2, wb.getNameAt(nPtg2.getIndex()));

    // check that the formula evaluator returns the correct result
    HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
    assertEquals(3.0, evaluator.evaluate(sh1.getRow(0).getCell(1)).getNumberValue(), 0.0);
    assertEquals(6.0, evaluator.evaluate(sh1.getRow(0).getCell(2)).getNumberValue(), 0.0);

    assertEquals(5.0, evaluator.evaluate(sh2.getRow(0).getCell(1)).getNumberValue(), 0.0);
    assertEquals(15.0, evaluator.evaluate(sh2.getRow(0).getCell(2)).getNumberValue(), 0.0);
  }