Search in sources :

Example 21 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project poi by apache.

the class TestFormulaParser method testRangeOperator.

@Test
public void testRangeOperator() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    HSSFCell cell = sheet.createRow(0).createCell(0);
    wb.setSheetName(0, "Sheet1");
    // explicit range ':' operator
    cell.setCellFormula("Sheet1!B$4:Sheet1!$C1");
    assertEquals("Sheet1!B$4:Sheet1!$C1", cell.getCellFormula());
    // plain area ref
    cell.setCellFormula("Sheet1!B$4:$C1");
    // note - area ref is normalised
    assertEquals("Sheet1!B1:$C$4", cell.getCellFormula());
    // different syntax for plain area ref
    cell.setCellFormula("Sheet1!$C1...B$4");
    assertEquals("Sheet1!B1:$C$4", cell.getCellFormula());
    // with funny sheet name
    wb.setSheetName(0, "A1...A2");
    cell.setCellFormula("A1...A2!B1");
    assertEquals("A1...A2!B1", cell.getCellFormula());
    wb.close();
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 22 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project poi by apache.

the class TestFormulaParser method testNamesWithUnderscore.

/** bug 49725, defined names with underscore */
@Test
public void testNamesWithUnderscore() throws IOException {
    //or new XSSFWorkbook();
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("NamesWithUnderscore");
    HSSFName nm;
    nm = wb.createName();
    nm.setNameName("DA6_LEO_WBS_Number");
    nm.setRefersToFormula("33");
    nm = wb.createName();
    nm.setNameName("DA6_LEO_WBS_Name");
    nm.setRefersToFormula("33");
    nm = wb.createName();
    nm.setNameName("A1_");
    nm.setRefersToFormula("22");
    nm = wb.createName();
    nm.setNameName("_A1");
    nm.setRefersToFormula("11");
    nm = wb.createName();
    nm.setNameName("A_1");
    nm.setRefersToFormula("44");
    nm = wb.createName();
    nm.setNameName("A_1_");
    nm.setRefersToFormula("44");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellFormula("DA6_LEO_WBS_Number*2");
    assertEquals("DA6_LEO_WBS_Number*2", cell.getCellFormula());
    cell.setCellFormula("(A1_*_A1+A_1)/A_1_");
    assertEquals("(A1_*_A1+A_1)/A_1_", cell.getCellFormula());
    cell.setCellFormula("INDEX(DA6_LEO_WBS_Name,MATCH($A3,DA6_LEO_WBS_Number,0))");
    assertEquals("INDEX(DA6_LEO_WBS_Name,MATCH($A3,DA6_LEO_WBS_Number,0))", cell.getCellFormula());
    wb.close();
}
Also used : TestHSSFName(org.apache.poi.hssf.usermodel.TestHSSFName) HSSFName(org.apache.poi.hssf.usermodel.HSSFName) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 23 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project poi by apache.

the class TestFormulaParser method testSetFormulaWithRowBeyond32768_Bug44539.

@Test
public void testSetFormulaWithRowBeyond32768_Bug44539() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    wb.setSheetName(0, "Sheet1");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellFormula("SUM(A32769:A32770)");
    if ("SUM(A-32767:A-32766)".equals(cell.getCellFormula())) {
        fail("Identified bug 44539");
    }
    assertEquals("SUM(A32769:A32770)", cell.getCellFormula());
    wb.close();
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 24 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project poi by apache.

the class TestFormulaParserEval method testEvaluateFormulaWithRowBeyond32768_Bug44539.

public void testEvaluateFormulaWithRowBeyond32768_Bug44539() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    wb.setSheetName(0, "Sheet1");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellFormula("SUM(A32769:A32770)");
    // put some values in the cells to make the evaluation more interesting
    sheet.createRow(32768).createCell(0).setCellValue(31);
    sheet.createRow(32769).createCell(0).setCellValue(11);
    HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
    CellValue result;
    try {
        result = fe.evaluate(cell);
    } catch (FormulaParseException e) {
        if (!e.getMessage().equals("Found reference to named range \"A\", but that named range wasn't defined!")) {
            throw new AssertionFailedError("Identifed bug 44539");
        }
        throw e;
    }
    assertEquals(CellType.NUMERIC, result.getCellTypeEnum());
    assertEquals(42.0, result.getNumberValue(), 0.0);
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) 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)

Example 25 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet in project poi by apache.

the class TestFormulaParser method testExponentialInSheet.

@Test
public void testExponentialInSheet() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    wb.createSheet("Cash_Flow");
    HSSFSheet sheet = wb.createSheet("Test");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    String formula;
    cell.setCellFormula("1.3E21/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "1.3E+21/3", formula);
    cell.setCellFormula("-1.3E21/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "-1.3E+21/3", formula);
    cell.setCellFormula("1322E21/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "1.322E+24/3", formula);
    cell.setCellFormula("-1322E21/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "-1.322E+24/3", formula);
    cell.setCellFormula("1.3E1/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "13/3", formula);
    cell.setCellFormula("-1.3E1/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "-13/3", formula);
    cell.setCellFormula("1.3E-4/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "0.00013/3", formula);
    cell.setCellFormula("-1.3E-4/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "-0.00013/3", formula);
    cell.setCellFormula("13E-15/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "0.000000000000013/3", formula);
    cell.setCellFormula("-13E-15/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "-0.000000000000013/3", formula);
    cell.setCellFormula("1.3E3/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "1300/3", formula);
    cell.setCellFormula("-1.3E3/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "-1300/3", formula);
    cell.setCellFormula("1300000000000000/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "1300000000000000/3", formula);
    cell.setCellFormula("-1300000000000000/3");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "-1300000000000000/3", formula);
    cell.setCellFormula("-10E-1/3.1E2*4E3/3E4");
    formula = cell.getCellFormula();
    assertEquals("Exponential formula string", "-1/310*4000/30000", formula);
    wb.close();
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Aggregations

HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)158 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)139 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)70 Test (org.junit.Test)65 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)58 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)27 FileOutputStream (java.io.FileOutputStream)23 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)20 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)16 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)14 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)13 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)12 ArrayList (java.util.ArrayList)11 AssertionFailedError (junit.framework.AssertionFailedError)11 CellValue (org.apache.poi.ss.usermodel.CellValue)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 HashMap (java.util.HashMap)8 RecordBase (org.apache.poi.hssf.record.RecordBase)8