Fix bug #46664 - fix up Tab IDs when adding new sheets, so that print areas don't end up invalid

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@953180 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2010-06-09 22:56:16 +00:00
parent 81e5d7f4b5
commit 6718bc0d2d
3 changed files with 54 additions and 0 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.7-SNAPSHOT" date="2010-??-??">
<action dev="POI-DEVELOPERS" type="fix">46664 - fix up Tab IDs when adding new sheets, so that print areas don't end up invalid</action>
<action dev="POI-DEVELOPERS" type="fix">45269 - improve replaceText on HWPF ranges</action>
<action dev="POI-DEVELOPERS" type="fix">47815 - correct documentation on what happens when you request a String from a non-string Formula cell</action>
<action dev="POI-DEVELOPERS" type="fix">49386 - avoid NPE when extracting OOXML file properties which are dates</action>

View File

@ -712,6 +712,15 @@ public final class InternalWorkbook {
boundsheets.add(bsr);
getOrCreateLinkTable().checkExternSheet(sheetnum);
fixTabIdRecord();
} else {
// Ensure we have enough tab IDs
// Can be a few short if new sheets were added
if(records.getTabpos() > 0) {
TabIdRecord tir = ( TabIdRecord ) records.get(records.getTabpos());
if(tir._tabids.length < boundsheets.size()) {
fixTabIdRecord();
}
}
}
}

View File

@ -35,6 +35,8 @@ import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.TabIdRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.record.formula.DeletedArea3DPtg;
@ -1589,6 +1591,48 @@ public final class TestBugs extends BaseTestBugzillaIssues {
assertEquals(2, wb.getNumberOfSheets());
}
/**
* Newly created sheets need to get a
* proper TabID, otherwise print setup
* gets confused on them.
*/
public void test46664() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.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)
wb.setPrintArea(
0, //sheet index
0, //start column
2, //end column
0, //start row
0 //end row
);
wb = writeOutAndReadBack(wb);
// Ensure the tab index
TabIdRecord tr = null;
for(Record r : wb.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", wb.getPrintArea(0));
}
/**
* Problems with formula references to
* sheets via URLs