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;
@ -141,6 +142,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
_table.getTrArray(row).setH(Units.toEMU(height));
}
@Override
public Iterator<XSLFTableRow> iterator(){
return _rows.iterator();
}
@ -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();

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