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

梦想与她 提交于 2019-12-11 23:58:40

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/29190901/how-do-i-return-an-array-from-an-openoffice-basic-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!