0

我知道可以在 localc 电子表格中手动编辑单元格文本并将其中的一些文本标记为粗体、斜体或其他。

我想用宏做同样的事情。我正在构建一个插入特定单元格的摘要字符串。摘要字符串包含多行,例如:

    Category1: item1, item2, item3
    Category2: item1, item2, ...
    CategoryN: item1, item2, ...

单元格中可能有 ~4 到 ~12 个类别行,具体取决于给定类别是否为空。

我必须做的功能正在工作,到目前为止正是我想要的。但现在我想像这样加粗单元格的“类别”部分:

    Category1: item1, item2, item3
    Category2: item1, item2, ...
    CategoryN: item1, item2, ...

可以在 LO Basic 中执行此操作吗?我知道在单元格中手动编辑是可能的,但这会破坏编写脚本的全部目的。

这是我到目前为止所拥有的,位于构建摘要字符串并将其插入到单元格对象(命名为“单元格”,很奇怪)的函数的底部:

newline = chr(10)
cell  = ThisComponent.Sheets.getByName("Summary").getCellRangeByName("Skills")



Dim next_nl as Integer
Dim next_colon as Integer
Dim TC as Object ' TextCursor object pointing into the cells text

TC = cell.createTextCursor

next_nl=1 ' start at first char of cell

While (next_nl > 0)
    TC.collapseToStart
    TC.gotoStart(false)  ' move to start of cell

    if (next_nl > 1) Then TC.goRight(next_nl, false) ' move to next_nl

    TC.goRight(0,true) ' begin selection

    next_colon = InStr(next_nl, cell.String, ":")

    If next_colon Then
        TC.goRight(next_colon, true) ' extend selection to next colon
        TC.CharWeight = com.sun.star.awt.FontWeight.BOLD ' bold the selection
        next_nl = InStr(next_colon, cell.String, newline) ' jump to the next LF
    Else
        next_nl = 0 ' no more colons to be found, finish up.'
    Endif

Wend

它部分有效。第一行有 Category 粗体和 items 普通文本。完美,到目前为止。

不幸的是,从第二行开始到倒数第二行中间的所有内容(嗯?什么?这很奇怪)都是完全粗体的。我怀疑Instr()混合格式的文本可能不太正确,字符数错误。

我尝试过的其他事情/想法:

  • 我试过搜索 CRchr(13)而不是 LF chr(10),但这根本不起作用。我期待它使用 MS-DOS CR 行尾,但它使用正确的 unix LF 行尾(即使您在行之间添加 CR,它也会将它们转换为 LF)。这可能是因为我在 Linux 上运行,所以当我开始工作时,我可能应该检测运行环境并为 linux、windows 或其他任何东西使用适当的行尾样式。

  • 我已经尝试移动Tc=cell.createTextCursorwhile循环内部,以防它需要在每次通过时重新初始化。没区别,结果一样。

    AFAICT,光标似乎没有“重置选择”类型的功能。

  • 也许有一些奇怪的变量范围问题,但我对 LO 中的变量范围知之甚少,所以不能说 - 我对 LO 宏很陌生,我主要用 sh、awk、perl 或 python 编程,最后一个我在 Basic 中写任何东西的时间可以追溯到 1980 年代。

  • 我什至不确定上述是否是解决问题的好方法,这只是在谷歌搜索文档时似乎相关的第一件事。如有必要,我非常愿意从一个更好的主意重新开始。

  • 我开始认为“技能”命名范围应该是每个类别一个单元格,而不是一个嵌入 LF 的大长字符串,然后我可以单独遍历它们。这样做需要我对电子表格的其他部分进行相当大的更改,所以我不想这样做,除非我必须这样做。

PS:我知道......我有点完美主义者。大写类别名称会起作用(事实上,它确实如此)但有点难看。将它们加粗会更好......这个摘要页面的目的是为了漂亮的展示。

4

1 回答 1

1

InStr()工作得很好,即使在 Windows 上。附近只有一个简单的错误goRight()

Sub BoldPartOfCell
    Dim next_nl As Integer  ' position of the next newline
    Dim next_colon As Integer
    Dim cursor As Object  ' TextCursor object pointing into the cell's text

    NEWLINE = Chr(10)  ' the LibreOffice line break character
    cell = ThisComponent.Sheets.getByName("Summary").getCellRangeByName("Skills")
    cursor = cell.createTextCursor
    next_nl = 1  ' start at first char of cell
    Do While True
        cursor.collapseToStart
        cursor.gotoStart(False)  ' move to start of cell
        if (next_nl > 1) Then cursor.goRight(next_nl, False) ' move to next_nl
        next_colon = InStr(next_nl, cell.String, ":")
        If next_colon > 0 Then
            ' extend selection to next colon
            cursor.goRight(next_colon - next_nl + 1, True)
            cursor.CharWeight = com.sun.star.awt.FontWeight.BOLD  ' bold the selection
            next_nl = InStr(next_colon, cell.String, NEWLINE)  ' jump to the next LF
        Else
            Exit Do
        End If
    Loop
End Sub
于 2017-05-08T17:29:18.753 回答