Author: gwoolsey
Date: Fri Jul 28 18:01:36 2017
New Revision: 1803317

URL: http://svn.apache.org/viewvc?rev=1803317&view=rev
Log:
Deleting a sheet did not delete table parts and relations.  Deleting a table 
needs to also delete any queryTable relations and parts.

Previous behavior didn't result in documents Excel complained about, but left 
dead entries in the ZIP structure, which made it bigger and bugged me.

This change does not attempt to delete query connection definitions, as those 
aren't referenced as relations, and don't have a usage counter to ensure we 
only delete them if there are no other references.  In my samples I had query 
tables on multiple sheets using the same connection definition, and wanted to 
delete only one sheet/table but leave others.

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.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=1803317&r1=1803316&r2=1803317&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 Fri 
Jul 28 18:01:36 2017
@@ -3957,6 +3957,24 @@ public class XSSFSheet extends POIXMLDoc
         return new ArrayList<XSSFTable>(tables.values());
     }
 
+    /**
+     * Remove table references and relations
+     * @param t table to remove
+     */
+    public void removeTable(XSSFTable t) {
+        long id = t.getCTTable().getId();
+        Map.Entry<String, XSSFTable> toDelete = null;
+        
+        for (Map.Entry<String, XSSFTable> entry : tables.entrySet()) {
+            if (entry.getValue().getCTTable().getId() == id) toDelete = entry;
+        }
+        if (toDelete != null) {
+            removeRelation(getRelationById(toDelete.getKey()), true);
+            tables.remove(toDelete.getKey());
+            toDelete.getValue().onTableDelete();
+        }
+    }
+    
     @Override
     public XSSFSheetConditionalFormatting getSheetConditionalFormatting(){
         return new XSSFSheetConditionalFormatting(this);
@@ -4397,6 +4415,20 @@ public class XSSFSheet extends POIXMLDoc
     }
 
     /**
+     * called when a sheet is being deleted/removed from a workbook, to clean 
up relations and other document pieces tied to the sheet
+     */
+    protected void onSheetDelete() {
+        for (RelationPart part : getRelationParts()) {
+            if (part.getDocumentPart() instanceof XSSFTable) {
+                // call table delete
+                removeTable((XSSFTable) part.getDocumentPart());
+                continue;
+            }
+            removeRelation(part.getDocumentPart(), true);
+        }
+    }
+    
+    /**
      * Determine the OleObject which links shapes with embedded resources
      *
      * @param shapeId the shape id

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java?rev=1803317&r1=1803316&r2=1803317&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java Fri 
Jul 28 18:01:36 2017
@@ -29,6 +29,7 @@ import java.util.List;
 import java.util.Locale;
 
 import org.apache.poi.POIXMLDocumentPart;
+import org.apache.poi.POIXMLDocumentPart.RelationPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.ss.SpreadsheetVersion;
 import org.apache.poi.ss.usermodel.Cell;
@@ -623,4 +624,14 @@ public class XSSFTable extends POIXMLDoc
             return true;
         }
         return false;
-    }}
+    }
+    
+    /**
+     * Remove relations
+     */
+    protected void onTableDelete() {
+        for (RelationPart part : getRelationParts()) {
+            removeRelation(part.getDocumentPart(), true);
+        }
+    }
+}
\ No newline at end of file

Modified: 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=1803317&r1=1803316&r2=1803317&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java 
Fri Jul 28 18:01:36 2017
@@ -1366,6 +1366,11 @@ public class XSSFWorkbook extends POIXML
      * @param index the 0-based index of the sheet to delete
      */
     private void onSheetDelete(int index) {
+        // remove all sheet relations
+        final XSSFSheet sheet = getSheetAt(index);
+
+        sheet.onSheetDelete();
+        
         //delete the CTSheet reference from workbook.xml
         workbook.getSheets().removeSheet(index);
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to