[Solved] Array of types in BASIC

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Rotnbair
Posts: 12
Joined: Wed Aug 23, 2023 10:01 pm

[Solved] Array of types in BASIC

Post by Rotnbair »

I have a BASIC macro to read a long list (Calc sheet) into an array in order to do some data processing in memory. Reading about creating a type, I have been looking at replacing my current parallel string and integer arrays with the following type:

type alist
word as string
rank as integer
ltr(4) as string
u as integer
end type

I attach a small Calc sheet with an example BASIC macro that copies some cells into the type example, then writes the type data into some other cells, as a demo. So that works, but my goal is to load a lot of data into an array consisting of several such types. I have tried several variations of getting the type fields into an array with no success.

I am aware of Mr. Andrew Pitonyak's statement in OOME_4_1: "Although user-defined types cannot directly contain an array, you can manage them in a variant type." Since my attached example includes an array element within the type, which works in the example sheet, I am not sure what he meant.

If anyone has any ideas about defining an array of types, thanks for the help. If this has been covered elsewhere (I did search), feel free to call me a dumbass but point me to it. Thanks.

 Edit: Changed subject, was type & array 
Make your post understandable by others 
-- MrProgrammer, forum moderator 
Attachments
typetest.ods
(13.57 KiB) Downloaded 253 times
Last edited by Rotnbair on Sun Jan 14, 2024 2:05 am, edited 4 times in total.
Apache OpenOffice 4.1.14
Windows 11
User avatar
Zizi64
Volunteer
Posts: 11364
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Array of types in Basic

Post by Zizi64 »

You can copy-paste the data and the types automatically (without knowing the actual type of each data) by this macro - based on the API:

Code: Select all

Sub MyGetSetDataArray
 Dim oDoc, oSheet, oSourceRange, oTargetRange as object

	oDoc = Thiscomponent
	oSheet = oDoc.Sheets.getByName("Sheet1")
	oSourceRange = oSheet.getCellRangeByName("$A$1:$H$1")
	oTargetRange = oSheet.getCellRangeByName("$J$1:$Q$1")
	oTargetRange.clearContents(1 OR 4) 'if you need to delete the content anyway - otherwise the Set... command will overwrite the previous data...

	oTargetRange.setDataArray(oSourceRange.getDataArray())

end sub
The parameters od the ClearContents:

Code: Select all

1   VALUE	selects constant numeric values that are not formatted as dates or times.
2   DATETIME	selects constant numeric values that have a date or time number format.
4   STRING	selects constant strings.
8   ANNOTATION	selects cell annotations.	
16  FORMULA	selects formulas.
32  HARDATTR	selects all explicit formatting but not the formatting which is applied implicitly through style sheets.
64  STYLES	selects cell styles.
128 OBJECTS	selects drawing objects.
256 §EDITATTR	selects formatting within parts of the cell contents.
512 FORMATTED	selects cells with formatting within the cells or cells with more than one paragraph within the cells.
http://www.openoffice.org/api/docs/comm ... Flags.html
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Rotnbair
Posts: 12
Joined: Wed Aug 23, 2023 10:01 pm

Re: [Solved] Array of types in BASIC

Post by Rotnbair »

After some tinkering, I found that a type can be an array with "records" including different items like strings, integers, singles, etc., that is good enough for my purposes. I have attached an example, updated from the previous posted version.
Attachments
typetest.ods
(15.31 KiB) Downloaded 149 times
Apache OpenOffice 4.1.14
Windows 11
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Array of types in BASIC

Post by JeJe »

To create an array of user type:

Code: Select all

type alist
word as string
rank as integer
ltr(4) as string
u as integer
end type

Sub Main
dim a(100)
for i = 0 to 100
a(i) = new alist
next
a(10).word = "test"
msgbox a(10).word
End Sub

In your solution you could dispense with the user type as you only use one instance and just define the arrays within it.

You could make things a lot simpler and faster though by following Zizi64's advice and work with the native dataarray.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply