From 6718bc0d2da72cd020f5ea40289f35f9fa5f3b1c Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 9 Jun 2010 22:56:16 +0000 Subject: [PATCH] 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 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/hssf/model/InternalWorkbook.java | 9 ++++ .../apache/poi/hssf/usermodel/TestBugs.java | 44 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 45793d7961..50e37663d6 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 46664 - fix up Tab IDs when adding new sheets, so that print areas don't end up invalid 45269 - improve replaceText on HWPF ranges 47815 - correct documentation on what happens when you request a String from a non-string Formula cell 49386 - avoid NPE when extracting OOXML file properties which are dates diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index c17a4d7173..b1340f524b 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -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(); + } + } } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index 01510b087d..e7e503adcb 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -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 -- 2.39.5