Browse Source

[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
tags/REL_5_2_4
PJ Fanning 8 months ago
parent
commit
1bdfdcc793

+ 21
- 0
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java 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);
}

+ 11
- 0
poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java 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();

BIN
test-data/document/Bug66988.docx View File


Loading…
Cancel
Save