Search in sources :

Example 1 with ArrayRecord

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

the class FormulaRecordAggregate method isPartOfArrayFormula.

public boolean isPartOfArrayFormula() {
    if (_sharedFormulaRecord != null) {
        return false;
    }
    CellReference expRef = _formulaRecord.getFormula().getExpReference();
    ArrayRecord arec = expRef == null ? null : _sharedValueManager.getArrayRecord(expRef.getRow(), expRef.getCol());
    return arec != null;
}
Also used : ArrayRecord(org.apache.poi.hssf.record.ArrayRecord) CellReference(org.apache.poi.ss.util.CellReference)

Example 2 with ArrayRecord

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

the class FormulaRecordAggregate method setArrayFormula.

public void setArrayFormula(CellRangeAddress r, Ptg[] ptgs) {
    ArrayRecord arr = new ArrayRecord(Formula.create(ptgs), new CellRangeAddress8Bit(r.getFirstRow(), r.getLastRow(), r.getFirstColumn(), r.getLastColumn()));
    _sharedValueManager.addArrayRecord(arr);
}
Also used : ArrayRecord(org.apache.poi.hssf.record.ArrayRecord) CellRangeAddress8Bit(org.apache.poi.hssf.util.CellRangeAddress8Bit)

Example 3 with ArrayRecord

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

the class SharedValueManager method getRecordForFirstCell.

/**
	 * Gets the {@link SharedValueRecordBase} record if it should be encoded immediately after the
	 * formula record contained in the specified {@link FormulaRecordAggregate} agg.  Note - the
	 * shared value record always appears after the first formula record in the group.  For arrays
	 * and tables the first formula is always the in the top left cell.  However, since shared
	 * formula groups can be sparse and/or overlap, the first formula may not actually be in the
	 * top left cell.
	 *
	 * @return the SHRFMLA, TABLE or ARRAY record for the formula cell, if it is the first cell of
	 * a table or array region. <code>null</code> if the formula cell is not shared/array/table,
	 * or if the specified formula is not the the first in the group.
	 */
public SharedValueRecordBase getRecordForFirstCell(FormulaRecordAggregate agg) {
    CellReference firstCell = agg.getFormulaRecord().getFormula().getExpReference();
    // and/or distinguishing between tExp and tTbl.
    if (firstCell == null) {
        // not a shared/array/table formula
        return null;
    }
    int row = firstCell.getRow();
    int column = firstCell.getCol();
    if (agg.getRow() != row || agg.getColumn() != column) {
        // not the first formula cell in the group
        return null;
    }
    if (!_groupsBySharedFormulaRecord.isEmpty()) {
        SharedFormulaGroup sfg = findFormulaGroupForCell(firstCell);
        if (null != sfg) {
            return sfg.getSFR();
        }
    }
    for (TableRecord tr : _tableRecords) {
        if (tr.isFirstCell(row, column)) {
            return tr;
        }
    }
    for (ArrayRecord ar : _arrayRecords) {
        if (ar.isFirstCell(row, column)) {
            return ar;
        }
    }
    return null;
}
Also used : ArrayRecord(org.apache.poi.hssf.record.ArrayRecord) CellReference(org.apache.poi.ss.util.CellReference) TableRecord(org.apache.poi.hssf.record.TableRecord)

Example 4 with ArrayRecord

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

the class FormulaRecordAggregate method getArrayFormulaRange.

public CellRangeAddress getArrayFormulaRange() {
    if (_sharedFormulaRecord != null) {
        throw new IllegalStateException("not an array formula cell.");
    }
    CellReference expRef = _formulaRecord.getFormula().getExpReference();
    if (expRef == null) {
        throw new IllegalStateException("not an array formula cell.");
    }
    ArrayRecord arec = _sharedValueManager.getArrayRecord(expRef.getRow(), expRef.getCol());
    if (arec == null) {
        throw new IllegalStateException("ArrayRecord was not found for the locator " + expRef.formatAsString());
    }
    CellRangeAddress8Bit a = arec.getRange();
    return new CellRangeAddress(a.getFirstRow(), a.getLastRow(), a.getFirstColumn(), a.getLastColumn());
}
Also used : ArrayRecord(org.apache.poi.hssf.record.ArrayRecord) CellRangeAddress8Bit(org.apache.poi.hssf.util.CellRangeAddress8Bit) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) CellReference(org.apache.poi.ss.util.CellReference)

Example 5 with ArrayRecord

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

the class TestRowRecordsAggregate method testArraysAndTables.

/**
	 * Prior to Aug 2008, POI would re-serialize spreadsheets with {@link ArrayRecord}s or
	 * {@link TableRecord}s with those records out of order.  Similar to
	 * {@link SharedFormulaRecord}s, these records should appear immediately after the first
	 * {@link FormulaRecord}s that they apply to (and only once).<br/>
	 */
@Test
public void testArraysAndTables() throws Exception {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testArraysAndTables.xls");
    Record[] sheetRecs = RecordInspector.getRecords(wb.getSheetAt(0), 0);
    int countArrayFormulas = verifySharedValues(sheetRecs, ArrayRecord.class);
    assertEquals(5, countArrayFormulas);
    int countTableFormulas = verifySharedValues(sheetRecs, TableRecord.class);
    assertEquals(3, countTableFormulas);
    // Note - SharedFormulaRecords are currently not re-serialized by POI (each is extracted
    // into many non-shared formulas), but if they ever were, the same rules would apply.
    int countSharedFormulas = verifySharedValues(sheetRecs, SharedFormulaRecord.class);
    assertEquals(0, countSharedFormulas);
    //		if (false) { // set true to observe re-serialized file
    //			File f = new File(System.getProperty("java.io.tmpdir") + "/testArraysAndTables-out.xls");
    //			try {
    //				OutputStream os = new FileOutputStream(f);
    //				wb.write(os);
    //				os.close();
    //			} catch (IOException e) {
    //				throw new RuntimeException(e);
    //			}
    //			System.out.println("Output file to " + f.getAbsolutePath());
    //		}
    wb.close();
}
Also used : Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) ArrayRecord(org.apache.poi.hssf.record.ArrayRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) TableRecord(org.apache.poi.hssf.record.TableRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Aggregations

ArrayRecord (org.apache.poi.hssf.record.ArrayRecord)5 CellReference (org.apache.poi.ss.util.CellReference)3 TableRecord (org.apache.poi.hssf.record.TableRecord)2 CellRangeAddress8Bit (org.apache.poi.hssf.util.CellRangeAddress8Bit)2 ContinueRecord (org.apache.poi.hssf.record.ContinueRecord)1 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)1 NumberRecord (org.apache.poi.hssf.record.NumberRecord)1 Record (org.apache.poi.hssf.record.Record)1 RowRecord (org.apache.poi.hssf.record.RowRecord)1 SharedFormulaRecord (org.apache.poi.hssf.record.SharedFormulaRecord)1 UnknownRecord (org.apache.poi.hssf.record.UnknownRecord)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 Test (org.junit.Test)1