[bug-66988] Fully replace content of XWPFTableCell on setText. Thanks to Anton Oellerer. This closes #503

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1911749 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2023-08-18 08:48:18 +00:00
parent 1c1cff4357
commit 1bdfdcc793
3 changed files with 32 additions and 0 deletions

View File

@ -421,7 +421,28 @@ public class XWPFTableCell implements IBody, ICell {
return text.toString();
}
/**
* Set the text of the cell to the passed string, replacing previous content. Up until POI 5.2.3, this method appended the text, which is now done
* by {@link XWPFTableCell#appendText(String)}.
*
* @param text The text to replace the cell content with
*/
public void setText(String text) {
XWPFParagraph par = paragraphs.isEmpty() ? addParagraph() : paragraphs.get(0);
while (!par.runsIsEmpty()) {
par.removeRun(0);
}
par.createRun().setText(text);
}
/**
* Append the passed string to the cell content.
* This was the behaviour of {@link XWPFTableCell#setText(String)} before POI 5.2.4
*
* @param text The text to append to the cells content.
* @since POI 5.2.4
*/
public void appendText(String text) {
XWPFParagraph par = paragraphs.isEmpty() ? addParagraph() : paragraphs.get(0);
par.createRun().setText(text);
}

View File

@ -329,6 +329,17 @@ class TestXWPFBugs {
}
}
@Test
void bug66988() throws IOException {
try (XWPFDocument document = XWPFTestDataSamples.openSampleDocument("Bug66988.docx")) {
XWPFTableCell cell = document.getTableArray(0).getRow(0).getCell(0);
cell.appendText("World");
assertEquals("HelloWorld", cell.getText());
cell.setText("FooBar");
assertEquals("FooBar", cell.getText());
}
}
private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering, int id){
// create a numbering scheme
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();

Binary file not shown.