Search in sources :

Example 16 with FormulaRecord

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

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

the class TestFormulaRecordAggregate method testExtraStringRecord_bug46213.

/**
	 * Sometimes a {@link StringRecord} appears after a {@link FormulaRecord} even though the
	 * formula has evaluated to a text value.  This might be more likely to occur when the formula
	 * <i>can</i> evaluate to a text value.<br/>
	 * Bug 46213 attachment 22874 has such an extra {@link StringRecord} at stream offset 0x5765.
	 * This file seems to open in Excel (2007) with no trouble.  When it is re-saved, Excel omits
	 * the extra record.  POI should do the same.
	 */
public void testExtraStringRecord_bug46213() {
    FormulaRecord fr = new FormulaRecord();
    fr.setValue(2.0);
    StringRecord sr = new StringRecord();
    sr.setString("NA");
    SharedValueManager svm = SharedValueManager.createEmpty();
    FormulaRecordAggregate fra;
    try {
        fra = new FormulaRecordAggregate(fr, sr, svm);
    } catch (RecordFormatException e) {
        if ("String record was  supplied but formula record flag is not  set".equals(e.getMessage())) {
            throw new AssertionFailedError("Identified bug 46213");
        }
        throw e;
    }
    RecordCollector rc = new RecordCollector();
    fra.visitContainedRecords(rc);
    Record[] vraRecs = rc.getRecords();
    assertEquals(1, vraRecs.length);
    assertEquals(fr, vraRecs[0]);
}
Also used : FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) RecordFormatException(org.apache.poi.hssf.record.RecordFormatException) RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) Record(org.apache.poi.hssf.record.Record) StringRecord(org.apache.poi.hssf.record.StringRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) AssertionFailedError(junit.framework.AssertionFailedError) StringRecord(org.apache.poi.hssf.record.StringRecord)

Example 18 with FormulaRecord

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

the class HSSFCell method getBooleanCellValue.

/**
     * get the value of the cell as a boolean.  For strings, numbers, and errors, we throw an exception.
     * For blank cells we return a false.
     */
@Override
public boolean getBooleanCellValue() {
    switch(_cellType) {
        case BLANK:
            return false;
        case BOOLEAN:
            return ((BoolErrRecord) _record).getBooleanValue();
        case FORMULA:
            break;
        default:
            throw typeMismatch(CellType.BOOLEAN, _cellType, false);
    }
    FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
    checkFormulaCachedValueType(CellType.BOOLEAN, fr);
    return fr.getCachedBooleanValue();
}
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 19 with FormulaRecord

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

the class HSSFCell method getNumericCellValue.

/**
     * Get the value of the cell as a number.
     * For strings we throw an exception.
     * For blank cells we return a 0.
     * See {@link HSSFDataFormatter} for turning this
     *  number into a string similar to that which
     *  Excel would render this number as.
     */
public double getNumericCellValue() {
    switch(_cellType) {
        case BLANK:
            return 0.0;
        case NUMERIC:
            return ((NumberRecord) _record).getValue();
        default:
            throw typeMismatch(CellType.NUMERIC, _cellType, false);
        case FORMULA:
            break;
    }
    FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
    checkFormulaCachedValueType(CellType.NUMERIC, fr);
    return fr.getValue();
}
Also used : FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate) NumberRecord(org.apache.poi.hssf.record.NumberRecord)

Example 20 with FormulaRecord

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

the class HSSFCell method convertCellValueToBoolean.

/**
     * Chooses a new boolean value for the cell when its type is changing.<p/>
     *
     * Usually the caller is calling setCellType() with the intention of calling
     * setCellValue(boolean) straight afterwards.  This method only exists to give
     * the cell a somewhat reasonable value until the setCellValue() call (if at all).
     * TODO - perhaps a method like setCellTypeAndValue(int, Object) should be introduced to avoid this
     */
private boolean convertCellValueToBoolean() {
    switch(_cellType) {
        case BOOLEAN:
            return ((BoolErrRecord) _record).getBooleanValue();
        case STRING:
            int sstIndex = ((LabelSSTRecord) _record).getSSTIndex();
            String text = _book.getWorkbook().getSSTString(sstIndex).getString();
            return Boolean.valueOf(text).booleanValue();
        case NUMERIC:
            return ((NumberRecord) _record).getValue() != 0;
        case FORMULA:
            // use cached formula result if it's the right type:
            FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
            checkFormulaCachedValueType(CellType.BOOLEAN, fr);
            return fr.getCachedBooleanValue();
        // These choices are not well justified.
        case ERROR:
        case BLANK:
            return false;
    }
    throw new RuntimeException("Unexpected cell 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) RichTextString(org.apache.poi.ss.usermodel.RichTextString) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord)

Aggregations

FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)22 Record (org.apache.poi.hssf.record.Record)8 FormulaRecordAggregate (org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)8 NumberRecord (org.apache.poi.hssf.record.NumberRecord)7 StringRecord (org.apache.poi.hssf.record.StringRecord)6 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)5 Test (org.junit.Test)5 BlankRecord (org.apache.poi.hssf.record.BlankRecord)4 SharedFormulaRecord (org.apache.poi.hssf.record.SharedFormulaRecord)4 ArrayList (java.util.ArrayList)3 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)3 MulBlankRecord (org.apache.poi.hssf.record.MulBlankRecord)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 ArrayRecord (org.apache.poi.hssf.record.ArrayRecord)2 BOFRecord (org.apache.poi.hssf.record.BOFRecord)2 CellValueRecordInterface (org.apache.poi.hssf.record.CellValueRecordInterface)2 ContinueRecord (org.apache.poi.hssf.record.ContinueRecord)2