【问题标题】:LibreOffice Calc: Access a user selected range in a macroLibreOffice Calc:访问宏中用户选择的范围
【发布时间】:2013-02-06 08:01:45
【问题描述】:

我希望将 MS Excel 电子表格转换为 LibreOffice Calc 电子表格。

Excel 文件包含一个 VBA 宏,它遍历用户选择的一系列单元格,Visual Basic 代码如下所示:

For Each Value In Selection
    ' Manipulate Value
Next Value

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

所以,我的问题是:如何在 Libre Basic 中重现这一点,访问用户选择的单元格范围?

【问题讨论】:

    标签: libreoffice libreoffice-basic


    【解决方案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
    

    如果要操作单元格的值,请使用二维数组

    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
    

    【讨论】:

    • 与 3 行 VBA 宏相比,这看起来太复杂了!哎哟。
    • @remi 请注意,示例 VBA 宏有一个注释行“Manipulate Value”:) 如果我们真的想做某事,该行很容易增长。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多