Calc Guide

2) Change the name of NumberFive to NumberFive_Implementation (Listing 3).
Listing 3. Change the name of NumberFive to NumberFive_Implementation
Function NumberFive_Implementation()
NumberFive_Implementation() = 5
End Function
3) In the Basic IDE (see Figure 312), hover the mouse cursor over the toolbar
buttons to display the tool tips. Click the Select Macro button to open the
OpenOffice.org Basic Macros dialog (see Figure 308).
4) Select the Standard library in the CalcTestMacros document and click New to
create a new module. Enter a meaningful name such as CalcFunctions and
click OK. OOo automatically creates a macro named Main and opens the
module for editing.
5) Create a macro in the Standard library that calls the implementation function
(see Listing 4). The new macro loads the AuthorsCalcMacros library if it is not
already loaded, and then calls the implementation function.
6) Save, close, and reopen the Calc document. This time, the NumberFive()
function works.
Listing 4. Change the name of NumberFive to NumberFive_Implementation.
Function NumberFive()
If NOT BasicLibraries.isLibraryLoaded("AuthorsCalcMacros") Then
BasicLibraries.LoadLibrary("AuthorsCalcMacros")
End If
NumberFive = NumberFive_Implementation()
End Function
Passing arguments to a macro
To illustrate a function that accepts arguments, we will write a macro that calculates
the sum of its arguments that are positive it will ignore arguments that are less
than zero (see Listing 5).
Listing 5. PositiveSum calculates the sum of the positive arguments.
Function PositiveSum(Optional x)
Dim TheSum As Double
Dim iRow As Integer
Dim iCol As Integer
TheSum = 0.0
If NOT IsMissing(x) Then
If NOT IsArray(x) Then
If x > 0 Then TheSum = x
Else
For iRow = LBound(x, 1) To UBound(x, 1)
For iCol = LBound(x, 2) To UBound(x, 2)
If x(iRow, iCol) > 0 Then TheSum = TheSum + x(iRow, iCol)
Next
Next
End If
End If
PositiveSum = TheSum
End Function
308 OpenOffice.org 3.3 Calc Guide