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

View File

@ -51,6 +51,7 @@ public class XSLFTableRow implements Iterable<XSLFTableCell> {
return _row; return _row;
} }
@Override
public Iterator<XSLFTableCell> iterator(){ public Iterator<XSLFTableCell> iterator(){
return _cells.iterator(); return _cells.iterator();
} }
@ -74,12 +75,41 @@ public class XSLFTableRow implements Iterable<XSLFTableCell> {
_cells.add(cell); _cells.add(cell);
if(_table.getNumberOfColumns() < _row.sizeOfTcArray()) { if(_table.getNumberOfColumns() < _row.sizeOfTcArray()) {
_table.getCTTable().getTblGrid().addNewGridCol().setW(Units.toEMU(100.0)); _table.getCTTable().getTblGrid().addNewGridCol().setW(Units.toEMU(100.0));
} }
_table.updateRowColIndexes(); _table.updateRowColIndexes();
return cell; 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. * Merge cells of a table row, inclusive.
* Indices are 0-based. * Indices are 0-based.

View File

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