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;
}
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);
}
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;
}
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());
}
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();
}
Aggregations