use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class TestValueRecordsAggregate method testData.
private static List<Record> testData() {
List<Record> records = new ArrayList<Record>();
FormulaRecord formulaRecord = new FormulaRecord();
BlankRecord blankRecord = new BlankRecord();
formulaRecord.setRow(1);
formulaRecord.setColumn((short) 1);
blankRecord.setRow(2);
blankRecord.setColumn((short) 2);
records.add(formulaRecord);
records.add(blankRecord);
records.add(new WindowTwoRecord());
return records;
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class TestRowRecordsAggregate method verifySharedValues.
private static int verifySharedValues(Record[] recs, Class<? extends SharedValueRecordBase> shfClass) {
int result = 0;
for (int i = 0; i < recs.length; i++) {
Record rec = recs[i];
if (rec.getClass() == shfClass) {
result++;
Record prevRec = recs[i - 1];
if (!(prevRec instanceof FormulaRecord)) {
fail("Bad record order at index " + i + ": Formula record expected but got (" + prevRec.getClass().getName() + ")");
}
verifySharedFormula((FormulaRecord) prevRec, rec);
}
}
return result;
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class TestEventWorkbookBuilder method testFormulas.
public void testFormulas() {
FormulaRecord[] fRecs = mockListen.getFormulaRecords();
// Check our formula records
assertEquals(6, fRecs.length);
InternalWorkbook stubWB = listener.getStubWorkbook();
assertNotNull(stubWB);
HSSFWorkbook stubHSSF = listener.getStubHSSFWorkbook();
assertNotNull(stubHSSF);
// Check these stubs have the right stuff on them
assertEquals("Sheet1", stubWB.getSheetName(0));
assertEquals("Sheet1", stubHSSF.getSheetName(0));
assertEquals("S2", stubWB.getSheetName(1));
assertEquals("S2", stubHSSF.getSheetName(1));
assertEquals("Sh3", stubWB.getSheetName(2));
assertEquals("Sh3", stubHSSF.getSheetName(2));
// Check we can get the formula without breaking
for (FormulaRecord fRec : fRecs) {
HSSFFormulaParser.toFormulaString(stubHSSF, fRec.getParsedExpression());
}
// Peer into just one formula, and check that
// all the ptgs give back the right things
Ptg[] ptgs = fRecs[0].getParsedExpression();
assertEquals(1, ptgs.length);
assertTrue(ptgs[0] instanceof Ref3DPtg);
Ref3DPtg ptg = (Ref3DPtg) ptgs[0];
HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(stubHSSF);
assertEquals("Sheet1!A1", ptg.toFormulaString(book));
// Now check we get the right formula back for
// a few sample ones
FormulaRecord fr;
// Sheet 1 A2 is on same sheet
fr = fRecs[0];
assertEquals(1, fr.getRow());
assertEquals(0, fr.getColumn());
assertEquals("Sheet1!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Sheet 1 A5 is to another sheet
fr = fRecs[3];
assertEquals(4, fr.getRow());
assertEquals(0, fr.getColumn());
assertEquals("'S2'!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Sheet 1 A7 is to another sheet, range
fr = fRecs[5];
assertEquals(6, fr.getRow());
assertEquals(0, fr.getColumn());
assertEquals("SUM(Sh3!A1:A4)", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Now, load via Usermodel and re-check
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("3dFormulas.xls");
assertEquals("Sheet1!A1", wb.getSheetAt(0).getRow(1).getCell(0).getCellFormula());
assertEquals("SUM(Sh3!A1:A4)", wb.getSheetAt(0).getRow(6).getCell(0).getCellFormula());
}
use of org.apache.poi.hssf.record.FormulaRecord 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.FormulaRecord in project poi by apache.
the class TestFormulaRecordAggregate method testBasic.
public void testBasic() {
FormulaRecord f = new FormulaRecord();
f.setCachedResultTypeString();
StringRecord s = new StringRecord();
s.setString("abc");
FormulaRecordAggregate fagg = new FormulaRecordAggregate(f, s, SharedValueManager.createEmpty());
assertEquals("abc", fagg.getStringValue());
assertFalse(fagg.isPartOfArrayFormula());
}
Aggregations