Bug 63624

Summary: XWPFTableCell.setText() returns empty when no paragraph is created
Product: POI Reporter: canbingzt
Component: XWPFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: All   
Bug Depends on: 66988    
Bug Blocks:    

Description canbingzt 2019-08-02 03:03:19 UTC
If the cell has no paragraph, call XWPFTableCell.setText() , then call XWPFTableCell.getText(), the result is empty.
Comment 1 Dominik Stadler 2019-11-17 12:19:55 UTC
Can you provide some self-sufficient source code which reproduces this problem so it is easier for others to take a look?
Comment 2 canbingzt 2019-11-18 01:36:58 UTC
(In reply to Dominik Stadler from comment #1)
> Can you provide some self-sufficient source code which reproduces this
> problem so it is easier for others to take a look?

XWPFDocument doc = new XWPFDocument();
XWPFTable table = doc.createTable(1, 1);
XWPFTableRow row = table.getRow(0);
XWPFTableCell cell = row.getCell(0);

String expected = "this must not be empty";
cell.setText(expected);
String actual = cell.getText(); //actual is empty
Assert.assertEquals(expected, actual);


//under code work well
XWPFParagraph p;
if (cell.getParagraphs() == null || cell.getParagraphs().size() == 0) {
    p = cell.addParagraph();
} else {
    p = cell.getParagraphArray(0);
}
p.createRun().setText(text, 0);
Comment 3 iseletkov 2020-04-06 06:41:00 UTC
Method setText() does not add new paragraph to "paragraphs" list, while getText() uses info from "paragraphs" only.

public void setText(String text) {
        CTP ctP = (ctTc.sizeOfPArray() == 0) ? ctTc.addNewP() : ctTc.getPArray(0);
        XWPFParagraph par = new XWPFParagraph(ctP, this);
        par.createRun().setText(text);
    }

public String getText() {
        StringBuilder text = new StringBuilder();
        for (XWPFParagraph p : paragraphs) {
            text.append(p.getText());
        }
        return text.toString();
    } 

I think new line is needed in line 433:
paragraphs.add(par);
Comment 4 PJ Fanning 2020-04-06 07:34:57 UTC
Hi iseletkov,

Alain Bearez appears to have added a fix on March 27th. Could you try the latest POI code?
Comment 5 Dominik Stadler 2020-05-16 17:32:50 UTC
I did a quick test and it seems to be fixed now, therefore closing this for now.
Comment 6 PJ Fanning 2023-08-18 13:07:37 UTC
https://bz.apache.org/bugzilla/show_bug.cgi?id=66988 changes the behaviour to make setText replace the text and adds a new appendText to append.

66988 will be part of POI 5.2.4.