Search in sources :

Example 6 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)

Example 7 with CTCellFormula

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

the class XSSFCell method setFormula.

private void setFormula(String formula, FormulaType formulaType) {
    XSSFWorkbook wb = _row.getSheet().getWorkbook();
    if (formula == null) {
        wb.onDeleteFormula(this);
        if (_cell.isSetF()) {
            _cell.unsetF();
        }
        return;
    }
    XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
    //validate through the FormulaParser
    FormulaParser.parse(formula, fpb, formulaType, wb.getSheetIndex(getSheet()), getRowIndex());
    CTCellFormula f = CTCellFormula.Factory.newInstance();
    f.setStringValue(formula);
    _cell.setF(f);
    if (_cell.isSetV()) {
        _cell.unsetV();
    }
}
Also used : CTCellFormula(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula)

Example 8 with CTCellFormula

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

the class XSSFCell method getCellFormula.

/**
     * package/hierarchy use only - reuse an existing evaluation workbook if available for caching
     *
     * @param fpb evaluation workbook for reuse, if available, or null to create a new one as needed
     * @return a formula for the cell
     * @throws IllegalStateException if the cell type returned by {@link #getCellTypeEnum()} is not {@link CellType#FORMULA}
     */
protected String getCellFormula(XSSFEvaluationWorkbook fpb) {
    CellType cellType = getCellTypeEnum();
    if (cellType != CellType.FORMULA) {
        throw typeMismatch(CellType.FORMULA, cellType, false);
    }
    CTCellFormula f = _cell.getF();
    if (isPartOfArrayFormulaGroup() && f == null) {
        XSSFCell cell = getSheet().getFirstCellInArrayFormula(this);
        return cell.getCellFormula(fpb);
    }
    if (f.getT() == STCellFormulaType.SHARED) {
        return convertSharedFormula((int) f.getSi(), fpb == null ? XSSFEvaluationWorkbook.create(getSheet().getWorkbook()) : fpb);
    }
    return f.getStringValue();
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) STCellType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType) 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