Search in sources :

Example 1 with FormulaRecordAggregate

use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.

the class HSSFSheet method setArrayFormula.

@Override
public CellRange<HSSFCell> setArrayFormula(String formula, CellRangeAddress range) {
    // make sure the formula parses OK first
    int sheetIndex = _workbook.getSheetIndex(this);
    Ptg[] ptgs = HSSFFormulaParser.parse(formula, _workbook, FormulaType.ARRAY, sheetIndex);
    CellRange<HSSFCell> cells = getCellRange(range);
    for (HSSFCell c : cells) {
        c.setCellArrayFormula(range);
    }
    HSSFCell mainArrayFormulaCell = cells.getTopLeftCell();
    FormulaRecordAggregate agg = (FormulaRecordAggregate) mainArrayFormulaCell.getCellValueRecord();
    agg.setArrayFormula(range, ptgs);
    return cells;
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)

Example 2 with FormulaRecordAggregate

use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.

the class FormulaExtractor method getPtgs.

public static Ptg[] getPtgs(HSSFCell cell) {
    CellValueRecordInterface vr = cell.getCellValueRecord();
    if (!(vr instanceof FormulaRecordAggregate)) {
        throw new IllegalArgumentException("Not a formula cell");
    }
    FormulaRecordAggregate fra = (FormulaRecordAggregate) vr;
    return fra.getFormulaRecord().getParsedExpression();
}
Also used : CellValueRecordInterface(org.apache.poi.hssf.record.CellValueRecordInterface) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)

Example 3 with FormulaRecordAggregate

use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.

the class TestBug42464 method process.

private static void process(HSSFRow row, HSSFFormulaEvaluator eval) {
    Iterator<Cell> it = row.cellIterator();
    while (it.hasNext()) {
        HSSFCell cell = (HSSFCell) it.next();
        if (cell.getCellTypeEnum() != CellType.FORMULA) {
            continue;
        }
        FormulaRecordAggregate record = (FormulaRecordAggregate) cell.getCellValueRecord();
        FormulaRecord r = record.getFormulaRecord();
        Ptg[] ptgs = r.getParsedExpression();
        String cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex(), false, false).formatAsString();
        //			if(false && cellRef.equals("BP24")) { // TODO - replace System.out.println()s with asserts
        //				System.out.print(cellRef);
        //				System.out.println(" - has " + ptgs.length + " ptgs:");
        //				for(int i=0; i<ptgs.length; i++) {
        //					String c = ptgs[i].getClass().toString();
        //					System.out.println("\t" + c.substring(c.lastIndexOf('.')+1) );
        //				}
        //				System.out.println("-> " + cell.getCellFormula());
        //			}
        CellValue evalResult = eval.evaluate(cell);
        assertNotNull(evalResult);
    }
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate) CellValue(org.apache.poi.ss.usermodel.CellValue) CellReference(org.apache.poi.hssf.util.CellReference) Cell(org.apache.poi.ss.usermodel.Cell)

Example 4 with FormulaRecordAggregate

use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.

the class HSSFEvaluationWorkbook method getFormulaTokens.

@Override
public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
    HSSFCell cell = ((HSSFEvaluationCell) evalCell).getHSSFCell();
    // re-parsing the formula text also works, but is a waste of time
    // return HSSFFormulaParser.parse(cell.getCellFormula(), _uBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
    // It is useful within the tests to make sure that all formulas POI can evaluate can also be parsed.
    // see HSSFFileHandler.handleFile instead
    FormulaRecordAggregate fra = (FormulaRecordAggregate) cell.getCellValueRecord();
    return fra.getFormulaTokens();
}
Also used : FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)

Example 5 with FormulaRecordAggregate

use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.

the class HSSFCell method getRichStringCellValue.

/**
     * get the value of the cell as a string - for numeric cells we throw an exception.
     * For blank cells we return an empty string.
     * For formulaCells that are not string Formulas, we throw an exception
     */
public HSSFRichTextString getRichStringCellValue() {
    switch(_cellType) {
        case BLANK:
            return new HSSFRichTextString("");
        case STRING:
            return _stringValue;
        default:
            throw typeMismatch(CellType.STRING, _cellType, false);
        case FORMULA:
            break;
    }
    FormulaRecordAggregate fra = ((FormulaRecordAggregate) _record);
    checkFormulaCachedValueType(CellType.STRING, fra.getFormulaRecord());
    String strVal = fra.getStringValue();
    return new HSSFRichTextString(strVal == null ? "" : strVal);
}
Also used : FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate) RichTextString(org.apache.poi.ss.usermodel.RichTextString) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString)

Aggregations

FormulaRecordAggregate (org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)19 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)8 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)5 Ptg (org.apache.poi.ss.formula.ptg.Ptg)5 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)4 UnicodeString (org.apache.poi.hssf.record.common.UnicodeString)4 CellValueRecordInterface (org.apache.poi.hssf.record.CellValueRecordInterface)3 RichTextString (org.apache.poi.ss.usermodel.RichTextString)3 Test (org.junit.Test)3 NumberRecord (org.apache.poi.hssf.record.NumberRecord)2 ExpPtg (org.apache.poi.ss.formula.ptg.ExpPtg)2 Cell (org.apache.poi.ss.usermodel.Cell)2 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)2 BlankRecord (org.apache.poi.hssf.record.BlankRecord)1 CellReference (org.apache.poi.hssf.util.CellReference)1 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)1 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)1 FuncPtg (org.apache.poi.ss.formula.ptg.FuncPtg)1 FuncVarPtg (org.apache.poi.ss.formula.ptg.FuncVarPtg)1 MemFuncPtg (org.apache.poi.ss.formula.ptg.MemFuncPtg)1