首页 文章

在Libreoffice Calc Basic中将单元格文本的一部分标记为粗体?

提问于
浏览
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中执行此操作吗?我知道在单元格中进行手动编辑是可能的,但这会破坏脚本编写的全部目的 .

这是我到目前为止,在函数的底部,构建汇总字符串并将其插入到单元格对象(名为“cell”,奇怪的是):

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

它部分有效 . 第一行的类别为粗体,项目为普通文本 . 完美,到目前为止 .

不幸的是,从第二行的开头到第二行的中间的所有内容(嗯?什么?'s weird) is entirely bold. I' m怀疑可能 Instr() 对于混合格式文本不能正常工作,会导致字符计数错误 .

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

  • 我已经尝试搜索CR chr(13) 而不是LF chr(10) 但是在Linux上没有运行't work at all. I was expecting it to use MS-DOS CR line-endings but it uses proper unix LF line-endings (even if you add CRs between lines, it converts them to LF). This may be because i',所以当我开始工作时,我应该检测运行环境并使用适当的行结束样式用于linux,windows或其他 .

  • 我尝试在while循环中移动 Tc=cell.createTextCursor ,以防每次传递需要重新初始化 . 两种方式都没有区别,结果相同 .

AFAICT,似乎没有光标的“重置选择”类型函数 .

  • 也许有一些奇怪的变量范围问题,但我对LO中的变量范围几乎一无所知,所以无法分辨 - 我对LO宏很新,我主要用sh,awk,perl或python编程,上次我在Basic写的东西早在20世纪80年代 .

  • 我甚至不确定以上是解决问题的好方法,这只是谷歌搜索文档时看起来很重要的第一件事 . 如果有必要的话,我更愿意重新开始 .

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

PS:我知道......我是一个完美主义者 . 大写类别名称会起作用(事实上,确实如此),但有点难看 . 对它们进行粗体结构会更好......而本摘要页面的目的是为了漂亮的演示 .

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
    

相关问题