Page 1 of 1

Can an OOBasic Function return a Function??

Posted: Mon Jan 28, 2013 5:00 pm
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

Re: Can an OOBasic Function return a Function??

Posted: Mon Jan 28, 2013 5:04 pm
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

Re: Can an OOBasic Function return a Function??

Posted: Mon Jan 28, 2013 5:21 pm
by saleem145
To output 10 instead of =Text(10)

Saleem

Re: Can an OOBasic Function return a Function??

Posted: Mon Jan 28, 2013 5:27 pm
by karolus
saleem145 wrote:To output 10 instead of =Text(10)

Saleem

Code: Select all

Function Foo()

         Foo= str(10)

End Function

Re: Can an OOBasic Function return a Function??

Posted: Mon Jan 28, 2013 5:39 pm
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.

Re: Can an OOBasic Function return a Function??

Posted: Tue Jan 29, 2013 2:51 am
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

Re: Can an OOBasic Function return a Function??

Posted: Tue Jan 29, 2013 4:51 pm
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 ...

Re: Can an OOBasic Function return a Function??

Posted: Mon Feb 04, 2013 10:01 pm
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

Re: Can an OOBasic Function return a Function??

Posted: Mon Feb 04, 2013 10:01 pm
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

Re: Can an OOBasic Function return a Function??

Posted: Tue Feb 05, 2013 10:57 am
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.

Re: Can an OOBasic Function return a Function??

Posted: Tue Feb 05, 2013 12:40 pm
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

Re: Can an OOBasic Function return a Function??

Posted: Tue Feb 05, 2013 4:15 pm
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.