aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2020-12-25 09:50:50 +0000
committerPJ Fanning <fanningpj@apache.org>2020-12-25 09:50:50 +0000
commit353293f244546ab5f514b4900bade037545d3bb6 (patch)
tree8bea19ba63fbe9cf02236322a4d9f4535409a328
parenta2d728cbe6d9547f6327392ce8796d6826a3374e (diff)
downloadpoi-353293f244546ab5f514b4900bade037545d3bb6.tar.gz
poi-353293f244546ab5f514b4900bade037545d3bb6.zip
[bug-65023] add col bugfix. Thanks to Paula Muldoon, This closes #212
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884794 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java11
-rw-r--r--src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java82
-rw-r--r--test-data/document/TestTableColumns.docxbin0 -> 12672 bytes
3 files changed, 88 insertions, 5 deletions
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
index b8294c771a..e95057f342 100644
--- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
+++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
@@ -235,15 +235,16 @@ public class XWPFTable implements IBodyElement, ISDTContents {
}
/**
- * 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();
}
}
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java
new file mode 100644
index 0000000000..9354afd7d0
--- /dev/null
+++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java
@@ -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);
+ }
+ }
+}
diff --git a/test-data/document/TestTableColumns.docx b/test-data/document/TestTableColumns.docx
new file mode 100644
index 0000000000..a474a69477
--- /dev/null
+++ b/test-data/document/TestTableColumns.docx
Binary files differ