use of org.apache.poi.hssf.record.Record in project poi by apache.
the class LinkTable method addNameXPtg.
/**
* Register an external name in this workbook
*
* @param name the name to register
* @return a NameXPtg describing this name
*/
public NameXPtg addNameXPtg(String name) {
int extBlockIndex = -1;
ExternalBookBlock extBlock = null;
// find ExternalBlock for Add-In functions and remember its index
for (int i = 0; i < _externalBookBlocks.length; i++) {
SupBookRecord ebr = _externalBookBlocks[i].getExternalBookRecord();
if (ebr.isAddInFunctions()) {
extBlock = _externalBookBlocks[i];
extBlockIndex = i;
break;
}
}
// An ExternalBlock for Add-In functions was not found. Create a new one.
if (extBlock == null) {
extBlock = new ExternalBookBlock();
extBlockIndex = extendExternalBookBlocks(extBlock);
// add the created SupBookRecord before ExternSheetRecord
int idx = findFirstRecordLocBySid(ExternSheetRecord.sid);
_workbookRecordList.add(idx, extBlock.getExternalBookRecord());
// register the SupBookRecord in the ExternSheetRecord
// -2 means that the scope of this name is Workbook and the reference applies to the entire workbook.
_externSheetRecord.addRef(_externalBookBlocks.length - 1, -2, -2);
}
// create a ExternalNameRecord that will describe this name
ExternalNameRecord extNameRecord = new ExternalNameRecord();
extNameRecord.setText(name);
// The docs don't explain why Excel set the formula to #REF!
extNameRecord.setParsedExpression(new Ptg[] { ErrPtg.REF_INVALID });
int nameIndex = extBlock.addExternalName(extNameRecord);
int supLinkIndex = 0;
// the created ExternalNameRecord will be appended to it
for (Iterator<Record> iterator = _workbookRecordList.iterator(); iterator.hasNext(); supLinkIndex++) {
Record record = iterator.next();
if (record instanceof SupBookRecord) {
if (((SupBookRecord) record).isAddInFunctions())
break;
}
}
int numberOfNames = extBlock.getNumberOfNames();
// a new name is inserted in the end of the SupBookRecord, after the last name
_workbookRecordList.add(supLinkIndex + numberOfNames, extNameRecord);
int fakeSheetIdx = -2;
/* the scope is workbook*/
int ix = _externSheetRecord.getRefIxForSheet(extBlockIndex, fakeSheetIdx, fakeSheetIdx);
return new NameXPtg(ix, nameIndex);
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class InternalSheet method cloneSheet.
/**
* Clones the low level records of this sheet and returns the new sheet instance.
* This method is implemented by adding methods for deep cloning to all records that
* can be added to a sheet. The <b>Record</b> object does not implement cloneable.
* When adding a new record, implement a public clone method if and only if the record
* belongs to a sheet.
*
* @return the cloned sheet
*/
public InternalSheet cloneSheet() {
List<Record> clonedRecords = new ArrayList<Record>(_records.size());
for (int i = 0; i < _records.size(); i++) {
RecordBase rb = _records.get(i);
if (rb instanceof RecordAggregate) {
((RecordAggregate) rb).visitContainedRecords(new RecordCloner(clonedRecords));
continue;
}
if (rb instanceof EscherAggregate) {
/**
* this record will be removed after reading actual data from EscherAggregate
*/
rb = new DrawingRecord();
}
try {
Record rec = (Record) ((Record) rb).clone();
clonedRecords.add(rec);
} catch (CloneNotSupportedException e) {
throw new RecordFormatException(e);
}
}
return createSheet(new RecordStream(clonedRecords, 0));
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class HSSFSheet method getDataValidations.
@Override
public List<HSSFDataValidation> getDataValidations() {
DataValidityTable dvt = _sheet.getOrCreateDataValidityTable();
final List<HSSFDataValidation> hssfValidations = new ArrayList<HSSFDataValidation>();
RecordVisitor visitor = new RecordVisitor() {
private HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(getWorkbook());
@Override
public void visitRecord(Record r) {
if (!(r instanceof DVRecord)) {
return;
}
DVRecord dvRecord = (DVRecord) r;
CellRangeAddressList regions = dvRecord.getCellRangeAddress().copy();
DVConstraint constraint = DVConstraint.createDVConstraint(dvRecord, book);
HSSFDataValidation hssfDataValidation = new HSSFDataValidation(regions, constraint);
hssfDataValidation.setErrorStyle(dvRecord.getErrorStyle());
hssfDataValidation.setEmptyCellAllowed(dvRecord.getEmptyCellAllowed());
hssfDataValidation.setSuppressDropDownArrow(dvRecord.getSuppressDropdownArrow());
hssfDataValidation.createPromptBox(dvRecord.getPromptTitle(), dvRecord.getPromptText());
hssfDataValidation.setShowPromptBox(dvRecord.getShowPromptOnCellSelected());
hssfDataValidation.createErrorBox(dvRecord.getErrorTitle(), dvRecord.getErrorText());
hssfDataValidation.setShowErrorBox(dvRecord.getShowErrorOnInvalidValue());
hssfValidations.add(hssfDataValidation);
}
};
dvt.visitContainedRecords(visitor);
return hssfValidations;
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class HSSFSheet method setPropertiesFromSheet.
/**
* used internally to set the properties given a Sheet object
*/
private void setPropertiesFromSheet(InternalSheet sheet) {
RowRecord row = sheet.getNextRow();
while (row != null) {
createRowFromRecord(row);
row = sheet.getNextRow();
}
Iterator<CellValueRecordInterface> iter = sheet.getCellValueIterator();
long timestart = System.currentTimeMillis();
if (log.check(POILogger.DEBUG)) {
log.log(DEBUG, "Time at start of cell creating in HSSF sheet = ", Long.valueOf(timestart));
}
HSSFRow lastrow = null;
// Add every cell to its row
while (iter.hasNext()) {
CellValueRecordInterface cval = iter.next();
long cellstart = System.currentTimeMillis();
HSSFRow hrow = lastrow;
if (hrow == null || hrow.getRowNum() != cval.getRow()) {
hrow = getRow(cval.getRow());
lastrow = hrow;
if (hrow == null) {
/* we removed this check, see bug 47245 for the discussion around this
// Some tools (like Perl module Spreadsheet::WriteExcel - bug 41187) skip the RowRecords
// Excel, OpenOffice.org and GoogleDocs are all OK with this, so POI should be too.
if (rowRecordsAlreadyPresent) {
// if at least one row record is present, all should be present.
throw new RuntimeException("Unexpected missing row when some rows already present");
}*/
// create the row record on the fly now.
RowRecord rowRec = new RowRecord(cval.getRow());
sheet.addRow(rowRec);
hrow = createRowFromRecord(rowRec);
}
}
if (log.check(POILogger.DEBUG)) {
if (cval instanceof Record) {
log.log(DEBUG, "record id = " + Integer.toHexString(((Record) cval).getSid()));
} else {
log.log(DEBUG, "record = " + cval);
}
}
hrow.createCellFromRecord(cval);
if (log.check(POILogger.DEBUG)) {
log.log(DEBUG, "record took ", Long.valueOf(System.currentTimeMillis() - cellstart));
}
}
if (log.check(POILogger.DEBUG)) {
log.log(DEBUG, "total sheet cell creation took ", Long.valueOf(System.currentTimeMillis() - timestart));
}
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class TestDrawingAggregate method test45129.
/**
* test reading drawing aggregate from a test file from Bugzilla 45129
*/
@Test
public void test45129() throws IOException {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("45129.xls");
HSSFSheet sh = wb.getSheetAt(0);
InternalWorkbook iworkbook = HSSFTestHelper.getWorkbookForTest(wb);
InternalSheet isheet = HSSFTestHelper.getSheetForTest(sh);
List<RecordBase> records = isheet.getRecords();
// the sheet's drawing is not aggregated
assertEquals("wrong size of sheet records stream", 394, records.size());
// the last record before the drawing block
assertTrue("records.get(18) is expected to be RowRecordsAggregate but was " + records.get(18).getClass().getSimpleName(), records.get(18) instanceof RowRecordsAggregate);
// records to be aggregated
List<RecordBase> dgRecords = records.subList(19, 389);
// collect drawing records into a byte buffer.
byte[] dgBytes = toByteArray(dgRecords);
for (RecordBase rb : dgRecords) {
Record r = (Record) rb;
short sid = r.getSid();
// we expect that drawing block consists of either
// DrawingRecord or ContinueRecord or ObjRecord or TextObjectRecord
assertTrue(sid == DrawingRecord.sid || sid == ContinueRecord.sid || sid == ObjRecord.sid || sid == TextObjectRecord.sid);
}
// the first record after the drawing block
assertTrue("records.get(389) is expected to be Window2", records.get(389) instanceof WindowTwoRecord);
// aggregate drawing records.
// The subrange [19, 388] is expected to be replaced with a EscherAggregate object
DrawingManager2 drawingManager = iworkbook.findDrawingGroup();
int loc = isheet.aggregateDrawingRecords(drawingManager, false);
EscherAggregate agg = (EscherAggregate) records.get(loc);
assertEquals("wrong size of the aggregated sheet records stream", 25, records.size());
assertTrue("records.get(18) is expected to be RowRecordsAggregate but was " + records.get(18).getClass().getSimpleName(), records.get(18) instanceof RowRecordsAggregate);
assertTrue("records.get(19) is expected to be EscherAggregate but was " + records.get(19).getClass().getSimpleName(), records.get(19) instanceof EscherAggregate);
assertTrue("records.get(20) is expected to be Window2 but was " + records.get(20).getClass().getSimpleName(), records.get(20) instanceof WindowTwoRecord);
byte[] dgBytesAfterSave = agg.serialize();
assertEquals("different size of drawing data before and after save", dgBytes.length, dgBytesAfterSave.length);
assertTrue("drawing data before and after save is different", Arrays.equals(dgBytes, dgBytesAfterSave));
wb.close();
}
Aggregations