Write Your Own Functions

From Apache OpenOffice Wiki
Jump to: navigation, search



Write Your Own Functions

Calc can consider and execute macros as Calc functions. Use the following steps to create a simple macro.

  1. Create a new Calc document named CalcTestMacros.ods.
  2. Use Tools → Macros → Organize Macros → OpenOffice Basic to open the OpenOffice Basic Macros dialog shown in Figure 314. The Macro from box lists available macro library containers, including currently open documents. My Macros contains macros that you write or add to OpenOffice. OpenOffice Macros contains macros that are included with OpenOffice and should not be changed.
AOO41CG12 314.png
Figure 314: OpenOffice Basic Macros dialog
  1. Click  Organizer  to open the Basic Macro Organizer dialog (Figure 315). On the Libraries tab, select the document to contain the macro.


AOO41CG12 315.png
Figure 315: OpenOffice Basic Macro Organizer


  1. Click  New  to open the New Library dialog (Figure 316).


AOO41CG12 316.png
Figure 316: New Library dialog


  1. Enter a descriptive library name (such as AuthorsCalcMacros) and click  OK  to create the library. The new library name is shown in the library list, but the dialog may show only a portion of the name (Figure 317).


AOO41CG12 317.png
Figure 317: The library is shown in the organizer


  1. Select AuthorsCalcMacros and click  Edit  to edit the library. Calc automatically creates a module named Module1 and a macro named Main (Figure 318).


AOO41CG12 318.png
Figure 318: Basic Integrated Development Environment (IDE)


  1. Modify the code so that it is the same as that shown in Listing 2. The important addition is the creation of the NumberFive function, which returns the number five. The Option Explicit statement forces all variables to be declared before they are used. If Option Explicit is omitted, variables are automatically defined at first use as type Variant.
  2. Save the modified Module1.

Listing 2. Function that returns five.

REM  *****  BASIC  *****
Option Explicit
 
Sub Main
End Sub
   
Function NumberFive()
  NumberFive = 5
End Function
Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY).
Personal tools