From 353293f244546ab5f514b4900bade037545d3bb6 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 25 Dec 2020 09:50:50 +0000 Subject: [PATCH] [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 --- .../apache/poi/xwpf/usermodel/XWPFTable.java | 11 +-- .../apache/poi/xwpf/usermodel/TestColumn.java | 82 ++++++++++++++++++ test-data/document/TestTableColumns.docx | Bin 0 -> 12672 bytes 3 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java create mode 100644 test-data/document/TestTableColumns.docx 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 0000000000000000000000000000000000000000..a474a694777608ba8fdfc873431fab7eccd3077d GIT binary patch literal 12672 zcmeHt1zR1-*7d=H6C}7>a3{FC1q<%(?(XgmIasjZdeGn=+}$<7od5~&oy?uNGn1L` z7reKhr>ncGYwfP?-cq${E6PGazXH4fzySaNQou&xw2c-706+o*0AK*%Ahkv9?OaUl zT=Z2u9Za2d89i)mNb;c}sXqW9!R`OI{U81U^(iBEJc{?$#`-FX9*TgTUridWzlNk>5*@75mNWSP;z8?CVA4!IZMKI zGe!3tFHIFWsvUm*UawdCdz%L!HMFgW#be%K8!2=D*6eYZT$*M$I9d}kY69;?;$1|> zSQo|~t=R2eF`^|I&BUxR1*jm$QFY;4h9BF9_bhYsR#MvDux0$>9btr@#&P`+%mi!G z*%%Vyd)Di8`&15-n^|M6Xm057Y?zrSvl5Le5b)01rVhOdI*a=wL+Ro3;3Jv7`#HcTYa>ItGXDq=ClV~7habD% z>a3QMKAJT6bnaA@;XDz*u`dyCVx<=n47yO;c-p_{0_-98kZI zMko2O5IuPk>u~o9_gfN8rR-gJG)I7^H)eBtF;=*OeqZ2=> zZn2JAxMout4$*RaSs)!@=g6fptfsa725UclN0K6ddn#s$8f#mhdkObA^HoJR9Vq zY{Sfp^Sig}AUt_?``)E-e4mFoe9vf~N~KR^intiw!ShU?I{K2FtbH&RE}~L`7~wky z{%c$@L+iW3f+cR9Sx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