bug 58191: Support merge cells within a table row; patch from Mark Olesen

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760217 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-09-11 03:21:33 +00:00
parent 050010094f
commit 5409095923
2 changed files with 45 additions and 3 deletions

View File

@ -32,9 +32,9 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
* Represents a table in a .pptx presentation * Represents a table in a .pptx presentation
*/ */
public class XSLFTableRow implements Iterable<XSLFTableCell> { public class XSLFTableRow implements Iterable<XSLFTableCell> {
private CTTableRow _row; private final CTTableRow _row;
private List<XSLFTableCell> _cells; private final List<XSLFTableCell> _cells;
private XSLFTable _table; private final XSLFTable _table;
/*package*/ XSLFTableRow(CTTableRow row, XSLFTable table){ /*package*/ XSLFTableRow(CTTableRow row, XSLFTable table){
_row = row; _row = row;
@ -78,6 +78,29 @@ public class XSLFTableRow implements Iterable<XSLFTableCell> {
_table.updateRowColIndexes(); _table.updateRowColIndexes();
return cell; return cell;
} }
/**
* Merge cells of a table row, inclusive.
* Indices are 0-based.
*
* @param firstCol 0-based index of first column to merge, inclusive
* @param lastCol 0-based index of last column to merge, inclusive
*/
public void mergeCells(int firstCol, int lastCol)
{
if (firstCol >= lastCol) {
throw new IllegalArgumentException(
"Cannot merge, first column >= last column : "
+ firstCol + " >= " + lastCol
);
}
final int colSpan = (lastCol - firstCol) + 1;
_cells.get(firstCol).setGridSpan(colSpan);
for (final XSLFTableCell cell : _cells.subList(firstCol+1, lastCol+1)) {
cell.setHMerge(true);
}
}
} }

View File

@ -17,9 +17,11 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -103,6 +105,23 @@ public class TestXSLFTableRow {
assertTrue(tc.getTcPr().getLnR().isSetNoFill()); assertTrue(tc.getTcPr().getLnR().isSetNoFill());
} }
@Test
public void mergeCells() {
try {
row.mergeCells(0, 0);
fail("expected IllegalArgumentException when merging fewer than 2 columns");
} catch (final IllegalArgumentException e) {
// expected
}
row.mergeCells(0, 1);
List<XSLFTableCell> cells = row.getCells();
//the top-left cell of a merged region is not regarded as merged
assertFalse("top-left cell of merged region", cells.get(0).isMerged());
assertTrue("inside merged region", cells.get(1).isMerged());
assertFalse("outside merged region", cells.get(2).isMerged());
}
@Test @Test
public void getXmlObject() { public void getXmlObject() {
CTTableRow ctrow = row.getXmlObject(); CTTableRow ctrow = row.getXmlObject();