You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2020/12/25 09:50:50 UTC

svn commit: r1884794 - in /poi/trunk: src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java test-data/document/TestTableColumns.docx

Author: fanningpj
Date: Fri Dec 25 09:50:50 2020
New Revision: 1884794

URL: http://svn.apache.org/viewvc?rev=1884794&view=rev
Log:
[bug-65023] add col bugfix. Thanks to Paula Muldoon, This closes #212

Added:
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java   (with props)
    poi/trunk/test-data/document/TestTableColumns.docx   (with props)
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java?rev=1884794&r1=1884793&r2=1884794&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java Fri Dec 25 09:50:50 2020
@@ -235,15 +235,16 @@ public class XWPFTable implements IBodyE
     }
 
     /**
-     * add a new column for each row in this table
+     * Add a new cell at the end of each row in this table, creating a new column.
+     * If rows have different numbers of columns, will still append a cell to each row.
+     * Currently does not match the width of existing columns.
      */
     public void addNewCol() {
-        if (ctTbl.sizeOfTrArray() == 0) {
+        if (tableRows.size() == 0) {
             createRow();
         }
-        for (int i = 0; i < ctTbl.sizeOfTrArray(); i++) {
-            XWPFTableRow tabRow = new XWPFTableRow(ctTbl.getTrArray(i), this);
-            tabRow.createCell();
+        for (int i = 0; i < tableRows.size(); i++) {
+            tableRows.get(i).createCell();
         }
     }
 

Added: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java?rev=1884794&view=auto
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java (added)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java Fri Dec 25 09:50:50 2020
@@ -0,0 +1,82 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xwpf.usermodel;
+
+import org.apache.poi.xwpf.XWPFTestDataSamples;
+import org.junit.jupiter.api.Test;
+import java.io.IOException;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class TestColumn {
+    @Test
+    public void testAddNewColWithCorrectAmountOfColumns() throws IOException {
+        XWPFDocument doc = new XWPFDocument();
+        XWPFTable table = doc.createTable(2, 4);
+        table.addNewCol();
+
+        int expectedNumberOfColumns = 5;
+        for (int i = 0; i < table.tableRows.size(); i++) {
+            assertEquals(expectedNumberOfColumns, table.tableRows.get(i).getTableCells().size());
+        }
+        doc.close();
+    }
+
+    @Test
+    public void testAddNewColWithEmptyTable() throws IOException {
+        XWPFDocument doc = new XWPFDocument();
+        XWPFTable table = doc.createTable(0, 0);
+        table.removeRow(0);
+        table.addNewCol();
+
+        int expectedNumberOfColumnsInRow1 = 1;
+        int actualNumberOfColumnsInRow1 = table.tableRows.get(0).getTableCells().size();
+        assertEquals(expectedNumberOfColumnsInRow1, actualNumberOfColumnsInRow1);
+        doc.close();
+    }
+
+    @Test
+    public void testAddNewColWithDocx() throws Exception {
+        try (XWPFDocument doc = XWPFTestDataSamples
+                .openSampleDocument("TestTableColumns.docx")) {
+            XWPFTable table = doc.getTables().get(0);
+            table.addNewCol();
+
+            int expectedNumberOfColumns = 5;
+            for (int i = 0; i < table.tableRows.size(); i++) {
+                assertEquals(expectedNumberOfColumns, table.tableRows.get(i).getTableCells().size());
+            }
+        }
+    }
+
+    @Test
+    public void testAddNewColWhenRowsHaveDifferentNumbersOfColumnsWithDocx() throws Exception {
+        try (XWPFDocument doc = XWPFTestDataSamples
+                .openSampleDocument("TestTableColumns.docx")) {
+            XWPFTable table = doc.getTables().get(1);
+            table.addNewCol();
+
+            int expectedNumberOfColumnsInRow1 = 5;
+            int actualNumberOfColumnsInRow1 = table.tableRows.get(0).getTableCells().size();
+            assertEquals(expectedNumberOfColumnsInRow1, actualNumberOfColumnsInRow1);
+
+            int expectedNumberOfColumnsInRow2 = 4;
+            int actualNumberOfColumnsInRow2 = table.tableRows.get(1).getTableCells().size();
+            assertEquals(expectedNumberOfColumnsInRow2, actualNumberOfColumnsInRow2);
+        }
+    }
+}

Propchange: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: poi/trunk/test-data/document/TestTableColumns.docx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/document/TestTableColumns.docx?rev=1884794&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/document/TestTableColumns.docx
------------------------------------------------------------------------------
--- svn:mime-type (added)
+++ svn:mime-type Fri Dec 25 09:50:50 2020
@@ -0,0 +1 @@
+application/vnd.openxmlformats-officedocument.wordprocessingml.document



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