add removeTable method in cell

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880739 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sayi 2020-08-10 12:39:21 +00:00
parent a8d90aba91
commit 8329582036
2 changed files with 38 additions and 0 deletions

View 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) {

View 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();