Search code examples
arraysopenoffice.orgbasic

How do I return an array from an OpenOffice Basic function?


I'm reading through the documentation on OpenOffice regarding arrays and it seems like Calc is capable of array functions just like Excel.

To test this I created a simple function that should return an array {1, 2, 3}:

Function Test()
    Dim Test(3) As Variant
    Test(1) = 1
    Test(2) = 2
    Test(3) = 3
End Function

When I populate a cell with =Test() and press Ctrl+Shift+Enter I just get a cell with 3 in it. Where are 1 and 2? What am I doing wrong?

I'm running OpenOffice 4.1.1.


Solution

  • The dimensioning of an function return type must be done within the Function statement. If the function shall return an array, then it must be dimensioned as Variant. There is no way in starbasic dimensioning the function as array type directly. The array itself can be created separately within the function body and then assigned to the function as return value.

    Like so:

    Function Test() as Variant
        Dim arr_Test(1 to 3) As Integer
        arr_Test(1) = 1
        arr_Test(2) = 2
        arr_Test(3) = 3
        Test = arr_Test
    End Function
    

    Or if you want it shorter, you could use the built-in Array function which returns a Variant containing an array.

    Like so:

    Function Test2() as Variant
        Test2 = Array(1,2,3)
    End Function
    

    Documentations for openoffice- / libreoffice- / -BASIC programming:

    https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide

    https://wiki.openoffice.org/wiki/Documentation/DevGuide/OpenOffice.org_Developers_Guide -> https://wiki.openoffice.org/wiki/Documentation/DevGuide/Basic/OpenOffice.org_Basic

    But both have not fully references for BASIC basics ;-) such as references for all built-in functions.

    https://help.libreoffice.org/Basic/Basic_Help -> https://help.libreoffice.org/Basic/Run-Time_Functions seems to be almost completely.