[Solved] Call POWER function in StarBasic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

[Solved] Call POWER function in StarBasic

Post by luofeiyu »

To get "3^4" in the cell,you simply write "=power(3;4)".I want to call the power function in basic:

Code: Select all

sub test()
    dim oFunctionAccess as object
    dim aParameters(2) as integer
    dim num as integer 	
    oFunctionAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )	
    aParameters(0) = 3
    aParameters(1) = 4		
    num = oFunctionAccess.CallFunction( "power", aParameters)
    print num
end sub
It can't work,how to fix it?
Last edited by luofeiyu on Thu Jan 16, 2025 2:51 am, edited 1 time in total.
LibreOffice 7.4.7.2 on Debian 12
User avatar
Lupp
Volunteer
Posts: 3693
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to call power function in basic?

Post by Lupp »

The POWER() function is superfluous anyway, because ther is the operator "^" for exponentiation.

Code: Select all

myResult = myBase ^ myExponent
does the trick.
BTW: The function and the operator as well also work with type Double.
On Windows 10: LibreOffice 25.2.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

Re: How to call power function in basic?

Post by luofeiyu »

I want to call it with the format of `oFunctionAccess.CallFunction( "power",.....)`.
LibreOffice 7.4.7.2 on Debian 12
User avatar
Lupp
Volunteer
Posts: 3693
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to call power function in basic?

Post by Lupp »

That's a useless complication. Are there reasons?

Your error:
You dimensioned

Code: Select all

aParameters(2)
and created an array with three elements :(0), (1), (2): by that.
On Windows 10: LibreOffice 25.2.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

Re: Call POWER function in StarBasic

Post by luofeiyu »

The right format is as below:

Code: Select all

sub callpower()
    dim oFunctionAccess as object
    dim aParameters(1) as integer
    dim num as integer 	
    oFunctionAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )	
    aParameters(0) = 3
    aParameters(1) = 4		
    num = oFunctionAccess.CallFunction( "power", aParameters)
    print num
end sub
LibreOffice 7.4.7.2 on Debian 12
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Call POWER function in StarBasic

Post by JeJe »

Code: Select all

	dim num 
    oFunctionAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )	
    num = oFunctionAccess.CallFunction( "power", array(3,4))

I presume you just want to learn how to use FunctionAccess as 3^4 is exactly the same - as Lupp and the help page says. Leaving num as a variant will prevent an overflow error with an integer if the return is large.

https://wiki.openoffice.org/wiki/Docume ... R_function
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Locked