Ask Your Question

# Problem creating user-defined function in LibreCalc [closed]

I am trying to create the following function:

Function IMPARA (x,y)
IMPARA=IMDIV(IMPRODUCT(x,y),IMSUM(x,y))
End Function


When I call the function in the spreadsheet as =IMPARA(A1,B2), I get the following error: BASIC runtime error. Sub-procedure or function procedure not defined.

What am I doing wrong?

edit retag reopen merge delete

## Comments

Can you use Calc functions in BASIC? It sounds like it may be unhappy with the IMDIV(), IMPRODUCT(), and IMSUM() functions as being 'not defined'.

( 2013-06-25 03:23:15 +0100 )edit
1

I think you are right. Complex numbers are actually strings, so I guess I will have to parse the arguments into real and imaginery parts, convert to numbers and do the math and then re-create the string. PITA.

( 2013-06-25 04:55:20 +0100 )edit

Depends on operating system. Under windows, "Function Function_Name()" is enough. But under Ubuntu, you must declare the function properly, such "Function Function_Name() as Var_Type". If your function returning number, use Double data type for output.

( 2013-06-25 08:24:54 +0100 )edit

@Paijo setting IMPARA (x as string, y as string) as Var_Type does not alter the error. I am not sure you can use Calc functions (e.g., SUM) in a user-defined function. As @neolith says, for complex numbers (e.g., "-10+8i") this makes it a real pain. @neolith please report your results back here, regardless of whether you get it working or not.

( 2013-06-25 13:27:19 +0100 )edit

@oweng I just try to create a user-defined function use calc function nama. I make a RAND() function that always return value 100. Calc function always executed, and the macro never executed. So, back to main problem, I agree with you that the source of error are undefined function/procedure.

( 2013-06-26 11:58:48 +0100 )edit

## 3 Answers

Sort by » oldest newest most voted

You can call calc functions in basic, but the arguments need to be passed as an array. See this link

I think this function may work--give it a try and confirm.

function impara(x,y)
svc=createUnoService("com.sun.star.sheet.FunctionAccess")
arg1=array(x,y)
pr=svc.callFunction("IMPRODUCT",arg1)
su=svc.callFunction("IMSUM",arg1)
arg2=array(pr,su)
impara=svc.callFunction("IMDIV",arg2)
end function

more

## Comments

Thanks, works perfectly.

( 2013-06-27 02:50:00 +0100 )edit

I am going to move some of the content from the above comments into an answer in order to provide a possible resolution for this question.

I don't think you can you use Calc functions in BASIC. Even changing the first line to:

IMPARA (x as string, y as string) as Var_Type


...gives the same error. It sounds like it is unhappy with the IMDIV(), IMPRODUCT(), and IMSUM() functions as being 'not defined'. Complex numbers (e.g., "-10+8i") are treated as strings, so the function needs to parse the arguments into real and imaginary parts, convert these parts to numbers, do the math, and then re-create the string (answer) to be returned.

more

A good place to find useful information:
https://wiki.documentfoundation.org/Macros

Here the information on page 513 from Pitonyak guide.

**15.10. Accessing Calc functions**
You can call Calc functions from a macro.
Listing 455. Call the MIN function directly.

Sub callFunction
Dim oFA
oFA = createUnoService( "com.sun.star.sheet.FunctionAccess" )
' Calculate min of numbers.
print oFA.callFunction( "MIN", array( 10, 23, 5, 345 ) )
End Sub

more

## Stats

Asked: 2013-06-24 17:47:19 +0100

Seen: 3,933 times

Last updated: Jun 27 '13