Search in sources :

Example 1 with ExpPtg

use of org.apache.poi.ss.formula.ptg.ExpPtg in project poi by apache.

the class FormulaRecordAggregate method handleMissingSharedFormulaRecord.

/**
	 * Sometimes the shared formula flag "seems" to be erroneously set (because the corresponding
	 * {@link SharedFormulaRecord} does not exist). Normally this would leave no way of determining
	 * the {@link Ptg} tokens for the formula.  However as it turns out in these
	 * cases, Excel encodes the unshared {@link Ptg} tokens in the right place (inside the {@link
	 * FormulaRecord}).  So the the only thing that needs to be done is to ignore the erroneous
	 * shared formula flag.<br/>
	 *
	 * This method may also be used for setting breakpoints to help diagnose issues regarding the
	 * abnormally-set 'shared formula' flags.
	 * (see TestValueRecordsAggregate.testSpuriousSharedFormulaFlag()).<p/>
	 */
private static void handleMissingSharedFormulaRecord(FormulaRecord formula) {
    // make sure 'unshared' formula is actually available
    Ptg firstToken = formula.getParsedExpression()[0];
    if (firstToken instanceof ExpPtg) {
        throw new RecordFormatException("SharedFormulaRecord not found for FormulaRecord with (isSharedFormula=true)");
    }
    // could log an info message here since this is a fairly unusual occurrence.
    // no point leaving the flag erroneously set
    formula.setSharedFormula(false);
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) ExpPtg(org.apache.poi.ss.formula.ptg.ExpPtg) RecordFormatException(org.apache.poi.hssf.record.RecordFormatException) ExpPtg(org.apache.poi.ss.formula.ptg.ExpPtg)

Example 2 with ExpPtg

use of org.apache.poi.ss.formula.ptg.ExpPtg in project poi by apache.

the class FormulaViewer method listFormula.

private void listFormula(FormulaRecord record) {
    String sep = "~";
    Ptg[] tokens = record.getParsedExpression();
    Ptg token;
    int numptgs = tokens.length;
    String numArg;
    token = tokens[numptgs - 1];
    if (token instanceof FuncPtg) {
        numArg = String.valueOf(numptgs - 1);
    } else {
        numArg = String.valueOf(-1);
    }
    StringBuilder buf = new StringBuilder();
    if (token instanceof ExpPtg)
        return;
    buf.append(token.toFormulaString());
    buf.append(sep);
    switch(token.getPtgClass()) {
        case Ptg.CLASS_REF:
            buf.append("REF");
            break;
        case Ptg.CLASS_VALUE:
            buf.append("VALUE");
            break;
        case Ptg.CLASS_ARRAY:
            buf.append("ARRAY");
            break;
        default:
            throwInvalidRVAToken(token);
    }
    buf.append(sep);
    if (numptgs > 1) {
        token = tokens[numptgs - 2];
        switch(token.getPtgClass()) {
            case Ptg.CLASS_REF:
                buf.append("REF");
                break;
            case Ptg.CLASS_VALUE:
                buf.append("VALUE");
                break;
            case Ptg.CLASS_ARRAY:
                buf.append("ARRAY");
                break;
            default:
                throwInvalidRVAToken(token);
        }
    } else {
        buf.append("VALUE");
    }
    buf.append(sep);
    buf.append(numArg);
    System.out.println(buf);
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) ExpPtg(org.apache.poi.ss.formula.ptg.ExpPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) ExpPtg(org.apache.poi.ss.formula.ptg.ExpPtg)

Example 3 with ExpPtg

use of org.apache.poi.ss.formula.ptg.ExpPtg in project poi by apache.

the class TestFormulaRecordAggregate method testArrayFormulas.

public void testArrayFormulas() {
    int rownum = 4;
    int colnum = 4;
    FormulaRecord fr = new FormulaRecord();
    fr.setRow(rownum);
    fr.setColumn((short) colnum);
    FormulaRecordAggregate agg = new FormulaRecordAggregate(fr, null, SharedValueManager.createEmpty());
    Ptg[] ptgsForCell = { new ExpPtg(rownum, colnum) };
    agg.setParsedExpression(ptgsForCell);
    String formula = "SUM(A1:A3*B1:B3)";
    Ptg[] ptgs = HSSFFormulaParser.parse(formula, null, FormulaType.ARRAY, 0);
    agg.setArrayFormula(new CellRangeAddress(rownum, rownum, colnum, colnum), ptgs);
    assertTrue(agg.isPartOfArrayFormula());
    assertEquals("E5", agg.getArrayFormulaRange().formatAsString());
    Ptg[] ptg = agg.getFormulaTokens();
    String fmlaSer = FormulaRenderer.toFormulaString(null, ptg);
    assertEquals(formula, fmlaSer);
    agg.removeArrayFormula(rownum, colnum);
    assertFalse(agg.isPartOfArrayFormula());
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) ExpPtg(org.apache.poi.ss.formula.ptg.ExpPtg) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) ExpPtg(org.apache.poi.ss.formula.ptg.ExpPtg) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 4 with ExpPtg

use of org.apache.poi.ss.formula.ptg.ExpPtg in project poi by apache.

the class HSSFCell method setCellArrayFormula.

void setCellArrayFormula(CellRangeAddress range) {
    int row = _record.getRow();
    short col = _record.getColumn();
    short styleIndex = _record.getXFIndex();
    setCellType(CellType.FORMULA, false, row, col, styleIndex);
    // Billet for formula in rec
    Ptg[] ptgsForCell = { new ExpPtg(range.getFirstRow(), range.getFirstColumn()) };
    FormulaRecordAggregate agg = (FormulaRecordAggregate) _record;
    agg.setParsedExpression(ptgsForCell);
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) ExpPtg(org.apache.poi.ss.formula.ptg.ExpPtg) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate) ExpPtg(org.apache.poi.ss.formula.ptg.ExpPtg)

Aggregations

ExpPtg (org.apache.poi.ss.formula.ptg.ExpPtg)4 Ptg (org.apache.poi.ss.formula.ptg.Ptg)4 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)1 RecordFormatException (org.apache.poi.hssf.record.RecordFormatException)1 FormulaRecordAggregate (org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)1 FuncPtg (org.apache.poi.ss.formula.ptg.FuncPtg)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1