From 832958203663cb7ba4b5b01f049805e0c83f30f3 Mon Sep 17 00:00:00 2001 From: Sayi Date: Mon, 10 Aug 2020 12:39:21 +0000 Subject: [PATCH] add removeTable method in cell git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880739 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/XWPFTableCell.java | 12 +++++++++ .../poi/xwpf/usermodel/TestXWPFTableCell.java | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java index 257b6b0146..1f8f5f4902 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java @@ -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) { diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java index 34c0cdae78..cc6434e841 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java @@ -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(); -- 2.39.5