diff options
author | Nick Burch <nick@apache.org> | 2008-06-18 11:35:04 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2008-06-18 11:35:04 +0000 |
commit | 1f08e18ea8d04bb26b2c2fc52de6fb30e356cf86 (patch) | |
tree | 293eda7e0ca08897484619978bb52c1f7a86b40f /src | |
parent | 5c27b368a603b7568648ac8dae3c1699217dfb32 (diff) | |
download | poi-1f08e18ea8d04bb26b2c2fc52de6fb30e356cf86.tar.gz poi-1f08e18ea8d04bb26b2c2fc52de6fb30e356cf86.zip |
A partial fix for bug #30978, but something still remains, which seems to be related to changing the ptg
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@669140 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r-- | src/java/org/apache/poi/hssf/model/Workbook.java | 24 | ||||
-rw-r--r-- | src/testcases/org/apache/poi/hssf/data/30978-alt.xls | bin | 0 -> 14848 bytes | |||
-rw-r--r-- | src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java | 62 |
3 files changed, 84 insertions, 2 deletions
diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index 5b38935f4c..fa22cfb688 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -604,8 +604,28 @@ public class Workbook implements Model fixTabIdRecord(); } - // If we decide that we need to fix up - // NameRecords, do it here + // Within NameRecords, it's ok to have the formula + // part point at deleted sheets. It's also ok to + // have the ExternSheetNumber point at deleted + // sheets. + // However, the sheet index must be adjusted, or + // excel will break. (Sheet index is either 0 for + // global, or 1 based index to sheet) + int sheetNum1Based = sheetnum + 1; + for(int i=0; i<getNumNames(); i++) { + NameRecord nr = getNameRecord(i); + + if(nr.getIndexToSheet() == sheetNum1Based) { + // Excel re-writes these to point to no sheet + nr.setEqualsToIndexToSheet((short)0); + } else if(nr.getIndexToSheet() > sheetNum1Based) { + // Bump down by one, so still points + // at the same sheet + nr.setEqualsToIndexToSheet((short)( + nr.getEqualsToIndexToSheet()-1 + )); + } + } } /** diff --git a/src/testcases/org/apache/poi/hssf/data/30978-alt.xls b/src/testcases/org/apache/poi/hssf/data/30978-alt.xls Binary files differnew file mode 100644 index 0000000000..c591582885 --- /dev/null +++ b/src/testcases/org/apache/poi/hssf/data/30978-alt.xls diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index cbe175a0e3..ab247b7c8a 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -28,7 +28,10 @@ import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord; +import org.apache.poi.hssf.record.NameRecord; +import org.apache.poi.hssf.record.formula.DeletedArea3DPtg; import org.apache.poi.hssf.util.Region; import org.apache.poi.util.TempFile; @@ -989,4 +992,63 @@ public final class TestBugs extends TestCase { fail(); } catch(FileNotFoundException e) {} } + + /** + * Test that we can delete sheets without + * breaking the build in named ranges + * used for printing stuff. + * Currently broken, as we change the Ptg + */ + public void BROKENtest30978() throws Exception { + HSSFWorkbook wb = openSample("30978-alt.xls"); + assertEquals(1, wb.getNumberOfNames()); + assertEquals(3, wb.getNumberOfSheets()); + + // Check all names fit within range, and use + // DeletedArea3DPtg + Workbook w = wb.getWorkbook(); + for(int i=0; i<w.getNumNames(); i++) { + NameRecord r = w.getNameRecord(i); + assertTrue(r.getIndexToSheet() <= wb.getNumberOfSheets()); + + List nd = r.getNameDefinition(); + assertEquals(1, nd.size()); + assertTrue(nd.get(0) instanceof DeletedArea3DPtg); + } + + + // Delete the 2nd sheet + wb.removeSheetAt(1); + + + // Re-check + assertEquals(1, wb.getNumberOfNames()); + assertEquals(2, wb.getNumberOfSheets()); + + for(int i=0; i<w.getNumNames(); i++) { + NameRecord r = w.getNameRecord(i); + assertTrue(r.getIndexToSheet() <= wb.getNumberOfSheets()); + + List nd = r.getNameDefinition(); + assertEquals(1, nd.size()); + assertTrue(nd.get(0) instanceof DeletedArea3DPtg); + } + + + // Save and re-load + wb = writeOutAndReadBack(wb); + w = wb.getWorkbook(); + + assertEquals(1, wb.getNumberOfNames()); + assertEquals(2, wb.getNumberOfSheets()); + + for(int i=0; i<w.getNumNames(); i++) { + NameRecord r = w.getNameRecord(i); + assertTrue(r.getIndexToSheet() <= wb.getNumberOfSheets()); + + List nd = r.getNameDefinition(); + assertEquals(1, nd.size()); + assertTrue(nd.get(0) instanceof DeletedArea3DPtg); + } + } } |