Search in sources :

Example 1 with CTCellFormula

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

the class XSSFCell method convertSharedFormula.

/**
     * Creates a non shared formula from the shared formula counterpart
     *
     * @param si Shared Group Index
     * @return non shared formula created for the given shared formula and this cell
     */
private String convertSharedFormula(int si, XSSFEvaluationWorkbook fpb) {
    XSSFSheet sheet = getSheet();
    CTCellFormula f = sheet.getSharedFormula(si);
    if (f == null) {
        throw new IllegalStateException("Master cell of a shared formula with sid=" + si + " was not found");
    }
    String sharedFormula = f.getStringValue();
    //Range of cells which the shared formula applies to
    String sharedFormulaRange = f.getRef();
    CellRangeAddress ref = CellRangeAddress.valueOf(sharedFormulaRange);
    int sheetIndex = sheet.getWorkbook().getSheetIndex(sheet);
    SharedFormula sf = new SharedFormula(SpreadsheetVersion.EXCEL2007);
    Ptg[] ptgs = FormulaParser.parse(sharedFormula, fpb, FormulaType.CELL, sheetIndex, getRowIndex());
    Ptg[] fmla = sf.convertSharedFormulas(ptgs, getRowIndex() - ref.getFirstRow(), getColumnIndex() - ref.getFirstColumn());
    return FormulaRenderer.toFormulaString(fpb, fmla);
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) RichTextString(org.apache.poi.ss.usermodel.RichTextString) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) CTCellFormula(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula) SharedFormula(org.apache.poi.ss.formula.SharedFormula)

Example 2 with CTCellFormula

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

the class XSSFCell method setCellType.

/**
     * Set the cells type (numeric, formula or string)
     *
     * @throws IllegalArgumentException if the specified cell type is invalid
     */
@Override
public void setCellType(CellType cellType) {
    CellType prevType = getCellTypeEnum();
    if (isPartOfArrayFormulaGroup()) {
        notifyArrayFormulaChanging();
    }
    if (prevType == CellType.FORMULA && cellType != CellType.FORMULA) {
        getSheet().getWorkbook().onDeleteFormula(this);
    }
    switch(cellType) {
        case NUMERIC:
            _cell.setT(STCellType.N);
            break;
        case STRING:
            if (prevType != CellType.STRING) {
                String str = convertCellValueToString();
                XSSFRichTextString rt = new XSSFRichTextString(str);
                rt.setStylesTableReference(_stylesSource);
                int sRef = _sharedStringSource.addEntry(rt.getCTRst());
                _cell.setV(Integer.toString(sRef));
            }
            _cell.setT(STCellType.S);
            break;
        case FORMULA:
            if (!_cell.isSetF()) {
                CTCellFormula f = CTCellFormula.Factory.newInstance();
                f.setStringValue("0");
                _cell.setF(f);
                if (_cell.isSetT()) {
                    _cell.unsetT();
                }
            }
            break;
        case BLANK:
            setBlank();
            break;
        case BOOLEAN:
            String newVal = convertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING;
            _cell.setT(STCellType.B);
            _cell.setV(newVal);
            break;
        case ERROR:
            _cell.setT(STCellType.E);
            break;
        default:
            throw new IllegalArgumentException("Illegal cell type: " + cellType);
    }
    if (cellType != CellType.FORMULA && _cell.isSetF()) {
        _cell.unsetF();
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) STCellType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType) RichTextString(org.apache.poi.ss.usermodel.RichTextString) CTCellFormula(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)

Example 3 with CTCellFormula

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

the class XSSFCell method setCellArrayFormula.

/* package */
void setCellArrayFormula(String formula, CellRangeAddress range) {
    setFormula(formula, FormulaType.ARRAY);
    CTCellFormula cellFormula = _cell.getF();
    cellFormula.setT(STCellFormulaType.ARRAY);
    cellFormula.setRef(range.formatAsString());
}
Also used : CTCellFormula(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)

Example 4 with CTCellFormula

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula 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 CTCellFormula

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula 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

CTCellFormula (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)8 Ptg (org.apache.poi.ss.formula.ptg.Ptg)2 CellType (org.apache.poi.ss.usermodel.CellType)2 RichTextString (org.apache.poi.ss.usermodel.RichTextString)2 CTCell (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell)2 STCellType (org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType)2 AssertionFailedError (junit.framework.AssertionFailedError)1 SharedFormula (org.apache.poi.ss.formula.SharedFormula)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 Internal (org.apache.poi.util.Internal)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1