Download StarOffice™ Programmer`s Tutorial
Transcript
You can find all names in a named collection with the method getElementNames()
which returns a sequence of strings (see Section 3.5.2.3 “Sequences” on page 27. The
following code snippet displays the names of all style families for a document:
oStyleFamilies = oDocument.StyleFamilies
mFamilyNames = oStyleFamilies.getElementNames
For i%=LBound(mFamilyNames) To UBound(mFamilyNames)
Print mFamilyNames(i%)
Next i%
To find out if a named collection contains a certain element, you can use the
hasByName( ) method. It returns TRUE if the element exists, FALSE otherwise.
While the methods mentioned so far are available for all objects providing the
XNameAccess( ) interface, some of the more advanced facilities are only available
with the XNameContainer( ) interface. It provides two methods to add and remove
elements by name. To add a new element to a name container, you’d use
insertByName( ) like this:
oParagraphStyles.insertByName("myParagraphStyle", oStyle)
and to remove an element, you’d write
oParagraphStyles.removeByName("myParagraphStyle")
Please note that some interfaces might overwrite the insertByName( ) method with
its own version, requiring additional parameters. You should therefore always check
the reference manual. A third interface, namely XNameReplace( ), permits you to
replace existing elements:
oParagraphStyles.replaceByName("myParagraphStyle", oStyle)
Notice that the object you replace the old one with must exist before you can use
replaceByName().
3.3.2
Index access
To access indexed collections, you use the XIndexAccess interface like this:
oSheets = oCalcDocument.Sheets
oSheet = oSheets(0)
This code gets the collection of sheets from the current document and assigns the
first of them to oSheet. It will work only if the current document is a spreadsheet.
The simple usage of parentheses to access an indexed collection is some syntactic
sugar provided by StarBasic. The complete method call would be oSheet =
oSheets.getByIndex(0)( ).
Using StarOffice API - Basics 23