]> source.dussan.org Git - poi.git/commitdiff
add removeTable method in cell
authorSayi <sayi@apache.org>
Mon, 10 Aug 2020 12:39:21 +0000 (12:39 +0000)
committerSayi <sayi@apache.org>
Mon, 10 Aug 2020 12:39:21 +0000 (12:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880739 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java

index 257b6b01461a90d8d37dc605db483b28c367decb..1f8f5f4902d71748f1dccec6ab35461c5e5d5c7b 100644 (file)
@@ -421,6 +421,18 @@ public class XWPFTableCell implements IBody, ICell {
         tables.add(i, table);
     }
 
+    /**
+     * removes a table of this table cell
+     *
+     * @param pos The position in the list of tables, 0-based
+     */
+    public void removeTable(int pos) {
+        XWPFTable removedTable = tables.get(pos);
+        tables.remove(pos);
+        ctTc.removeTbl(pos);
+        bodyElements.remove(removedTable);
+    }
+
     public String getText() {
         StringBuilder text = new StringBuilder();
         for (XWPFParagraph p : paragraphs) {
index 34c0cdae780f345bd5aaa76d85be16f3f200077f..cc6434e841a161ebd24e6f12fe0f53c7cc4841af 100644 (file)
@@ -28,6 +28,7 @@ import org.junit.Test;
 
 import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
+import org.apache.xmlbeans.XmlCursor;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
@@ -214,6 +215,31 @@ public class TestXWPFTableCell {
         doc.close();
     }
 
+    @Test
+    public void testRemoveTable() throws Exception {
+        XWPFDocument doc = new XWPFDocument();
+        XWPFTable table = doc.createTable();
+        XWPFTableRow tr = table.createRow();
+        XWPFTableCell cell = tr.addNewTableCell();
+
+        // cell have at least one paragraph by default
+        XWPFParagraph p0 = cell.getParagraphArray(0);
+        XmlCursor newCursor = p0.getCTP().newCursor();
+        cell.insertNewTbl(newCursor);
+        newCursor.dispose();
+
+        assertEquals(1, cell.getTables().size());
+        assertEquals(2, cell.getBodyElements().size());
+
+        // remove table
+        cell.removeTable(0);
+        assertEquals(0, cell.getTables().size());
+        assertEquals(1, cell.getBodyElements().size());
+        assertEquals(p0, cell.getBodyElements().get(0));
+
+        doc.close();
+    }
+
     @Test
     public void testBug63624() throws Exception {
         XWPFDocument doc = new XWPFDocument();