use of org.apache.poi.hssf.record.Record 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.Record in project poi by apache.
the class TestCFRecordsAggregate method testCFRecordsAggregate.
public void testCFRecordsAggregate() {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
List<Record> recs = new ArrayList<Record>();
CFHeaderBase header = new CFHeaderRecord();
CFRuleBase rule1 = CFRuleRecord.create(sheet, "7");
CFRuleBase rule2 = CFRuleRecord.create(sheet, ComparisonOperator.BETWEEN, "2", "5");
CFRuleBase rule3 = CFRuleRecord.create(sheet, ComparisonOperator.GE, "100", null);
header.setNumberOfConditionalFormats(3);
CellRangeAddress[] cellRanges = { new CellRangeAddress(0, 1, 0, 0), new CellRangeAddress(0, 1, 2, 2) };
header.setCellRanges(cellRanges);
recs.add(header);
recs.add(rule1);
recs.add(rule2);
recs.add(rule3);
CFRecordsAggregate record = CFRecordsAggregate.createCFAggregate(new RecordStream(recs, 0));
// Serialize
byte[] serializedRecord = new byte[record.getRecordSize()];
record.serialize(0, serializedRecord);
InputStream in = new ByteArrayInputStream(serializedRecord);
//Parse
recs = RecordFactory.createRecords(in);
// Verify
assertNotNull(recs);
assertEquals(4, recs.size());
header = (CFHeaderRecord) recs.get(0);
rule1 = (CFRuleRecord) recs.get(1);
assertNotNull(rule1);
rule2 = (CFRuleRecord) recs.get(2);
assertNotNull(rule2);
rule3 = (CFRuleRecord) recs.get(3);
assertNotNull(rule3);
cellRanges = header.getCellRanges();
assertEquals(2, cellRanges.length);
assertEquals(3, header.getNumberOfConditionalFormats());
assertFalse(header.getNeedRecalculation());
record = CFRecordsAggregate.createCFAggregate(new RecordStream(recs, 0));
record = record.cloneCFAggregate();
assertNotNull(record.getHeader());
assertEquals(3, record.getNumberOfRules());
header = record.getHeader();
rule1 = record.getRule(0);
assertNotNull(rule1);
rule2 = record.getRule(1);
assertNotNull(rule2);
rule3 = record.getRule(2);
assertNotNull(rule3);
cellRanges = header.getCellRanges();
assertEquals(2, cellRanges.length);
assertEquals(3, header.getNumberOfConditionalFormats());
assertFalse(header.getNeedRecalculation());
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class TestHSSFSheet method protectSheetRecordOrder_bug47363a.
/**
* {@link PasswordRecord} belongs with the rest of the Worksheet Protection Block
* (which should be before {@link DimensionsRecord}).
*/
@Test
public void protectSheetRecordOrder_bug47363a() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
s.protectSheet("secret");
RecordCollector rc = new RecordCollector();
s.getSheet().visitContainedRecords(rc, 0);
Record[] recs = rc.getRecords();
int nRecs = recs.length;
if (recs[nRecs - 2] instanceof PasswordRecord && recs[nRecs - 5] instanceof DimensionsRecord) {
fail("Identified bug 47363a - PASSWORD after DIMENSION");
}
// Check that protection block is together, and before DIMENSION
confirmRecordClass(recs, nRecs - 4, DimensionsRecord.class);
confirmRecordClass(recs, nRecs - 9, ProtectRecord.class);
confirmRecordClass(recs, nRecs - 8, ObjectProtectRecord.class);
confirmRecordClass(recs, nRecs - 7, ScenarioProtectRecord.class);
confirmRecordClass(recs, nRecs - 6, PasswordRecord.class);
wb.close();
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class TestHSSFCell method testCachedTypeChange.
/**
* Test for small bug observable around r736460 (prior to version 3.5). POI fails to remove
* the {@link StringRecord} following the {@link FormulaRecord} after the result type had been
* changed to number/boolean/error. Excel silently ignores the extra record, but some POI
* versions (prior to bug 46213 / r717883) crash instead.
* @throws IOException
*/
@Test
public void testCachedTypeChange() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellFormula("A1");
cell.setCellValue("abc");
confirmStringRecord(sheet, true);
cell.setCellValue(123);
Record[] recs = RecordInspector.getRecords(sheet, 0);
if (recs.length == 28 && recs[23] instanceof StringRecord) {
wb.close();
throw new AssertionFailedError("Identified bug - leftover StringRecord");
}
confirmStringRecord(sheet, false);
// string to error code
cell.setCellValue("abc");
confirmStringRecord(sheet, true);
cell.setCellErrorValue(FormulaError.REF.getCode());
confirmStringRecord(sheet, false);
// string to boolean
cell.setCellValue("abc");
confirmStringRecord(sheet, true);
cell.setCellValue(false);
confirmStringRecord(sheet, false);
wb.close();
}
use of org.apache.poi.hssf.record.Record 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;
}
Aggregations