Browse Source

[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
tags/REL_5_0_0
PJ Fanning 3 years ago
parent
commit
353293f244

+ 6
- 5
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java View File

@@ -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();
}
}


+ 82
- 0
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestColumn.java View File

@@ -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);
}
}
}

BIN
test-data/document/TestTableColumns.docx View File


Loading…
Cancel
Save