【问题标题】:Ubound(Array()) doesn't work to know how many elements has the Array with method .getDataArray()Ubound(Array()) 无法使用 .getDataArray() 方法知道数组中有多少元素
【发布时间】:2020-06-09 08:42:52
【问题描述】:

我是使用 OOobasic 构建 LibreOffice 宏的新手。

我正在测试方法 .getDataArray() 正如您在示例中看到的那样,数组被加载了值,但后来,我想用这个数组进行迭代,直到用 UBound() 结束数组,但 UBound 始终为 0,我不知道为什么。 如您所见, getDataArray 获取范围的值。就我而言,我的范围是从 A1 到 AH1 的简单行。

Sub TestArray2
Dim oSheet as object
Dim Simple_Row_array() As Variant
Dim SimpleRow 'Como no sabemos lo grande que es lo redimensionamos despues.
Dim Columnas as Long

oSheet = ThisComponent.Sheets.getByName("Concedidos")
Dim oRange As Object  : oRange = oSheet.getCellRangebyName( "A1:AH1" )

Columnas = oRange.Columns.getCount() - 1'Get the number of columns. getColumn, getRow existe.
Redim Preserve Simple_Row_array (0 To Columnas)
Redim Preserve SimpleRow (Columnas)

Simple_Row_array() = oRange.getDataArray() 'Asign values to an array

For i = LBound(Simple_Row_array()) To UBound(Simple_Row_array())
SimpleRow(i) = Simple_Row_array(0)(i)
Next i

Print UBound(SimpleRow()) 'It display the amount of values correctly
Print UBound(Simple_Row_array(),1)'it displays always 0. 
Print UBound(Simple_Row_array())'it displays always 0.
End Sub

在循环“For”中,如果我通过变量“Columnas”更改 UBound(),则迭代有效。 任何想法为什么我错了? 请具体点。如果您有解决方案,请将代码发送给我。

【问题讨论】:

    标签: libreoffice-calc libreoffice-basic


    【解决方案1】:

    函数getDataArray() 返回一个数组数组,每个成员代表电子表格中的一行,因此代表目标范围的数组是Simple_Row_array成员,索引为零,即Simple_Row_array(0)。所以如果你写:

    For i = LBound(Simple_Row_array(0)) To UBound(Simple_Row_array(0))
        SimpleRow(i) = Simple_Row_array(0)(i)
    Next i
    
    Print UBound(SimpleRow()) 'It display the amount of values correctly
    Print UBound(Simple_Row_array(0)) 'now displays 33. 
    

    一切都应该正常工作。


    所以 Ooo Basic (AOO Basic) 中的脚本将二维数组传递给一维数组以获得 libreOffice 的 Calc 文档 - OpenOffice' 将是这样的:

    Sub ConvertArrayUnidimensional
    'Convert a bidimensional array to a unidimensional array for libreOffice - OpenOffice' Calc document. OOo Basic.
    Dim oSheet as object
    Dim Simple_Row_array() As Variant 'The bidimensional array.
    Dim SimpleRow 'The new unidimensional array has this name.
    Dim Columnas as Long
    
    oSheet = ThisComponent.Sheets.getByName("Solicitud") 'Get this sheet 'Solicitudes'
    Dim oRange As Object  : oRange = oSheet.getCellRangebyName( "A1:AH1" ) 'Get the first row. Where are the name headers of the columns.
    
    Columnas = oRange.Columns.getCount() - 1  'Get the number of columns.
    Redim Preserve Simple_Row_array (0 To Columnas)
    Redim Preserve SimpleRow (Columnas)
    
    Simple_Row_array() = oRange.getDataArray() 'Asign values to an array. getDataArray method takes the whole row values of this range in a bidimensional way.
    
    For i = LBound(Simple_Row_array()) To UBound(Simple_Row_array(0))
    SimpleRow(i) = Simple_Row_array(0)(i)
    Next i
    
    Print "SimpleRow: " & UBound(SimpleRow()) 'It display the amount of values correctly 33
    Print "Simple_Row_array(0): " & UBound(Simple_Row_array(0)) 'Now displays the total number which is 33. 
    End Sub
    

    【讨论】:

    • 抱歉,我的回复延迟了。它现在完美无缺。非常感谢!!!
    猜你喜欢
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多