poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From abea...@apache.org
Subject svn commit: r1875901 - in /poi/trunk/src/ooxml: java/org/apache/poi/xslf/usermodel/XSLFTable.java java/org/apache/poi/xslf/usermodel/XSLFTableRow.java testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java
Date Tue, 31 Mar 2020 00:47:45 GMT
Author: abearez
Date: Tue Mar 31 00:47:45 2020
New Revision: 1875901

URL: http://svn.apache.org/viewvc?rev=1875901&view=rev
Log:
Insert a new row in XSLFTable

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java
    poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableRow.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java?rev=1875901&r1=1875900&r2=1875901&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java Tue Mar 31 00:47:45
2020
@@ -153,19 +153,41 @@ public class XSLFTable extends XSLFGraph
 
     public XSLFTableRow addRow(){
         CTTableRow tr = _table.addNewTr();
+        return initializeRow(tr);
+    }
+
+    private XSLFTableRow initializeRow(CTTableRow tr) {
         XSLFTableRow row = new XSLFTableRow(tr, this);
         // default height is 20 points
         row.setHeight(20.0);
         _rows.add(row);
-        updateRowColIndexes();
+        for (int i = 0;  i < getNumberOfColumns(); i++) {
+            row.addCell();
+        }
         return row;
     }
 
     /**
+     * Insert a new row at the given index.
+     * @param rowIdx the row index.
+     * @since POI 4.1.3
+     */
+    public XSLFTableRow insertRow(int rowIdx) {
+        if (getNumberOfRows() < rowIdx) {
+            throw new IndexOutOfBoundsException("Cannot insert row at " + rowIdx + "; table
has only " + getNumberOfRows() + "rows.");
+        }
+        CTTableRow tr = _table.insertNewTr(rowIdx);
+        return initializeRow(tr);
+    }
+
+    /**
      * Remove the row on the given index
      * @param rowIdx the row index
      */
     public void removeRow(int rowIdx) {
+        if (getNumberOfRows() < rowIdx) {
+            throw new IndexOutOfBoundsException("Cannot remove row at " + rowIdx + "; table
has only " + getNumberOfRows() + "rows.");
+        }
         _table.removeTr(rowIdx);
         _rows.remove(rowIdx);
         updateRowColIndexes();
@@ -176,14 +198,13 @@ public class XSLFTable extends XSLFGraph
      * @since POI 4.1.2
      */
     public void addColumn() {
-        long width = _table.getTblGrid().getGridColArray(_table.getTblGrid().sizeOfGridColArray()
- 1).getW();
+        long width = _table.getTblGrid().getGridColArray(getNumberOfColumns() - 1).getW();
         CTTableCol col = _table.getTblGrid().addNewGridCol();
         col.setW(width);
-        for(XSLFTableRow row : _rows) {
+        for (XSLFTableRow row : _rows) {
             XSLFTableCell cell = row.addCell();
             new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
         }
-        updateRowColIndexes();
     }
 
     /**
@@ -192,17 +213,16 @@ public class XSLFTable extends XSLFGraph
      * @since POI 4.1.2
      */
     public void insertColumn(int colIdx) {
-        if (_table.getTblGrid().sizeOfGridColArray() < colIdx) {
-            throw new IndexOutOfBoundsException("Cannot insert column at " + colIdx + ";
table has only " + _table.getTblGrid().sizeOfGridColArray() + "columns.");
+        if (getNumberOfColumns() < colIdx) {
+            throw new IndexOutOfBoundsException("Cannot insert column at " + colIdx + ";
table has only " + getNumberOfColumns() + "columns.");
         }
         long width = _table.getTblGrid().getGridColArray(colIdx).getW();
         CTTableCol col = _table.getTblGrid().insertNewGridCol(colIdx);
         col.setW(width);
-        for(XSLFTableRow row : _rows) {
+        for (XSLFTableRow row : _rows) {
             XSLFTableCell cell = row.insertCell(colIdx);
             new XDDFTextBody(cell, cell.getTextBody(true)).initialize();
         }
-        updateRowColIndexes();
     }
 
     /**
@@ -211,11 +231,13 @@ public class XSLFTable extends XSLFGraph
      * @since POI 4.1.2
      */
     public void removeColumn(int colIdx) {
+        if (getNumberOfColumns() < colIdx) {
+            throw new IndexOutOfBoundsException("Cannot remove column at " + colIdx + ";
table has only " + getNumberOfColumns() + "columns.");
+        }
         _table.getTblGrid().removeGridCol(colIdx);
-        for(XSLFTableRow row : _rows) {
+        for (XSLFTableRow row : _rows) {
             row.removeCell(colIdx);
         }
-        updateRowColIndexes();
     }
 
     static CTGraphicalObjectFrame prototype(int shapeId){

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableRow.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableRow.java?rev=1875901&r1=1875900&r2=1875901&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableRow.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTableRow.java Tue Mar 31 00:47:45
2020
@@ -105,6 +105,9 @@ public class XSLFTableRow implements Ite
      * @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();

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java?rev=1875901&r1=1875900&r2=1875901&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFTable.java Tue Mar
31 00:47:45 2020
@@ -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<data[0].length; colIdx++) {
+            XSLFTextRun tr = row.getCells().get(colIdx).setText(data[0][colIdx]);
+            tr.setFontSize(20.);
+            tr.setFontFamily("Arial");
+        }
+
         while (rowIdx<data.length) {
-            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<data[0].length; colIdx++) {
-                XSLFTextRun tr = row.getCells().get(colIdx).setText(data[0][colIdx]);
-                tr.setFontSize(20.);
+            row = tab.addRow();
+            for (int col=0; col<data[rowIdx].length; col++) {
+                XSLFTextRun tr = tab.getCell(rowIdx, col).setText(data[rowIdx][col]);
+                tr.setFontSize(15.);
                 tr.setFontFamily("Arial");
             }
-
-
-            while (rowIdx<data.length) {
-                row = tab.addRow();
-                for (int col=0; col<data[rowIdx].length; col++) {
-                    XSLFTextRun tr = row.addCell().setText(data[rowIdx][col]);
-                    tr.setFontSize(15.);
-                    tr.setFontFamily("Arial");
-                }
-                tab.updateCellAnchor();
-                if (tab.getAnchor().getHeight() > maxHeight) {
-                    tab.removeRow(rowIdx-startRow);
-                    break;
-                }
-                rowIdx++;
+            row = tab.insertRow(rowIdx);
+            for (int col=0; col<data[rowIdx].length; col++) {
+                XSLFTextRun tr = tab
+                        .getCell(rowIdx, col)
+                        .setText(
+                                data[rowIdx][col]);
+                tr.setFontSize(12.);
+                tr.setFontFamily("Arial");
             }
-
             tab.updateCellAnchor();
-            as.setAnchor(tab.getAnchor());
+            if (tab.getAnchor().getHeight() > 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);



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message