Search in sources :

Example 1 with FormulaRecord

use of org.apache.poi.hssf.record.FormulaRecord 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 2 with FormulaRecord

use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.

the class TestValueRecordsAggregate method testSharedFormula.

/**
	 * Make sure the shared formula DOESNT makes it to the FormulaRecordAggregate when being parsed
	 * as part of the value records
	 */
@Test
public void testSharedFormula() {
    List<Record> records = new ArrayList<Record>();
    records.add(new FormulaRecord());
    records.add(new SharedFormulaRecord());
    records.add(new WindowTwoRecord());
    constructValueRecord(records);
    List<CellValueRecordInterface> cvrs = getValueRecords();
    //Ensure that the SharedFormulaRecord has been converted
    assertEquals(1, cvrs.size());
    CellValueRecordInterface record = cvrs.get(0);
    assertNotNull("Row contains a value", record);
    assertTrue("First record is a FormulaRecordsAggregate", (record instanceof FormulaRecordAggregate));
}
Also used : SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) CellValueRecordInterface(org.apache.poi.hssf.record.CellValueRecordInterface) ArrayList(java.util.ArrayList) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) Record(org.apache.poi.hssf.record.Record) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) Test(org.junit.Test)

Example 3 with FormulaRecord

use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.

the class HSSFCell method getErrorCellValue.

/**
     * get the value of the cell as an error code.  For strings, numbers, and booleans, we throw an exception.
     * For blank cells we return a 0.
     */
@Override
public byte getErrorCellValue() {
    switch(_cellType) {
        case ERROR:
            return ((BoolErrRecord) _record).getErrorValue();
        case FORMULA:
            FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
            checkFormulaCachedValueType(CellType.ERROR, fr);
            return (byte) fr.getCachedErrorValue();
        default:
            throw typeMismatch(CellType.ERROR, _cellType, false);
    }
}
Also used : BoolErrRecord(org.apache.poi.hssf.record.BoolErrRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)

Example 4 with FormulaRecord

use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.

the class HSSFCell method setCellFormula.

public void setCellFormula(String formula) {
    if (isPartOfArrayFormulaGroup()) {
        notifyArrayFormulaChanging();
    }
    int row = _record.getRow();
    short col = _record.getColumn();
    short styleIndex = _record.getXFIndex();
    if (formula == null) {
        notifyFormulaChanging();
        setCellType(CellType.BLANK, false, row, col, styleIndex);
        return;
    }
    int sheetIndex = _book.getSheetIndex(_sheet);
    Ptg[] ptgs = HSSFFormulaParser.parse(formula, _book, FormulaType.CELL, sheetIndex);
    setCellType(CellType.FORMULA, false, row, col, styleIndex);
    FormulaRecordAggregate agg = (FormulaRecordAggregate) _record;
    FormulaRecord frec = agg.getFormulaRecord();
    frec.setOptions((short) 2);
    frec.setValue(0);
    //only set to default if there is no extended format index already set
    if (agg.getXFIndex() == (short) 0) {
        agg.setXFIndex((short) 0x0f);
    }
    agg.setParsedExpression(ptgs);
}
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) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)

Example 5 with FormulaRecord

use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.

the class HSSFCell method convertCellValueToString.

private String convertCellValueToString() {
    switch(_cellType) {
        case BLANK:
            return "";
        case BOOLEAN:
            return ((BoolErrRecord) _record).getBooleanValue() ? "TRUE" : "FALSE";
        case STRING:
            int sstIndex = ((LabelSSTRecord) _record).getSSTIndex();
            return _book.getWorkbook().getSSTString(sstIndex).getString();
        case NUMERIC:
            return NumberToTextConverter.toText(((NumberRecord) _record).getValue());
        case ERROR:
            return FormulaError.forInt(((BoolErrRecord) _record).getErrorValue()).getString();
        case FORMULA:
            // just use cached formula result instead
            break;
        default:
            throw new IllegalStateException("Unexpected cell type (" + _cellType + ")");
    }
    FormulaRecordAggregate fra = ((FormulaRecordAggregate) _record);
    FormulaRecord fr = fra.getFormulaRecord();
    switch(CellType.forInt(fr.getCachedResultType())) {
        case BOOLEAN:
            return fr.getCachedBooleanValue() ? "TRUE" : "FALSE";
        case STRING:
            return fra.getStringValue();
        case NUMERIC:
            return NumberToTextConverter.toText(fr.getValue());
        case ERROR:
            return FormulaError.forInt(fr.getCachedErrorValue()).getString();
        default:
            throw new IllegalStateException("Unexpected formula result type (" + _cellType + ")");
    }
}
Also used : BoolErrRecord(org.apache.poi.hssf.record.BoolErrRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord)

Aggregations

FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)24 NumberRecord (org.apache.poi.hssf.record.NumberRecord)9 Record (org.apache.poi.hssf.record.Record)8 FormulaRecordAggregate (org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)8 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)7 StringRecord (org.apache.poi.hssf.record.StringRecord)7 BlankRecord (org.apache.poi.hssf.record.BlankRecord)6 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)5 Test (org.junit.Test)5 SharedFormulaRecord (org.apache.poi.hssf.record.SharedFormulaRecord)4 ArrayList (java.util.ArrayList)3 BOFRecord (org.apache.poi.hssf.record.BOFRecord)3 LabelRecord (org.apache.poi.hssf.record.LabelRecord)3 MulBlankRecord (org.apache.poi.hssf.record.MulBlankRecord)3 NoteRecord (org.apache.poi.hssf.record.NoteRecord)3 RKRecord (org.apache.poi.hssf.record.RKRecord)3 RowRecord (org.apache.poi.hssf.record.RowRecord)3 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)3 Ptg (org.apache.poi.ss.formula.ptg.Ptg)3 AssertionFailedError (junit.framework.AssertionFailedError)2