Cellen en bereiken
Een werkbladdocument bestaat uit een tweedimensionale lijst die cellen bevat. Elke cel wordt gedefinieerd door zijn positie X en Y ten opzichte van de bovenste linker cel, die de positie (0,0) heeft.
Adresseren en bewerken van Individuele cellen
Het volgende voorbeeld maakt een object dat verwijst naar de bovenste linker cel en voegt een tekst in die cel in:
Dim Doc As Object
Dim Blad As Object
Dim Cel As Object
Doc = ThisComponent
Blad = Doc.Sheets(0)
Cel = Blad.getCellByPosition(0, 0)
Cel.String = "Test"
In aanvulling op de numerieke coördinaten heeft elke cel op een blad een naam, bijvoorbeeld, de bovenste linker cel (0,0) van een werkblad wordt A1 genoemd. De letter A staat voor de kolom en het nummer 1 voor de rij. Het is belangrijk dat de naam en positie van een cel niet worden verward omdat de telling van de rijnamen begint met 1 maar het tellen van de positie begint met 0.
Als de positie van de cel vast staat is het duidelijker om de volgende code te gebruiken:
Dim Doc As Object
Dim Blad As Object
Dim Cel As Object
Doc = ThisComponent
Blad = Doc.Sheets(0)
Cel = Blad.getCellRangeByName("A1")
Cel.String = "Test"
De bovenstaande code werkt ook met een benoemde cel.
In Apache OpenOffice mag een tabelcel leeg zijn of tekst, getallen of formules bevatten. Het celtype wordt niet bepaald door de inhoud die wordt opgeslagen in de cel, maar eerder door de object-eigenschap die werd gebruikt voor de invoer. Getallen kunnen worden ingevoegd en aangeroepen met de eigenschap Value, tekst met de eigenschap String en formules met de eigenschap Formula.
Dim Doc As Object
Dim Blad As Object
Dim Cel As Object
Doc = ThisComponent
Blad = Doc.Sheets(0)
Cel = Blad.getCellByPosition(0, 0)
Cel.Value = 100
Cel = Blad.getCellByPosition(0, 1)
Cel.String = "Test"
Cel = Blad.getCellByPosition(0, 2)
Cel.Formula = "=A1"
Het voorbeeld voegt één getal, één tekst en één formule in in de velden A1 t/m A3.
| StarOffice 5 : The Value, String, and Formula properties supersede the old PutCell method of StarOffice 5 for setting the values of a table cell. |
Apache OpenOffice treats cell content that is entered using the String property as text, even if the content is a number. Numbers are left-aligned in the cell instead of right-aligned. You should also note the difference between text and numbers when you use formulas:
Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object
Doc = ThisComponent
Sheet = Doc.Sheets(0)
Cell = Sheet.getCellByPosition(0, 0)
Cell.Value = 100
Cell = Sheet.getCellByPosition(0, 1)
Cell.String = 1000
Cell = Sheet.getCellByPosition(0, 2)
Cell.Formula = "=A1+A2"
MsgBox Cell.Value
Although cell A1 contains the value 100 and cell A2 contains the value 1000, the A1+A2 formula returns the value 100. This is because the contents of cell A2 were entered as a string and not as a number.
To check if the contents of a cell contains a number or a string, use the Type property:
Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object
Doc = ThisComponent
Sheet = Doc.Sheets(0)
Cell = Sheet.getCellByPosition(1,1)
Cell.Value = 1000
Select Case Cell.Type
Case com.sun.star.table.CellContentType.EMPTY
MsgBox "Content: Empty"
Case com.sun.star.table.CellContentType.VALUE
MsgBox "Content: Value"
Case com.sun.star.table.CellContentType.TEXT
MsgBox "Content: Text"
Case com.sun.star.table.CellContentType.FORMULA
MsgBox "Content: Formula"
End Select
The Cell.Type property returns a value for the com.sun.star.table.CellContentType enumeration which identifies the contents type of a cell. The possible values are:
- EMPTY
- no value
- VALUE
- number
- TEXT
- strings
- FORMULA
- formula
Inserting, Deleting, Copying and Moving Cells
In addition to directly modifying cell content, Apache OpenOffice Calc also provides an interface that allows you to insert, delete, copy, or merge cells. The interface (com.sun.star.sheet.XCellRangeMovement) is available through the spreadsheet object and provides four methods for modifying cell content.
The insertCell method is used to insert cells into a sheet.
Dim Doc As Object
Dim Sheet As Object
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
Doc = ThisComponent
Sheet = Doc.Sheets(0)
CellRangeAddress.Sheet = 0
CellRangeAddress.StartColumn = 1
CellRangeAddress.StartRow = 1
CellRangeAddress.EndColumn = 2
CellRangeAddress.EndRow = 2
Sheet.insertCells(CellRangeAddress, com.sun.star.sheet.CellInsertMode.DOWN)
This example inserts a cells range that is two rows by two columns in size into the second column and row (each bear the number 1) of the first sheet (number 0) in the spreadsheet. Any existing values in the specified cell range are moved below the range.
To define the cell range that you want to insert, use the com.sun.star.table.CellRangeAddress structure. The following values are included in this structure:
- Sheet (short)
- number of the sheet (numbering begins with 0).
- StartColumn (long)
- first column in the cell range (numbering begins with 0).
- StartRow (long)
- first row in the cell range (numbering begins with 0).
- EndColumn (long)
- final column in the cell range (numbering begins with 0).
- EndRow (long)
- final row in the cell range (numbering begins with 0).
The completed CellRangeAddress structure must be passed as the first parameter to the insertCells method. The second parameter of insertCells contains a value of the com.sun.star.sheet.CellInsertMode enumeration and defines what is to be done with the values that are located in front of the insert position. The CellInsertMode enumeration recognizes the following values:
- NONE
- the current values remain in their present position.
- DOWN
- the cells at and under the insert position are moved downwards.
- RIGHT
- the cells at and to the right of the insert position are moved to the right.
- ROWS
- the rows after the insert position are moved downwards.
- COLUMNS
- the columns after the insert position are moved to the right.
The removeRange method is the counterpart to the insertCells method. This method deletes the range that is defined in the CellRangeAddress structure from the sheet.
Dim Doc As Object
Dim Sheet As Object
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
Doc = ThisComponent
Sheet = Doc.Sheets(0)
CellRangeAddress.Sheet = 0
CellRangeAddress.StartColumn = 1
CellRangeAddress.StartRow = 1
CellRangeAddress.EndColumn = 2
CellRangeAddress.EndRow = 2
Sheet.removeRange(CellRangeAddress, com.sun.star.sheet.CellDeleteMode.UP)
This example removes the B2:C3 cell range from the sheet and then shifts the underlying cells up by two rows. The type of removal is defined by one of the following values from the com.sun.star.sheet.CellDeleteMode enumeration:
- NONE
- the current values remain in their current position.
- UP
- the cells at and below the insert position are moved upwards.
- LEFT
- the cells at and to the right of the insert position are moved to the left.
- ROWS
- the rows after the insert position are moved upwards.
- COLUMNS
- the columns after the insert position are moved to the left.
The XRangeMovement interface provides two additional methods for moving (moveRange) or copying (copyRange) cell ranges. The following example moves the B2:C3 range so that the range starts at position A6:
Dim Doc As Object
Dim Sheet As Object
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
Dim CellAddress As New com.sun.star.table.CellAddress
Doc = ThisComponent
Sheet = Doc.Sheets(0)
CellRangeAddress.Sheet = 0
CellRangeAddress.StartColumn = 1
CellRangeAddress.StartRow = 1
CellRangeAddress.EndColumn = 2
CellRangeAddress.EndRow = 2
CellAddress.Sheet = 0
CellAddress.Column = 0
CellAddress.Row = 5
Sheet.moveRange(CellAddress, CellRangeAddress)
In addition to the CellRangeAdress structure, the moveRange method expects a com.sun.star.table.CellAddress structure to define the origin of the move's target region. The CellAddress structure provides the following values:
- Sheet (short)
- number of the spreadsheet (numbering begins with 0).
- Column (long)
- number of the addressed column (numbering begins with 0).
- Row (long)
- number of the addressed row (numbering begins with 0).
The cell contents in the target range are always overwritten by the moveRange method. Unlike in the InsertCells method , a parameter for performing automatic moves is not provided in the moveRange method.
The copyRange method functions in the same way as the moveRange method, except that copyRange inserts a copy of the cell range instead of moving it.
| Content on this page is licensed under the Public Documentation License (PDL). |