[Solved] Function and returned object

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

[Solved] Function and returned object

Post by Mr.Dandy »

Hello,

I have a function in Calc that collect a CellRange structure and an createseachdescriptor object.
How can I return these into one single object?
Last edited by Mr.Dandy on Sat Jun 16, 2018 3:21 pm, edited 1 time in total.
OpenOffice 4.1.12 - Windows 10
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Function and returned object

Post by Lupp »

In Basic you can mainly use an array (with variant elements), a structure of defined (specialised) type or a collection, I think.

In your case a collection might be appropriate. See example code:

Code: Select all

Function createRangeWithSearchD() 
theSheet    = ThisComponent.Sheets(0)
myRange     = theSheet.GetCellRangeByName("D5:G12")
sDRange     = myRange.CreateSearchDescriptor()
With sDRange
  .SearchRegularExpression = True
  REM Additional settings may follow.
End With
Dim myCollection As New Collection
myCollection.Add(myRange, "theRange")
myCollection.Add(sDRange, "theSD") 
createRangeWithSearchD = myCollection
End Function


Sub test()
REM A rather absurd example. But it works!
coll = createRangeWithSearchD()
coll.Item("theSD").SearchString = "^1.*$"
firstFinding = coll.Item("theRange").findFirst(coll.Item("theSD"))
End Sub
One strange thing: Instead of using the keystrings assigned with the .Add method you can also use an index. It is 1-based!

Editing: I missed to state that I only recently learned about the Basic 'Class' Collection from user JeJe.
Last edited by Lupp on Fri Jun 15, 2018 11:30 am, edited 1 time in total.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Function and returned object

Post by JeJe »

I presume you know this and have a reason for wanting to pack everything into the function return - but in case you don't - its not necessary. If you pass objects/variable to the sub or function in the declaration - then you can use the return value to tell you whether the function succeeded or not. If the return value is a simple number then its often used for both purposes (eg 0 or -1 or another set value if the function fails, anything else and it succeeded)

Code: Select all

'with some help from Lupp's above example
sub test2()
	dim theRange as object,SearchDescriptor as object,succeed as boolean
	succeed = MYfunction(ThisComponent.Sheets(0),"D5:G12",therange,searchdescriptor)
	if succeed then
	msgbox searchdescriptor.SearchRegularExpression
		'do something

	end if
end sub

function MYfunction(Thesheet as object,rangeExpression as string, theRange as object,SearchDescriptor as object) as boolean
on error goto errorhandler:
	theRange = theSheet.GetCellRangeByName(rangeExpression)
	SearchDescriptor = theRange.CreateSearchDescriptor()
	SearchDescriptor.SearchRegularExpression = True
	MYfunction = true
errorhandler:
	'exits with MYfunction = false
end function

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Function and returned object

Post by Mr.Dandy »

Thanks for your explanations

Solved!
OpenOffice 4.1.12 - Windows 10
Post Reply