0312CG3 CalcMacros
0312CG3 CalcMacros
12
Copyright
This document is Copyright 20052009 by its contributors as listed in the section titled Authors. You may distribute it and/or modify it under the terms of either the GNU General Public License, version 3 or later, or the Creative Commons Attribution License, version 3.0 or later. All trademarks within this guide belong to their legitimate owners.
Authors
Andrew Pitonyak Gary Schnabl Jean Hollis Weber
Feedback
Maintainer: Andrew Pitonyak [[email protected]] Please direct any comments or suggestions about this document to: [email protected]
Contents
Copyright...............................................................................................2 Introduction...........................................................................................4 Using the macro recorder......................................................................4 Write your own functions.......................................................................7 Using a macro as a function..............................................................10 Passing arguments to a macro..........................................................13 Arguments are passed as values.......................................................14 Writing macros that act like built-in functions..................................14 Accessing cells directly........................................................................15 Sorting.................................................................................................17 Conclusion...........................................................................................18
Calc Macros
Introduction
A macro is a saved sequence of commands or keystrokes that are stored for later use. An example of a simple macro is one that types your address. The OpenOffice.org (OOo) macro language is very flexible, allowing automation of both simple and complex tasks. Macros are especially useful to repeat a task the same way over and over again. This chapter briefly discusses common problems related to macro programming using Calc.
6) Use Edit > Paste Special to open the Paste Special dialog (see Figure 3).
Calc Macros
More detail on recording macros is provided in Chapter 13 (Getting Started with Macros) in the Getting Started guide; we recommend you read it if you have not already done so. More detail is also provided in the following sections, but not as related to recording macros.
Save the Calc document, close it, and open it again. By default, OOo will warn you when you open a document that contains a macro (see Figure 13). Click Enable Macros, or OOo will not allow any macros to be run inside the document. If you do not expect a document to contain a macro, it is safer to click Disable Macros in case the macro is a virus.
10
Calc Macros
macro loads the library containing the implementation and then calls the implementation. 1) Use Tools > Macros > Organize Macros > OpenOffice.org Basic to open the OpenOffice.org Basic Macros dialog (see Figure 17). Select the NumberFive macro and click Edit to open the macro for editing.
3) In the Basic IDE (see Figure 11), 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 17). 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.
12
Calc Macros
6) Save, close, and reopen the Calc document. This time, the NumberFive() function works.
The macro in Listing 5 demonstrates a couple of important techniques. 1) The argument x is optional. If the argument is not optional and it is called without an argument, OOo prints a warning message every time the macro is called. If Calc calls the function many times, then the error is displayed many times. 2) IsMissing checks that an argument was passed before the argument is used.
13
3) IsArray checks to see if the argument is a single value, or an array. For example, =PositiveSum(7) or =PositiveSum(A4). In the first case, the number 7 is passed as an argument, and in the second case, the value of cell A4 is passed to the function. 4) If a range is passed to the function, it is passed as a twodimensional array of values; for example, =PositiveSum(A2:B5). LBound and UBound are used to determine the array bounds that are used. Although the lower bound is one, it is considered safer to use LBound in case it changes in the future.
The macro in Listing 5 is careful and checks to see if the argument is an array or a single argument. The macro does not verify that each value is numeric. You may be as careful as you desire. The more things you check, the more robust the macro is, and the slower it runs.
Tip
Passing one argument is as easy as passing two: add another argument to the function definition (see Listing 6). When calling a function with two arguments, separate the arguments with a semicolon; for example, =TestMax(3; -4).
Listing 6. TestMax accepts two arguments and returns the larger of the two.
Function TestMax(x, y) If x >= y Then TestMax = x Else TestMax = y End If End Function
in the function lists. It is possible to write functions that behave as regular functions by writing an Add-In. However, this is an advanced topic that is not covered here; see http://wiki.services.openoffice.org/wiki/SimpleCalcAddIn.
Tip
A cell object supports the methods getValue(), getString(), and getFormula() to get the numerical value, the string value, or the formula used in a cell. Use the corresponding set functions to set appropriate values.
Use oSheet.getCellRangeByName("A2") to return a range of cells by name. If a single cell is referenced, then a cell object is returned. If a cell range is given, then an entire range of cells is returned (see Listing 8). Notice that a cell range returns data as an array of arrays, which is more cumbersome than treating it as an array with two dimensions as is done in Listing 5.
15
Tip
When a macro is called as a Calc function, the macro cannot modify any value in the sheet from which the macro was called.
Sorting
Consider sorting the data in Figure 18. First, sort on column B descending and then column A ascending.
16
Calc Macros
An array of sort fields determines the columns that are sorted. This is an array with two elements, 0 and 1. To sort on only one column, use: Dim oSortFields(0) As New com.sun.star.util.SortField oSortFields(1) As New com.sun.star.util.SortField
REM The sort descriptor is an array of properties. REM The primary property contains the sort fields. Dim oSortDesc(0) As New com.sun.star.beans.PropertyValue REM Get the sheet named "Sheet1" oSheet = ThisComponent.Sheets.getByName("Sheet1") REM Get the cell range to sort oCellRange = oSheet.getCellRangeByName("A1:C5") REM Select the range to sort. REM The only purpose would be to emphasize the sorted data. 'ThisComponent.getCurrentController.select(oCellRange) REM The columns are numbered starting with 0, so REM column A is 0, column B is 1, etc. REM Sort column B (column 1) descending. oSortFields(0).Field = 1 oSortFields(0).SortAscending = FALSE REM If column B has two cells with the same value, REM then use column A ascending to decide the order. oSortFields(1).Field = 0 oSortFields(1).SortAscending = True REM Setup the sort descriptor. oSortDesc(0).Name = "SortFields" oSortDesc(0).Value = oSortFields() REM Sort the range. oCellRange.Sort(oSortDesc()) End Sub
Sorting
17
Conclusion
This chapter provides a brief overview on how to create libraries and modules, using the macro recorder, using macros as Calc functions, and writing your own macros without the macro recorder. Each topic deserves at least one chapter, and writing your own macros for Calc could easily fill an entire book. In other words, this is just the beginning of what you can learn!
18
Calc Macros