use of org.apache.poi.hssf.record.Record in project poi by apache.
the class RecordsStresser method handleWorkbook.
public static void handleWorkbook(HSSFWorkbook wb) {
List<Record> records = wb.getWorkbook().getRecords();
for (Record record : records) {
// }
try {
Record newRecord = (Record) record.clone();
assertEquals("Expecting the same class back from clone(), but had Record of type " + record.getClass() + " and got back a " + newRecord.getClass() + " from clone()", record.getClass(), newRecord.getClass());
byte[] origBytes = record.serialize();
byte[] newBytes = newRecord.serialize();
assertArrayEquals("Record of type " + record.getClass() + " should return the same byte array via the clone() method, but did return a different array", origBytes, newBytes);
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
} catch (RuntimeException e) {
// some Records do not implement clone, ignore those for now
assertTrue(e.getMessage().contains("needs to define a clone method"));
}
}
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class FormulaRecordAggregate method visitContainedRecords.
public void visitContainedRecords(RecordVisitor rv) {
rv.visitRecord(_formulaRecord);
Record sharedFormulaRecord = _sharedValueManager.getRecordForFirstCell(this);
if (sharedFormulaRecord != null) {
rv.visitRecord(sharedFormulaRecord);
}
if (_formulaRecord.hasCachedResultString() && _stringRecord != null) {
rv.visitRecord(_stringRecord);
}
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class TestBugs method bug46664.
/**
* Newly created sheets need to get a
* proper TabID, otherwise print setup
* gets confused on them.
* Also ensure that print setup refs are
* by reference not value
*/
@Test
public void bug46664() throws Exception {
HSSFWorkbook wb1 = new HSSFWorkbook();
HSSFSheet sheet = wb1.createSheet("new_sheet");
HSSFRow row = sheet.createRow((short) 0);
row.createCell(0).setCellValue(new HSSFRichTextString("Column A"));
row.createCell(1).setCellValue(new HSSFRichTextString("Column B"));
row.createCell(2).setCellValue(new HSSFRichTextString("Column C"));
row.createCell(3).setCellValue(new HSSFRichTextString("Column D"));
row.createCell(4).setCellValue(new HSSFRichTextString("Column E"));
row.createCell(5).setCellValue(new HSSFRichTextString("Column F"));
//set print area from column a to column c (on first row)
wb1.setPrintArea(//sheet index
0, //start column
0, //end column
2, //start row
0, //end row
0);
HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
wb1.close();
// Ensure the tab index
TabIdRecord tr = null;
for (Record r : wb2.getWorkbook().getRecords()) {
if (r instanceof TabIdRecord) {
tr = (TabIdRecord) r;
}
}
assertNotNull(tr);
assertEquals(1, tr._tabids.length);
assertEquals(0, tr._tabids[0]);
// Ensure the print setup
assertEquals("new_sheet!$A$1:$C$1", wb2.getPrintArea(0));
HSSFName printArea = wb2.getName("Print_Area");
assertNotNull(printArea);
assertEquals("new_sheet!$A$1:$C$1", printArea.getRefersToFormula());
// Needs reference not value
NameRecord nr = wb2.getWorkbook().getNameRecord(wb2.getNameIndex("Print_Area"));
assertEquals("Print_Area", nr.getNameText());
assertEquals(1, nr.getNameDefinition().length);
assertEquals("new_sheet!$A$1:$C$1", ((Area3DPtg) nr.getNameDefinition()[0]).toFormulaString(HSSFEvaluationWorkbook.create(wb2)));
assertEquals('R', nr.getNameDefinition()[0].getRVAType());
wb2.close();
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class HSSFChart method createBarChart.
/**
* Creates a bar chart. API needs some work. :)
* <p>
* NOTE: Does not yet work... checking it in just so others
* can take a look.
*/
public void createBarChart(HSSFWorkbook workbook, HSSFSheet parentSheet) {
List<Record> records = new ArrayList<Record>();
records.add(createMSDrawingObjectRecord());
records.add(createOBJRecord());
records.add(createBOFRecord());
records.add(new HeaderRecord(""));
records.add(new FooterRecord(""));
records.add(createHCenterRecord());
records.add(createVCenterRecord());
records.add(createPrintSetupRecord());
// unknown 33
records.add(createFontBasisRecord1());
records.add(createFontBasisRecord2());
records.add(new ProtectRecord(false));
records.add(createUnitsRecord());
records.add(createChartRecord(0, 0, 30434904, 19031616));
records.add(createBeginRecord());
records.add(createSCLRecord((short) 1, (short) 1));
records.add(createPlotGrowthRecord(65536, 65536));
records.add(createFrameRecord1());
records.add(createBeginRecord());
records.add(createLineFormatRecord(true));
records.add(createAreaFormatRecord1());
records.add(createEndRecord());
records.add(createSeriesRecord());
records.add(createBeginRecord());
records.add(createTitleLinkedDataRecord());
records.add(createValuesLinkedDataRecord());
records.add(createCategoriesLinkedDataRecord());
records.add(createDataFormatRecord());
// records.add(createBeginRecord());
// unknown
// records.add(createEndRecord());
records.add(createSeriesToChartGroupRecord());
records.add(createEndRecord());
records.add(createSheetPropsRecord());
records.add(createDefaultTextRecord(DefaultDataLabelTextPropertiesRecord.CATEGORY_DATA_TYPE_ALL_TEXT_CHARACTERISTIC));
records.add(createAllTextRecord());
records.add(createBeginRecord());
// unknown
records.add(createFontIndexRecord(5));
records.add(createDirectLinkRecord());
records.add(createEndRecord());
// eek, undocumented text type
records.add(createDefaultTextRecord((short) 3));
records.add(createUnknownTextRecord());
records.add(createBeginRecord());
records.add(createFontIndexRecord((short) 6));
records.add(createDirectLinkRecord());
records.add(createEndRecord());
records.add(createAxisUsedRecord((short) 1));
createAxisRecords(records);
records.add(createEndRecord());
records.add(createDimensionsRecord());
records.add(createSeriesIndexRecord(2));
records.add(createSeriesIndexRecord(1));
records.add(createSeriesIndexRecord(3));
records.add(EOFRecord.instance);
parentSheet.insertChartRecords(records);
workbook.insertChartRecord();
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class HSSFChart method createSeries.
public HSSFSeries createSeries() throws Exception {
ArrayList<RecordBase> seriesTemplate = new ArrayList<RecordBase>();
boolean seriesTemplateFilled = false;
int idx = 0;
int deep = 0;
int chartRecordIdx = -1;
int chartDeep = -1;
int lastSeriesDeep = -1;
int endSeriesRecordIdx = -1;
int seriesIdx = 0;
final List<RecordBase> records = sheet.getSheet().getRecords();
/* store first series as template and find last series index */
for (final RecordBase record : records) {
idx++;
if (record instanceof BeginRecord) {
deep++;
} else if (record instanceof EndRecord) {
deep--;
if (lastSeriesDeep == deep) {
lastSeriesDeep = -1;
endSeriesRecordIdx = idx;
if (!seriesTemplateFilled) {
seriesTemplate.add(record);
seriesTemplateFilled = true;
}
}
if (chartDeep == deep) {
break;
}
}
if (record instanceof ChartRecord) {
if (record == chartRecord) {
chartRecordIdx = idx;
chartDeep = deep;
}
} else if (record instanceof SeriesRecord) {
if (chartRecordIdx != -1) {
seriesIdx++;
lastSeriesDeep = deep;
}
}
if (lastSeriesDeep != -1 && !seriesTemplateFilled) {
seriesTemplate.add(record);
}
}
/* check if a series was found */
if (endSeriesRecordIdx == -1) {
return null;
}
/* next index in the records list where the new series can be inserted */
idx = endSeriesRecordIdx + 1;
HSSFSeries newSeries = null;
/* duplicate record of the template series */
ArrayList<RecordBase> clonedRecords = new ArrayList<RecordBase>();
for (final RecordBase record : seriesTemplate) {
Record newRecord = null;
if (record instanceof BeginRecord) {
newRecord = new BeginRecord();
} else if (record instanceof EndRecord) {
newRecord = new EndRecord();
} else if (record instanceof SeriesRecord) {
SeriesRecord seriesRecord = (SeriesRecord) ((SeriesRecord) record).clone();
newSeries = new HSSFSeries(seriesRecord);
newRecord = seriesRecord;
} else if (record instanceof LinkedDataRecord) {
LinkedDataRecord linkedDataRecord = ((LinkedDataRecord) record).clone();
if (newSeries != null) {
newSeries.insertData(linkedDataRecord);
}
newRecord = linkedDataRecord;
} else if (record instanceof DataFormatRecord) {
DataFormatRecord dataFormatRecord = ((DataFormatRecord) record).clone();
dataFormatRecord.setSeriesIndex((short) seriesIdx);
dataFormatRecord.setSeriesNumber((short) seriesIdx);
newRecord = dataFormatRecord;
} else if (record instanceof SeriesTextRecord) {
SeriesTextRecord seriesTextRecord = (SeriesTextRecord) ((SeriesTextRecord) record).clone();
if (newSeries != null) {
newSeries.setSeriesTitleText(seriesTextRecord);
}
newRecord = seriesTextRecord;
} else if (record instanceof Record) {
newRecord = (Record) ((Record) record).clone();
}
if (newRecord != null) {
clonedRecords.add(newRecord);
}
}
/* check if a user model series object was created */
if (newSeries == null) {
return null;
}
/* transfer series to record list */
for (final RecordBase record : clonedRecords) {
records.add(idx++, record);
}
return newSeries;
}
Aggregations