Fix column addition and removal in XSLFTable

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871061 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alain Béarez 2019-12-08 22:23:17 +00:00
parent bb2ad49a2f
commit a56353b651
3 changed files with 42 additions and 8 deletions

View File

@ -179,8 +179,9 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
long width = _table.getTblGrid().getGridColArray(_table.getTblGrid().sizeOfGridColArray() - 1).getW();
CTTableCol col = _table.getTblGrid().addNewGridCol();
col.setW(width);
for(CTTableRow row : _table.getTrList()) {
row.addNewTc();
for(XSLFTableRow row : _rows) {
XSLFTableCell cell = row.addCell();
cell.getTextBody(true);
}
updateRowColIndexes();
}
@ -197,8 +198,9 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
long width = _table.getTblGrid().getGridColArray(colIdx).getW();
CTTableCol col = _table.getTblGrid().insertNewGridCol(colIdx);
col.setW(width);
for(CTTableRow row : _table.getTrList()) {
row.insertNewTc(colIdx);
for(XSLFTableRow row : _rows) {
XSLFTableCell cell = row.insertCell(colIdx);
cell.getTextBody(true);
}
updateRowColIndexes();
}
@ -210,8 +212,8 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
*/
public void removeColumn(int colIdx) {
_table.getTblGrid().removeGridCol(colIdx);
for(CTTableRow row : _table.getTrList()) {
row.removeTc(colIdx);
for(XSLFTableRow row : _rows) {
row.removeCell(colIdx);
}
updateRowColIndexes();
}

View File

@ -51,6 +51,7 @@ public class XSLFTableRow implements Iterable<XSLFTableCell> {
return _row;
}
@Override
public Iterator<XSLFTableCell> iterator(){
return _cells.iterator();
}
@ -80,6 +81,35 @@ public class XSLFTableRow implements Iterable<XSLFTableCell> {
return cell;
}
/**
* Insert a new cell at the given index.
* @param colIdx the column index.
* @since POI 4.1.2
*/
public XSLFTableCell insertCell(int colIdx){
CTTableCell c = _row.insertNewTc(colIdx);
c.set(XSLFTableCell.prototype());
XSLFTableCell cell = new XSLFTableCell(c, _table);
_cells.add(colIdx, cell);
if(_table.getNumberOfColumns() < _row.sizeOfTcArray()) {
_table.getCTTable().getTblGrid().insertNewGridCol(colIdx).setW(Units.toEMU(100.0));
}
_table.updateRowColIndexes();
return cell;
}
/**
* Remove the cell at the given index.
* @param colIdx the column index.
* @since POI 4.1.2
*/
public void removeCell(int colIdx){
_row.removeTc(colIdx);
_cells.remove(colIdx);
_table.updateRowColIndexes();
}
/**
* Merge cells of a table row, inclusive.
* Indices are 0-based.

View File

@ -70,9 +70,11 @@ public class TestXSLFTable {
tab.insertColumn(0);
assertEquals(tab.getColumnWidth(1), tab.getColumnWidth(0), 0.00001);
assertNotNull(tab.getCell(0, 0).getTextBody());
tab.addColumn();
tab.getCell(0, data[0].length + 1);
assertEquals(tab.getColumnWidth(tab.getNumberOfColumns() - 2), tab.getColumnWidth(tab.getNumberOfColumns() - 1), 0.00001);
assertNotNull(tab.getCell(0, tab.getNumberOfColumns() - 1).getTextBody());
tab.removeColumn(0);
tab.removeColumn(tab.getNumberOfColumns() - 1);
assertEquals(data[0].length, tab.getNumberOfColumns());