Search in sources :

Example 16 with FormulaRecordAggregate

use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate 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)

Example 17 with FormulaRecordAggregate

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

the class TestHSSFSheetUpdateArrayFormulas method testHSSFSetArrayFormula_singleCell.

// Test methods common with XSSF are in superclass
// Local methods here test HSSF-specific details of updating array formulas
@Test
public void testHSSFSetArrayFormula_singleCell() throws IOException {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Sheet1");
    CellRangeAddress range = new CellRangeAddress(2, 2, 2, 2);
    HSSFCell[] cells = sheet.setArrayFormula("SUM(C11:C12*D11:D12)", range).getFlattenedCells();
    assertEquals(1, cells.length);
    // sheet.setArrayFormula creates rows and cells for the designated range
    assertNotNull(sheet.getRow(2));
    HSSFCell cell = sheet.getRow(2).getCell(2);
    assertNotNull(cell);
    assertTrue(cell.isPartOfArrayFormulaGroup());
    //retrieve the range and check it is the same
    assertEquals(range.formatAsString(), cell.getArrayFormulaRange().formatAsString());
    FormulaRecordAggregate agg = (FormulaRecordAggregate) cell.getCellValueRecord();
    assertEquals(range.formatAsString(), agg.getArrayFormulaRange().formatAsString());
    assertTrue(agg.isPartOfArrayFormula());
    workbook.close();
}
Also used : FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Test(org.junit.Test)

Example 18 with FormulaRecordAggregate

use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate 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 19 with FormulaRecordAggregate

use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate 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

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