LibreOffice的计算值:在宏接入用户选择范围(LibreOffice Calc: Acces

2019-07-19 08:31发布

我期待到MS Excel电子表格转换成的LibreOffice Calc电子表格。

Excel文件包含VBA宏,在一定范围内由用户选择细胞的迭代,Visual Basic代码只是看起来像这样:

For Each Value In Selection
    ' Manipulate Value
Next Value

Selection包含在工作表的单元由用户手动选择。

所以,我的问题是:我怎么能重现此,accesing细胞的用户选择范围,在基本自由报?

Answer 1:

如果你想使用该单元的值

Sub Learn
    Dim myController as Object, myRange as Object
    Dim Tmp as Integer
    Dim i, j

    myController = ThisComponent.getCurrentController()
    myRange = myController.getSelection().getDataArray()

    'additional info that we can use
    print myRange.AbsoluteName
    print myRange.RangeAddress.StartColumn
    print myRange.RangeAddress.StartRow

    Tmp = 0
    For Each i in myRange
        For Each j in i
            Tmp = Tmp + j
        Next j
    Next i
    print Tmp
End Sub

如果你要处理的单元格的值,用一个2维数组

Sub Learn2
    Dim myController as Object, myRange as Object
    Dim newRange(0, 0)
    Dim Tmp as Integer
    Dim i, j, col, row

    myController = ThisComponent.getCurrentController()
    myRange = myController.getSelection().getDataArray()
    Redim newRange(UBound(myRange, 1), UBound(myRange(0), 1))
    row = 0
    col = 0
    For row = 0 to UBound(myRange, 1)
        For col = 0 to UBound(myRange(0), 1)
            newRange(row, col) = myRange(row)(col) * 2
        Next col
    Next row
    myController.getSelection().setDataArray(newRange)
End Sub


文章来源: LibreOffice Calc: Access a user selected range in a macro