use of org.apache.poi.hssf.record.SharedFormulaRecord in project poi by apache.
the class TestValueRecordsAggregate method testSharedFormula.
/**
* Make sure the shared formula DOESNT makes it to the FormulaRecordAggregate when being parsed
* as part of the value records
*/
@Test
public void testSharedFormula() {
List<Record> records = new ArrayList<Record>();
records.add(new FormulaRecord());
records.add(new SharedFormulaRecord());
records.add(new WindowTwoRecord());
constructValueRecord(records);
List<CellValueRecordInterface> cvrs = getValueRecords();
//Ensure that the SharedFormulaRecord has been converted
assertEquals(1, cvrs.size());
CellValueRecordInterface record = cvrs.get(0);
assertNotNull("Row contains a value", record);
assertTrue("First record is a FormulaRecordsAggregate", (record instanceof FormulaRecordAggregate));
}
use of org.apache.poi.hssf.record.SharedFormulaRecord in project poi by apache.
the class TestMissingRecordAwareHSSFListener method testEndOfRow_bug45672.
/**
* Make sure that the presence of shared formulas does not cause extra
* end-of-row records.
*/
public void testEndOfRow_bug45672() {
readRecords("ex45672.xls");
Record[] rr = r;
int eorCount = 0;
int sfrCount = 0;
for (Record record : rr) {
if (record instanceof SharedFormulaRecord) {
sfrCount++;
}
if (record instanceof LastCellOfRowDummyRecord) {
eorCount++;
}
}
if (eorCount == 2) {
throw new AssertionFailedError("Identified bug 45672");
}
assertEquals(1, eorCount);
assertEquals(1, sfrCount);
}
use of org.apache.poi.hssf.record.SharedFormulaRecord 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();
}
use of org.apache.poi.hssf.record.SharedFormulaRecord in project poi by apache.
the class TestSharedValueManager method testPartiallyOverlappingRanges.
/**
* This bug happened when there were two or more shared formula ranges that overlapped. POI
* would sometimes associate formulas in the overlapping region with the wrong shared formula
*/
public void testPartiallyOverlappingRanges() {
Record[] records;
int attempt = 1;
do {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook(SAMPLE_FILE_NAME);
HSSFSheet sheet = wb.getSheetAt(0);
RecordInspector.getRecords(sheet, 0);
assertEquals("1+1", sheet.getRow(2).getCell(0).getCellFormula());
if ("1+1".equals(sheet.getRow(3).getCell(0).getCellFormula())) {
throw new AssertionFailedError("Identified bug - wrong shared formula record chosen" + " (attempt " + attempt + ")");
}
assertEquals("2+2", sheet.getRow(3).getCell(0).getCellFormula());
records = RecordInspector.getRecords(sheet, 0);
} while (attempt++ < MAX_ATTEMPTS);
int count = 0;
for (Record record : records) {
if (record instanceof SharedFormulaRecord) {
count++;
}
}
assertEquals(2, count);
}
use of org.apache.poi.hssf.record.SharedFormulaRecord in project poi by apache.
the class FormulaRecordAggregate method unlinkSharedFormula.
public void unlinkSharedFormula() {
SharedFormulaRecord sfr = _sharedFormulaRecord;
if (sfr == null) {
throw new IllegalStateException("Formula not linked to shared formula");
}
Ptg[] ptgs = sfr.getFormulaTokens(_formulaRecord);
_formulaRecord.setParsedExpression(ptgs);
//Now its not shared!
_formulaRecord.setSharedFormula(false);
_sharedFormulaRecord = null;
}
Aggregations