OpenOffice.org BASIC Guide - OpenOffice.org wiki
OpenOffice.org BASIC Guide - OpenOffice.org wiki
OpenOffice.org BASIC Guide - OpenOffice.org wiki
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
The Structure of Spreadsheets<br />
Note – ThisComponent returns the currently active document.<br />
The expression Doc.Sheets(0) is a Basic simplification of the API call : Doc.getSheets.getByIndex(0)<br />
Example 2: access by means of the name<br />
Dim Doc As Object<br />
Dim Sheet As Object<br />
Doc = ThisComponent<br />
Sheet = Doc.Sheets.getByName("Sheet 1")<br />
In the first example, the sheet is accessed by its number (counting begins at 0). In the second example, the sheet is<br />
accessed by its name and the getByName method.<br />
The Sheet object that is obtained by the getByName method supports the com.sun.star.sheet.Spreadsheet<br />
service. In addition to providing several interfaces for editing the content, this service provides the following<br />
properties:<br />
IsVisible (Boolean)<br />
value True if the spreadsheet is visible.<br />
PageStyle (String)<br />
name of the page template for the spreadsheet.<br />
Renaming Sheets<br />
A sheet provides methods getName and setName to read and modify its name. Basic can handle both methods<br />
like a property Name. Here we rename the first sheet of the spreadsheet document.<br />
Dim Doc As Object<br />
Dim Sheet As Object<br />
Doc = ThisComponent<br />
Sheet = Doc.Sheets(0)<br />
Sheet.Name = "First"<br />
Creating and Deleting Sheets<br />
The Sheets container of a spreadsheet document is also used to create and delete individual sheets. The<br />
following example uses the hasByName method to check if a sheet called MySheet exists. If it does, the method<br />
determines a corresponding object reference by using the getByName method and then saves the reference in a<br />
variable in Sheet. If the corresponding sheet does not exist, it is created by the createInstance call and<br />
inserted in the spreadsheet document by the insertByName method.<br />
Dim Doc As Object<br />
Dim Sheet As Object<br />
Doc = ThisComponent<br />
If Doc.Sheets.hasByName("MySheet") Then<br />
Sheet = Doc.Sheets.getByName("MySheet")<br />
Else<br />
Sheet = Doc.createInstance("com.sun.star.sheet.Spreadsheet")<br />
Doc.Sheets.insertByName("MySheet", Sheet)<br />
End If<br />
The hasByName, getByName and insertByName methods are obtained from the<br />
com.sun.star.container.XNameContainer interface as described in Introduction to the API.<br />
The interface com.sun.star.sheet.Spreadsheets provides a better method to create a new sheet:<br />
insertNewByName. It inserts a new sheet with the name specified by the first argument, at the position specified<br />
by the second argument.<br />
Dim Doc As Object<br />
Doc = ThisComponent<br />
Doc.Sheets.insertNewByName("OtherSheet", 2)<br />
92 <strong>OpenOffice</strong>.<strong>org</strong> 3.2 <strong>BASIC</strong> <strong>Guide</strong> · March 2010