From c67d2605fd4cf3ce0392530613823e8d615be0ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alain=20B=C3=A9arez?= Date: Tue, 31 Mar 2020 00:47:45 +0000 Subject: [PATCH] Insert a new row in XSLFTable git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1875901 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xslf/usermodel/XSLFTable.java | 42 ++++++-- .../poi/xslf/usermodel/XSLFTableRow.java | 3 + .../poi/xslf/usermodel/TestXSLFTable.java | 102 +++++++++--------- 3 files changed, 89 insertions(+), 58 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java index 5f64a56260..a18d38b0f7 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java @@ -153,19 +153,41 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable { * @since POI 4.1.2 */ public void removeCell(int colIdx){ + if (_row.sizeOfTcArray() < colIdx) { + throw new IndexOutOfBoundsException("Cannot remove cell at " + colIdx + "; row has only " + _row.sizeOfTcArray() + "columns."); + } _row.removeTc(colIdx); _cells.remove(colIdx); _table.updateRowColIndexes(); diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java index e0ca3c671b..99f665493f 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java @@ -55,61 +55,67 @@ public class TestXSLFTable { XMLSlideShow ppt = new XMLSlideShow(); int rowIdx=1; + + XSLFSlide slide = ppt.createSlide(); + // a red bordered box in the background, to show/verify the table dimensions + XSLFAutoShape as = slide.createAutoShape(); + as.setShapeType(ShapeType.RECT); + as.setStrokeStyle(Color.RED, 2., StrokeStyle.LineDash.LG_DASH); + + XSLFTable tab = slide.createTable(1, data[0].length); + tab.setAnchor(new Rectangle2D.Double(50,50,0,0)); + tab.setColumnWidth(0, 60); + tab.setColumnWidth(1, 60); + tab.setColumnWidth(2, 60); + + tab.insertColumn(0); + assertEquals(tab.getColumnWidth(1), tab.getColumnWidth(0), 0.00001); + assertNotNull(tab.getCell(0, 0).getTextBody()); + tab.addColumn(); + XSLFTableCell cell = tab.getCell(0, data[0].length + 1); + assertEquals(1, cell.getTextBody().getParagraphs().size()); + assertEquals("", cell.getTextBody().getParagraph(0).getText()); + assertEquals(tab.getColumnWidth(tab.getNumberOfColumns() - 2), tab.getColumnWidth(tab.getNumberOfColumns() - 1), 0.00001); + assertNotNull(tab.getCell(0, tab.getNumberOfColumns() - 1).getTextBody()); + tab.removeColumn(0); + tab.removeColumn(tab.getNumberOfColumns() - 1); + assertEquals(data[0].length, tab.getNumberOfColumns()); + + int startRow = rowIdx-1; + + XSLFTableRow row = tab.getRows().get(0); + for (int colIdx=0; colIdx maxHeight) { - tab.removeRow(rowIdx-startRow); - break; - } - rowIdx++; + row = tab.insertRow(rowIdx); + for (int col=0; col maxHeight) { + tab.removeRow(rowIdx-startRow); + break; + } + rowIdx += 2; } + as.setAnchor(tab.getAnchor()); + File fileOut = TempFile.createTempFile("tabtest", ".pptx"); try (FileOutputStream fos = new FileOutputStream(fileOut)) { ppt.write(fos);