Search in sources :

Example 1 with CTCalcCell

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell in project poi by apache.

the class TestCalculationChain method test46535.

public void test46535() {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("46535.xlsx");
    CalculationChain chain = wb.getCalculationChain();
    //the bean holding the reference to the formula to be deleted
    CTCalcCell c = chain.getCTCalcChain().getCArray(0);
    int cnt = chain.getCTCalcChain().sizeOfCArray();
    assertEquals(10, c.getI());
    assertEquals("E1", c.getR());
    XSSFSheet sheet = wb.getSheet("Test");
    XSSFCell cell = sheet.getRow(0).getCell(4);
    assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
    cell.setCellFormula(null);
    //the count of items is less by one
    c = chain.getCTCalcChain().getCArray(0);
    int cnt2 = chain.getCTCalcChain().sizeOfCArray();
    assertEquals(cnt - 1, cnt2);
    //the first item in the calculation chain is the former second one
    assertEquals(10, c.getI());
    assertEquals("C1", c.getR());
    assertEquals(CellType.STRING, cell.getCellTypeEnum());
    cell.setCellValue("ABC");
    assertEquals(CellType.STRING, cell.getCellTypeEnum());
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CTCalcCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell)

Example 2 with CTCalcCell

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell in project poi by apache.

the class TestXSSFBugs method runTest56574.

private void runTest56574(boolean createRow) throws IOException {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56574.xlsx");
    Sheet sheet = wb.getSheet("Func");
    assertNotNull(sheet);
    Map<String, Object[]> data;
    data = new TreeMap<String, Object[]>();
    data.put("1", new Object[] { "ID", "NAME", "LASTNAME" });
    data.put("2", new Object[] { 2, "Amit", "Shukla" });
    data.put("3", new Object[] { 1, "Lokesh", "Gupta" });
    data.put("4", new Object[] { 4, "John", "Adwards" });
    data.put("5", new Object[] { 2, "Brian", "Schultz" });
    int rownum = 1;
    for (Map.Entry<String, Object[]> me : data.entrySet()) {
        final Row row;
        if (createRow) {
            row = sheet.createRow(rownum++);
        } else {
            row = sheet.getRow(rownum++);
        }
        assertNotNull(row);
        int cellnum = 0;
        for (Object obj : me.getValue()) {
            Cell cell = row.getCell(cellnum);
            if (cell == null) {
                cell = row.createCell(cellnum);
            } else {
                if (cell.getCellTypeEnum() == CellType.FORMULA) {
                    cell.setCellFormula(null);
                    cell.getCellStyle().setDataFormat((short) 0);
                }
            }
            if (obj instanceof String) {
                cell.setCellValue((String) obj);
            } else if (obj instanceof Integer) {
                cell.setCellValue((Integer) obj);
            }
            cellnum++;
        }
    }
    XSSFFormulaEvaluator.evaluateAllFormulaCells((XSSFWorkbook) wb);
    wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
    CalculationChain chain = ((XSSFWorkbook) wb).getCalculationChain();
    for (CTCalcCell calc : chain.getCTCalcChain().getCList()) {
        // A2 to A6 should be gone
        assertFalse(calc.getR().equals("A2"));
        assertFalse(calc.getR().equals("A3"));
        assertFalse(calc.getR().equals("A4"));
        assertFalse(calc.getR().equals("A5"));
        assertFalse(calc.getR().equals("A6"));
    }
    Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
    Sheet sheetBack = wbBack.getSheet("Func");
    assertNotNull(sheetBack);
    chain = ((XSSFWorkbook) wbBack).getCalculationChain();
    for (CTCalcCell calc : chain.getCTCalcChain().getCList()) {
        // A2 to A6 should be gone
        assertFalse(calc.getR().equals("A2"));
        assertFalse(calc.getR().equals("A3"));
        assertFalse(calc.getR().equals("A4"));
        assertFalse(calc.getR().equals("A5"));
        assertFalse(calc.getR().equals("A6"));
    }
    wbBack.close();
    wb.close();
}
Also used : CalculationChain(org.apache.poi.xssf.model.CalculationChain) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) CTCalcCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell) CTCalcCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell)

Aggregations

CTCalcCell (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 CalculationChain (org.apache.poi.xssf.model.CalculationChain)1 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1