Can an OOBasic Function return a Function??

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Can an OOBasic Function return a Function??

Post by saleem145 »

I want to write a function like

Code: Select all

Function Foo()

         Foo="=Text(10)"

End Function
But it doesn't work....it displays it as a string....

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: Can an OOBasic Function return a Function??

Post by karolus »

Hallo
saleem145 wrote:I want to write a function like

Code: Select all

Function Foo()

         Foo="=Text(10)"

End Function
But it doesn't work....it displays it as a string....

Saleem
What do you expect ? - it is a string
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Can an OOBasic Function return a Function??

Post by saleem145 »

To output 10 instead of =Text(10)

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: Can an OOBasic Function return a Function??

Post by karolus »

saleem145 wrote:To output 10 instead of =Text(10)

Saleem

Code: Select all

Function Foo()

         Foo= str(10)

End Function
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Can an OOBasic Function return a Function??

Post by Charlie Young »

If you want to format the text, you can use the Format function.

Code: Select all

Function FormattedFoo() As String
	FormattedFoo = Format(10,"##.0")
End Function
Obviously the 10 and the format can be arguments to the function. But of course since Format() and Str() are both functions themselves, there is no point in wrapping them in functions by their lonesome.
Apache OpenOffice 4.1.1
Windows XP
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Can an OOBasic Function return a Function??

Post by rudolfo »

saleem145 wrote:I want to write a function like

Code: Select all

Function Foo()
         Foo="=Text(10)"
End Function
But it doesn't work....it displays it as a string....

Saleem
I guess you think the function returns: =Text(10).
Which would in deed be evaluated to 10 if you type this in the input field for a cell. But that's trap. What it really returns is: '=Text(10).
In other words, the leading equal sign that usually introduces a formula is escaped with the quote and this prevents the part after the equal sign to be evaluated as a formula.
Or in short there is no such thing as recursive evaluation in this case. Typically you will write =Foo() in cell A3 and it will evaluate to =Text(10), but this result is not interpreted as a formula again.

This is in long words what Karolus pointed out very significantly: What do you expect ? - it is a string
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
JPL
Volunteer
Posts: 132
Joined: Fri Mar 30, 2012 3:14 pm

Re: Can an OOBasic Function return a Function??

Post by JPL »

Hello,

If you are looking for the dynamic execution of some Basic code then you need the equivalent of the

Code: Select all

Eval("...")
VBA function, which is not available in Basic.

If you are looking for the execution in Basic of Calc functions, then have a look at the XFunctionAccess interface (http://www.openoffice.org/api/docs/comm ... ccess.html).
As an example:

Code: Select all

Dim oFunctionAccess As Object, vResult As Variant
	Set oFunctionAccess = createUnoService("com.sun.star.sheet.FunctionAccess")
	vResult = oFunctionAccess.callFunction("TEXT",Array(10, "0,00"))
	MsgBox vResult
Hoping this will help.
JPL

PS: note that for above example the standard Format Basic function will do the job as well ...
Kubuntu 22.04 / LibO 24.2
Access2Base (LibO).
BaseDocumenter extension (LibO)
ScriptForge (LibO)
Documentation on https://help.libreoffice.org/latest/en- ... bPAR=BASIC
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Can an OOBasic Function return a Function??

Post by saleem145 »

Yeah, I was looking for dynamic execution of some function...its even more complicated...I would like to return a 2 dimensional dataset and only some columns would contain a function other regular data....

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Can an OOBasic Function return a Function??

Post by saleem145 »

Yeah, I was looking for dynamic execution of some function...its even more complicated...I would like to return a 2 dimensional dataset and only some columns would contain a function other regular data....actually some columns would contain a hyperlink which when clicked would call a macro and others regular data....

if I can return a hyperlink instead of function that would solve my problem too....

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
Jan_J
Posts: 167
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: Can an OOBasic Function return a Function??

Post by Jan_J »

If you had access to the range you are working on, you should be allowed to set Formula properties of cells within the range. But this approach requires using a subroutine, not a function in Basic.
JJ ∙ https://forum.openoffice.org/pl/
LO (7.6) ∙ Python (3.11|3.10) ∙ Unicode 15 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: Can an OOBasic Function return a Function??

Post by karolus »

Hallo
@Jan
Its not a Question of Function or Sub, its a Questions of 'where from the Function is called?' - from Spreadsheet as UDF or not.

Karolus
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Jan_J
Posts: 167
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: Can an OOBasic Function return a Function??

Post by Jan_J »

OK, karolus, You're right. I was responding by heart. When you make call from the code, both subprogram forms -- subroutines and functions as well -- have access to API objects.
But even so, if one has infomation about a cell or a range, he/she can set a formula in this cell during function call -- not by result, but by a side effect. I believe this may help to solve the problem.
JJ ∙ https://forum.openoffice.org/pl/
LO (7.6) ∙ Python (3.11|3.10) ∙ Unicode 15 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
Post Reply