Search code examples
libreoffice-calclibreoffice-basic

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


I am newbie with OOobasic building macros for LibreOffice.

I am testing the method .getDataArray() As you can see in the example, the array get loaded with the values, but later, I want to make an iteration with this array until the end of the array with UBound(), but UBound is always 0 and I don't know why. As you can see getDataArray get the values of a range. In my case, my range is a a simple row from A1 to 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

In the loop 'For' If I change the UBound() by the variable 'Columnas' then the iteration works. Any ideas why I am doig wrong? Please, be specific. If you have the solution send me the code.


Solution

  • The function getDataArray() returns an array of arrays with each member representing a row in the spreadsheet, so the array that represents your target range is the member of Simple_Row_array with index zero, i.e. Simple_Row_array(0). So if you write:

    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. 
    

    everything should work correctly.


    So the script in Ooo Basic (AOO Basic) to pass a bidimensional array to Unidimensional array for a Calc doc for libreOffice - OpenOffice' will be this:

    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