问题
I have a code that was working perfectly fine until LibreOffice 5. But in LibreOffice 6 (both 32 and 64 bits) it stopped working.
public String getNextSentenceOO() {
while (moreParagraphsOO) {
while (moreSentencesOO) {
xSentenceCursor.gotoEndOfSentence(true);
textSentence = xSentenceCursor.getString();
xTextViewCursor.gotoRange(xSentenceCursor.getStart(), false);
xTextViewCursor.gotoRange(xSentenceCursor.getEnd(), true);
if (!textSentence.equals("")) {
return textSentence;
}
moreSentencesOO = xSentenceCursor.gotoNextSentence(false);
if (xSentenceCursor.isEndOfSentence() && !xSentenceCursor.isStartOfSentence()){
moreSentencesOO = false;
}
}
moreParagraphsOO = xParagraphCursor.gotoNextParagraph(false);
moreSentencesOO = xSentenceCursor.gotoStartOfSentence(false);
}
return null;
}
The problem arises when a blank line exists in the document. In that case, the instruction:
moreParagraphsOO = xParagraphCursor.gotoNextParagraph(false);
doesn't make the cursor advance to the next paragraph, it remains in the same place, so the function enters an infinite loop. Any ideas?
As I have said, this was working flawlessly in LibreOffice 4 and 5 (even in the last version of LO5). It stopped working in LO6.
回答1:
Instead of a paragraph cursor, enumerate paragraphs. From Listing 7.52 of Andrew Pitonyak's Macro Document:
oParEnum = ThisComponent.getText().createEnumeration()
Do While oParEnum.hasMoreElements()
oPar = oParEnum.nextElement()
Also, there is a note in Listing 7.65 that may be relevant to your question:
REM In this example, I assume that there is text after
REM the text section. If there is not, then this is
REM an infinite loop. You had better verify that
REM gotoNextParagraph does not return False.
Do While NOT IsEmpty(oCurs.TextSection)
oCurs.gotoNextParagraph(False)
Loop
来源:https://stackoverflow.com/questions/62637778/libreoffice-xparagraphcursor-stuck-when-blank-line