You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2018/01/01 14:39:33 UTC

svn commit: r1819773 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/usermodel/XSSFSheet.java testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

Author: centic
Date: Mon Jan  1 14:39:33 2018
New Revision: 1819773

URL: http://svn.apache.org/viewvc?rev=1819773&view=rev
Log:
Bug 61543: do not fail with "part already exists" when tables are created/removed

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1819773&r1=1819772&r2=1819773&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Mon Jan  1 14:39:33 2018
@@ -3957,24 +3957,40 @@ public class XSSFSheet extends POIXMLDoc
      * Creates a new Table, and associates it with this Sheet
      */
     public XSSFTable createTable() {
-       if(! worksheet.isSetTableParts()) {
-          worksheet.addNewTableParts();
-       }
-
-       CTTableParts tblParts = worksheet.getTableParts();
-       CTTablePart tbl = tblParts.addNewTablePart();
-
-       // Table numbers need to be unique in the file, not just
-       //  unique within the sheet. Find the next one
-       int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1;
-       RelationPart rp = createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber, false);
-       XSSFTable table = rp.getDocumentPart();
-       tbl.setId(rp.getRelationship().getId());
-       table.getCTTable().setId(tableNumber);
+        if(! worksheet.isSetTableParts()) {
+            worksheet.addNewTableParts();
+        }
 
-       tables.put(tbl.getId(), table);
+        CTTableParts tblParts = worksheet.getTableParts();
+        CTTablePart tbl = tblParts.addNewTablePart();
 
-       return table;
+        // Table numbers need to be unique in the file, not just
+        //  unique within the sheet. Find the next one
+        int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1;
+
+        // the id could already be taken after insertion/deletion of different tables
+        outerloop:
+        while(true) {
+            for (PackagePart packagePart : getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType())) {
+                String fileName = XSSFRelation.TABLE.getFileName(tableNumber);
+                if(fileName.equals(packagePart.getPartName().getName())) {
+                    // duplicate found, increase the number and start iterating again
+                    tableNumber++;
+                    continue outerloop;
+                }
+            }
+
+            break;
+        }
+
+        RelationPart rp = createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber, false);
+        XSSFTable table = rp.getDocumentPart();
+        tbl.setId(rp.getRelationship().getId());
+        table.getCTTable().setId(tableNumber);
+
+        tables.put(tbl.getId(), table);
+
+        return table;
     }
 
     /**

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1819773&r1=1819772&r2=1819773&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java Mon Jan  1 14:39:33 2018
@@ -3231,4 +3231,25 @@ public final class TestXSSFBugs extends
             assertEquals("AND($A1>=EDATE($D$6,3),$B1>0)", rules.get(0).getFormula1());
         }
     }
+
+    @Test
+    public void test61543() throws IOException {
+        XSSFWorkbook wb = new XSSFWorkbook();
+
+        XSSFSheet sheet = wb.createSheet();
+        XSSFTable table1 = sheet.createTable();
+        XSSFTable table2 = sheet.createTable();
+        XSSFTable table3 = sheet.createTable();
+
+        sheet.removeTable(table1);
+
+        sheet.createTable();
+
+        sheet.removeTable(table2);
+        sheet.removeTable(table3);
+
+        sheet.createTable();
+
+        wb.close();
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org