Search in sources :

Example 1 with TableRecord

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

use of org.apache.poi.hssf.record.TableRecord 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)2 TableRecord (org.apache.poi.hssf.record.TableRecord)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 CellReference (org.apache.poi.ss.util.CellReference)1 Test (org.junit.Test)1