LibreOffice Calc:在宏中访问用户选择的范围 [英] LibreOffice Calc: Access a user selected range in a macro

查看:195
本文介绍了LibreOffice Calc:在宏中访问用户选择的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking to convert a MS Excel Spreadsheet into a LibreOffice Calc Spreadsheet.

Excel文件包含一个VBA宏,该宏在用户选择的单元格范围内进行迭代,Visual Basic代码如下所示:

The Excel file contains a VBA Macro that iterates over a range of cells selected by the user, the Visual Basic code simply looking like this:

For Each Value In Selection
    ' Manipulate Value
Next Value

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

Where Selection contains the cells in the worksheet manually selected by the user.

因此,我的问题是:如何在Libre Basic中通过用户选择的单元格区域重现此内容?

So, my question is: how can I reproduce this, accesing a user selected range of cells, in Libre Basic?

推荐答案

如果要使用单元格的值

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

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

If you want to manipulate the cell's value, use a 2 dimensional array

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:在宏中访问用户选择的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆