]> source.dussan.org Git - poi.git/commitdiff
fixed XSSFWorkbook.createSheet to properly increment sheetId when sheetId sequence...
authorYegor Kozlov <yegor@apache.org>
Fri, 24 Apr 2009 18:25:19 +0000 (18:25 +0000)
committerYegor Kozlov <yegor@apache.org>
Fri, 24 Apr 2009 18:25:19 +0000 (18:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@768389 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

index 6619952eedeccda93ab7e90b947d83779eaaa976..39f0cfc0e086daeebcf7a40fc3c551b9312a8452 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.5-beta6" date="2009-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">47089 - Fixed XSSFWorkbook.createSheet to properly increment sheetId</action>
            <action dev="POI-DEVELOPERS" type="fix">46568 - Fixed XSLFPowerPointExtractor to properly process line breaks</action>
            <action dev="POI-DEVELOPERS" type="fix">39056 - Fixed POIFSFileSystem to set CLSID of root when constructing instances from InputStream</action>
            <action dev="POI-DEVELOPERS" type="fix">47054 - Fixed cloneStyleFrom to avoid exception when cloning styles of the same family</action>
index 7da766676d56e34a8d45fbac7c06a12381da6aa8..82563c9f672ad305993ce526aff3d0443e53eae2 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.5-beta6" date="2009-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">47089 - Fixed XSSFWorkbook.createSheet to properly increment sheetId</action>
            <action dev="POI-DEVELOPERS" type="fix">46568 - Fixed XSLFPowerPointExtractor to properly process line breaks</action>
            <action dev="POI-DEVELOPERS" type="fix">39056 - Fixed POIFSFileSystem to set CLSID of root when constructing instances from InputStream</action>
            <action dev="POI-DEVELOPERS" type="fix">47054 - Fixed cloneStyleFrom to avoid exception when cloning styles of the same family</action>
index 1648ac86f35e79c69e0d25c187753bc578c7efea..c288354898d7a69817986c43593f1e2521217641 100644 (file)
@@ -453,7 +453,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
 
         CTSheet sheet = addSheet(sheetname);
 
-        int sheetNumber = getNumberOfSheets() + 1;
+        int sheetNumber = 1;
+        for(XSSFSheet sh : sheets) sheetNumber = (int)Math.max(sh.sheet.getSheetId() + 1, sheetNumber);
+
         XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFFactory.getInstance(), sheetNumber);
         wrapper.sheet = sheet;
         sheet.setId(wrapper.getPackageRelationship().getId());
index 5a4b2b1fd69655a74d9884aba9e47503d26923d1..33de41f84603a55a0245eb49f82b84713c43020f 100644 (file)
@@ -227,5 +227,18 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
                assertEquals(2, st.getFills().size());
                assertEquals(1, st.getBorders().size());
        }
-       
+
+    public void testIncrementSheetId() throws Exception {
+        XSSFWorkbook wb = getTestDataProvider().createWorkbook();
+        int sheetId = (int)wb.createSheet().sheet.getSheetId();
+        assertEquals(1, sheetId);
+        sheetId = (int)wb.createSheet().sheet.getSheetId();
+        assertEquals(2, sheetId);
+
+        //test file with gaps in the sheetId sequence
+        wb = getTestDataProvider().openSampleWorkbook("47089.xlsm");
+        int lastSheetId = (int)wb.getSheetAt(wb.getNumberOfSheets() - 1).sheet.getSheetId();
+        sheetId = (int)wb.createSheet().sheet.getSheetId();
+        assertEquals(lastSheetId+1, sheetId);
+    }
 }