Add, insert and remove columns on XSLFTable

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870486 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alain Béarez 2019-11-27 04:54:34 +00:00
parent ede9369bbc
commit 557b67cb24
2 changed files with 68 additions and 22 deletions

View File

@ -39,6 +39,7 @@ import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCol;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrameNonVisual;
@ -62,7 +63,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
if (!xc.toChild(XSLFRelation.NS_DRAWINGML, "tbl")) {
throw new IllegalStateException("a:tbl element was not found in\n " + god);
}
XmlObject xo = xc.getObject();
// Pesky XmlBeans bug - see Bugzilla #49934
// it never happens when using the full ooxml-schemas jar but may happen with the abridged poi-ooxml-schemas
@ -104,7 +105,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
// cell can be potentially empty ...
return cells.get(col);
}
@Internal
public CTTable getCTTable(){
return _table;
@ -135,12 +136,13 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
public double getRowHeight(int row) {
return Units.toPoints(_table.getTrArray(row).getH());
}
@Override
public void setRowHeight(int row, double height) {
_table.getTrArray(row).setH(Units.toEMU(height));
}
@Override
public Iterator<XSLFTableRow> iterator(){
return _rows.iterator();
}
@ -153,7 +155,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
CTTableRow tr = _table.addNewTr();
XSLFTableRow row = new XSLFTableRow(tr, this);
// default height is 20 points
row.setHeight(20.0);
row.setHeight(20.0);
_rows.add(row);
updateRowColIndexes();
return row;
@ -169,6 +171,42 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
updateRowColIndexes();
}
/**
* Add a new column at the end of the table.
* @since POI 4.1.2
*/
public void addColumn() {
long width = _table.getTblGrid().getGridColArray(_table.getTblGrid().sizeOfGridColArray() - 1).getW();
CTTableCol col = _table.getTblGrid().addNewGridCol();
col.setW(width);
updateRowColIndexes();
}
/**
* Insert a new column at the given index.
* @param colIdx the column index.
* @since POI 4.1.2
*/
public void insertColumn(int colIdx) {
if (_table.getTblGrid().sizeOfGridColArray() < colIdx) {
throw new IndexOutOfBoundsException("Cannot insert column at " + colIdx + "; table has only " + _table.getTblGrid().sizeOfGridColArray() + "columns.");
}
long width = _table.getTblGrid().getGridColArray(colIdx).getW();
CTTableCol col = _table.getTblGrid().insertNewGridCol(colIdx);
col.setW(width);
updateRowColIndexes();
}
/**
* Remove the column at the given index.
* @param colIdx the column index.
* @since POI 4.1.2
*/
public void removeColumn(int colIdx) {
_table.getTblGrid().removeGridCol(colIdx);
updateRowColIndexes();
}
static CTGraphicalObjectFrame prototype(int shapeId){
CTGraphicalObjectFrame frame = CTGraphicalObjectFrame.Factory.newInstance();
CTGraphicalObjectFrameNonVisual nvGr = frame.addNewNvGraphicFramePr();
@ -184,12 +222,12 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
XmlCursor grCur = gr.newCursor();
grCur.toNextToken();
grCur.beginElement(new QName(XSLFRelation.NS_DRAWINGML, "tbl"));
CTTable tbl = CTTable.Factory.newInstance();
tbl.addNewTblPr();
tbl.addNewTblGrid();
XmlCursor tblCur = tbl.newCursor();
tblCur.moveXmlContents(grCur);
tblCur.dispose();
grCur.dispose();
@ -248,12 +286,12 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
}
}
}
/**
* Get assigned TableStyle
*
* @return the assigned TableStyle
*
*
* @since POI 3.15-beta2
*/
protected XSLFTableStyle getTableStyle() {
@ -262,7 +300,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
if (!tab.isSetTblPr() || !tab.getTblPr().isSetTableStyleId()) {
return null;
}
String styleId = tab.getTblPr().getTableStyleId();
XSLFTableStyles styles = getSheet().getSlideShow().getTableStyles();
for (XSLFTableStyle style : styles.getStyles()) {
@ -272,7 +310,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
}
return null;
}
/* package */ void updateRowColIndexes() {
int rowIdx = 0;
for (XSLFTableRow xr : this) {
@ -304,7 +342,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
Rectangle2D tblAnc = getAnchor();
DrawFactory df = DrawFactory.getInstance(null);
double nextY = tblAnc.getY();
double nextX = tblAnc.getX();
@ -323,7 +361,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
}
rowHeights[row] = Math.max(rowHeights[row],maxHeight);
}
// #2 pass - init properties
for (int row=0; row<rows; row++) {
nextX = tblAnc.getX();
@ -337,7 +375,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
}
nextY += rowHeights[row]+DrawTableShape.borderSize;
}
// #3 pass - update merge info
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {

View File

@ -68,6 +68,14 @@ public class TestXSLFTable {
tab.setColumnWidth(1, 60);
tab.setColumnWidth(2, 60);
tab.insertColumn(0);
assertEquals(tab.getColumnWidth(1), tab.getColumnWidth(0), 0.00001);
tab.addColumn();
assertEquals(tab.getColumnWidth(tab.getNumberOfColumns() - 2), tab.getColumnWidth(tab.getNumberOfColumns() - 1), 0.00001);
tab.removeColumn(0);
tab.removeColumn(tab.getNumberOfColumns() - 1);
assertEquals(data[0].length, tab.getNumberOfColumns());
int startRow = rowIdx-1;
XSLFTableRow row = tab.getRows().get(0);
@ -176,7 +184,7 @@ public class TestXSLFTable {
assertEquals("A1", cells1.get(0).getText());
assertEquals("B1", cells1.get(1).getText());
assertEquals("C1", cells1.get(2).getText());
ppt.close();
}
@ -244,7 +252,7 @@ public class TestXSLFTable {
assertEquals(VerticalAlignment.MIDDLE, cell1.getVerticalAlignment());
cell1.setVerticalAlignment(null);
assertEquals(VerticalAlignment.TOP, cell1.getVerticalAlignment());
XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt1);
ppt1.close();
@ -254,25 +262,25 @@ public class TestXSLFTable {
assertEquals(1, tbl.getNumberOfRows());
assertEquals("POI", tbl.getCell(0, 0).getText());
assertEquals("Apache", tbl.getCell(0, 1).getText());
ppt2.close();
}
@Test
public void removeTable() throws IOException {
XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
XSLFSlide sl = ss.getSlides().get(0);
XSLFTable tab = (XSLFTable)sl.getShapes().get(4);
sl.removeShape(tab);
XMLSlideShow ss2 = XSLFTestDataSamples.writeOutAndReadBack(ss);
ss.close();
sl = ss2.getSlides().get(0);
for (XSLFShape s : sl.getShapes()) {
assertFalse(s instanceof XSLFTable);
}
ss2.close();
}
@ -292,7 +300,7 @@ public class TestXSLFTable {
// so we use something more reliable
assertTrue(tc0.getTextHeight() > 50);
assertEquals(0, tc0.getLineWidth(), 0);
ppt.close();
}