Ubound(Array()) 无法使用 .getDataArray() 方法知道有多少元素具有 Array

Ubound(Array()) doesn't work to know how many elements has the Array with method .getDataArray()

我是 LibreOffice OOobasic 构建宏的新手。

我正在测试方法 .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() 则迭代工作。 任何想法为什么我做错了? 请具体点如果您有解决方案,请将代码发给我。

函数 getDataArray() returns 数组的数组,每个成员代表电子表格中的一行,因此代表目标范围的数组是 成员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 - OpenOffice 的 Calc 文档的脚本将是这样的:

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