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?
[Solved] Function and returned object
[Solved] Function and returned 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
Re: Function and returned object
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:
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.
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
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
---
Lupp from München
Re: Function and returned object
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)
Re: Function and returned object
Thanks for your explanations
Solved!
Solved!
OpenOffice 4.1.12 - Windows 10