LibreOffice 段落光标在空行时卡住

Posted

技术标签:

【中文标题】LibreOffice 段落光标在空行时卡住【英文标题】:LibreOffice XParagraphCursor stuck when blank line 【发布时间】:2020-06-29 12:07:35 【问题描述】:

我的代码在 LibreOffice 5 之前运行良好。但在 LibreOffice 6(32 位和 64 位)中它停止工作。

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;

当文档中存在空行时会出现问题。在这种情况下,指令:

moreParagraphsOO = xParagraphCursor.gotoNextParagraph(false);

不使光标前进到下一段,它保持在同一个地方,所以函数进入一个无限循环。有什么想法吗?

正如我所说,这在 LibreOffice 4 和 5 中运行良好(即使在 LO5 的最新版本中也是如此)。它在 LO6 中停止工作。

【问题讨论】:

【参考方案1】:

枚举段落而不是段落光标。来自Andrew Pitonyak's Macro Document 的代码清单 7.52:

oParEnum = ThisComponent.getText().createEnumeration()
Do While oParEnum.hasMoreElements()
    oPar = oParEnum.nextElement()

此外,清单 7.65 中有一条注释可能与您的问题相关:

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

【讨论】:

以上是关于LibreOffice 段落光标在空行时卡住的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 python 宏获取 libreoffice 文档中段落的样式名称?

光标位于 youtube_player_iframe 顶部时鼠标滚动卡住

快速删除段落间多余的空行

LibreOffice Calc 上的 Python UNO,重新定位光标

将LibreOffice文档转换为豆瓣日记

4-14 标题 段落 文本格式化 链接 头部