OpenOffice.org OpenOffice - 3.2 Guide de base Page 353

  • Télécharger
  • Ajouter à mon manuel
  • Imprimer
  • Page
    / 497
  • Table des matières
  • MARQUE LIVRES
  • Noté. / 5. Basé sur avis des utilisateurs
Vue de la page 352
Writing macros that act like built-in functions
Although Calc finds and calls macros as normal functions, they do not
really behave as built-in functions. For example, macros do not appear
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.
Accessing cells directly
You can access the OOo internal objects directly to manipulate a Calc
document. For example, the macro in Listing 7 adds the values in cell
A2 from every sheet in the current document. ThisComponent is set by
StarBasic when the macro starts to reference the current document. A
Calc document contains sheets: ThisComponent.getSheets(). Use
getCellByPosition(col, row) to return a cell at a specific row and
column.
Listing 7. Add cell A2 in every sheet.
Function SumCellsAllSheets()
Dim TheSum As Double
Dim i As integer
Dim oSheets
Dim oSheet
Dim oCell
oSheets = ThisComponent.getSheets()
For i = 0 To oSheets.getCount() - 1
oSheet = oSheets.getByIndex(i)
oCell = oSheet.getCellByPosition(0, 1) ' GetCell A2
TheSum = TheSum + oCell.getValue()
Next
SumCellsAllSheets = TheSum
End Function
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,
Chapter 12 Calc Macros 353
Vue de la page 352
1 2 ... 348 349 350 351 352 353 354 355 356 357 358 ... 496 497

Commentaires sur ces manuels

Pas de commentaire