Search in sources :

Example 1 with CTCell

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

the class XSSFRow method createCell.

/**
     * Use this to create new cells within the row and return it.
     *
     * @param columnIndex - the column number this cell represents
     * @param type - the cell's data type
     * @return XSSFCell a high level representation of the created cell.
     * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0
     *   or greater than 16384, the maximum number of columns supported by the SpreadsheetML format (.xlsx)
     */
@Override
public XSSFCell createCell(int columnIndex, CellType type) {
    // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory
    // NOSONAR
    final Integer colI = new Integer(columnIndex);
    CTCell ctCell;
    XSSFCell prev = _cells.get(colI);
    if (prev != null) {
        ctCell = prev.getCTCell();
        ctCell.set(CTCell.Factory.newInstance());
    } else {
        ctCell = _row.addNewC();
    }
    XSSFCell xcell = new XSSFCell(this, ctCell);
    xcell.setCellNum(columnIndex);
    if (type != CellType.BLANK) {
        xcell.setCellType(type);
    }
    _cells.put(colI, xcell);
    return xcell;
}
Also used : CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)

Example 2 with CTCell

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

the class XSSFRow method shift.

/**
     * update cell references when shifting rows
     *
     * @param n the number of rows to move
     */
protected void shift(int n) {
    int rownum = getRowNum() + n;
    CalculationChain calcChain = _sheet.getWorkbook().getCalculationChain();
    int sheetId = (int) _sheet.sheet.getSheetId();
    String msg = "Row[rownum=" + getRowNum() + "] contains cell(s) included in a multi-cell array formula. " + "You cannot change part of an array.";
    for (Cell c : this) {
        XSSFCell cell = (XSSFCell) c;
        if (cell.isPartOfArrayFormulaGroup()) {
            cell.notifyArrayFormulaChanging(msg);
        }
        //remove the reference in the calculation chain
        if (calcChain != null)
            calcChain.removeItem(sheetId, cell.getReference());
        CTCell ctCell = cell.getCTCell();
        String r = new CellReference(rownum, cell.getColumnIndex()).formatAsString();
        ctCell.setR(r);
    }
    setRowNum(rownum);
}
Also used : CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) CalculationChain(org.apache.poi.xssf.model.CalculationChain) CellReference(org.apache.poi.ss.util.CellReference) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Cell(org.apache.poi.ss.usermodel.Cell)

Example 3 with CTCell

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

the class TestXSSFSheet method createRow.

/**
     * Rows and cells can be created in random order,
     * but CTRows are kept in ascending order
     */
@Override
@Test
public void createRow() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet();
    CTWorksheet wsh = sheet.getCTWorksheet();
    CTSheetData sheetData = wsh.getSheetData();
    assertEquals(0, sheetData.sizeOfRowArray());
    XSSFRow row1 = sheet.createRow(2);
    row1.createCell(2);
    row1.createCell(1);
    XSSFRow row2 = sheet.createRow(1);
    row2.createCell(2);
    row2.createCell(1);
    row2.createCell(0);
    XSSFRow row3 = sheet.createRow(0);
    row3.createCell(3);
    row3.createCell(0);
    row3.createCell(2);
    row3.createCell(5);
    CTRow[] xrow = sheetData.getRowArray();
    assertEquals(3, xrow.length);
    //rows are sorted: {0, 1, 2}
    assertEquals(4, xrow[0].sizeOfCArray());
    assertEquals(1, xrow[0].getR());
    assertTrue(xrow[0].equals(row3.getCTRow()));
    assertEquals(3, xrow[1].sizeOfCArray());
    assertEquals(2, xrow[1].getR());
    assertTrue(xrow[1].equals(row2.getCTRow()));
    assertEquals(2, xrow[2].sizeOfCArray());
    assertEquals(3, xrow[2].getR());
    assertTrue(xrow[2].equals(row1.getCTRow()));
    CTCell[] xcell = xrow[0].getCArray();
    assertEquals("D1", xcell[0].getR());
    assertEquals("A1", xcell[1].getR());
    assertEquals("C1", xcell[2].getR());
    assertEquals("F1", xcell[3].getR());
    //re-creating a row does NOT add extra data to the parent
    row2 = sheet.createRow(1);
    assertEquals(3, sheetData.sizeOfRowArray());
    //existing cells are invalidated
    assertEquals(0, sheetData.getRowArray(1).sizeOfCArray());
    assertEquals(0, row2.getPhysicalNumberOfCells());
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    wsh = sheet.getCTWorksheet();
    xrow = sheetData.getRowArray();
    assertEquals(3, xrow.length);
    //rows are sorted: {0, 1, 2}
    assertEquals(4, xrow[0].sizeOfCArray());
    assertEquals(1, xrow[0].getR());
    //cells are now sorted
    xcell = xrow[0].getCArray();
    assertEquals("A1", xcell[0].getR());
    assertEquals("C1", xcell[1].getR());
    assertEquals("D1", xcell[2].getR());
    assertEquals("F1", xcell[3].getR());
    assertEquals(0, xrow[1].sizeOfCArray());
    assertEquals(2, xrow[1].getR());
    assertEquals(2, xrow[2].sizeOfCArray());
    assertEquals(3, xrow[2].getR());
    wb2.close();
}
Also used : CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) CTSheetData(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CTRow(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow) Test(org.junit.Test)

Example 4 with CTCell

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

the class XSSFRowShifter method updateRowFormulas.

/**
     * Update the formulas in specified row using the formula shifting policy specified by shifter
     *
     * @param row the row to update the formulas on
     * @param shifter the formula shifting policy
     */
@Internal
public void updateRowFormulas(Row row, FormulaShifter shifter) {
    XSSFSheet sheet = (XSSFSheet) row.getSheet();
    for (Cell c : row) {
        XSSFCell cell = (XSSFCell) c;
        CTCell ctCell = cell.getCTCell();
        if (ctCell.isSetF()) {
            CTCellFormula f = ctCell.getF();
            String formula = f.getStringValue();
            if (formula.length() > 0) {
                String shiftedFormula = shiftFormula(row, formula, shifter);
                if (shiftedFormula != null) {
                    f.setStringValue(shiftedFormula);
                    if (f.getT() == STCellFormulaType.SHARED) {
                        int si = (int) f.getSi();
                        CTCellFormula sf = sheet.getSharedFormula(si);
                        sf.setStringValue(shiftedFormula);
                        updateRefInCTCellFormula(row, shifter, sf);
                    }
                }
            }
            //Range of cells which the formula applies to.
            updateRefInCTCellFormula(row, shifter, f);
        }
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CTCellFormula(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Cell(org.apache.poi.ss.usermodel.Cell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Internal(org.apache.poi.util.Internal)

Example 5 with CTCell

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

the class TestXSSFSheetUpdateArrayFormulas method confirmArrayFormulaCell.

private static void confirmArrayFormulaCell(XSSFCell c, String cellRef, String formulaText, String arrayRangeRef) {
    if (c == null) {
        throw new AssertionFailedError("Cell should not be null.");
    }
    CTCell ctCell = c.getCTCell();
    assertEquals(cellRef, ctCell.getR());
    if (formulaText == null) {
        assertFalse(ctCell.isSetF());
        assertNull(ctCell.getF());
    } else {
        CTCellFormula f = ctCell.getF();
        assertEquals(arrayRangeRef, f.getRef());
        assertEquals(formulaText, f.getStringValue());
        assertEquals(STCellFormulaType.ARRAY, f.getT());
    }
}
Also used : CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) AssertionFailedError(junit.framework.AssertionFailedError) CTCellFormula(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)

Aggregations

CTCell (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)8 Cell (org.apache.poi.ss.usermodel.Cell)2 Test (org.junit.Test)2 CTCellFormula (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)2 AssertionFailedError (junit.framework.AssertionFailedError)1 CellReference (org.apache.poi.ss.util.CellReference)1 Internal (org.apache.poi.util.Internal)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 CTRow (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow)1 CTSheetData (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData)1 CTWorksheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet)1