Difference between revisions of "NL/Documentation/BASIC Guide/Cells and Ranges"
(Created page with "{{NL/Documentation/BASICGuideTOC/v2 |ShowPrevNext=block |ShowPrevPage=block |PrevPage=NL/Documentation/BASIC Guide/Rows and Columns |NextPage=NL/Documentation/BASIC Guide/Form..." (tussenstap opslaan)) |
|||
Line 13: | Line 13: | ||
Het volgende voorbeeld maakt een object dat verwijst naar de bovenste linker cel en voegt een tekst in die cel in: | Het volgende voorbeeld maakt een object dat verwijst naar de bovenste linker cel en voegt een tekst in die cel in: | ||
− | |||
<source lang="oobas"> | <source lang="oobas"> | ||
Line 67: | Line 66: | ||
Het voorbeeld voegt één getal, één tekst en één formule in in de velden A1 t/m A3. | Het voorbeeld voegt één getal, één tekst en één formule in in de velden A1 t/m A3. | ||
− | {{Documentation/SO5note| | + | {{Documentation/SO5note|De eigenschappen <tt>Value</tt>, <tt>String</tt> en <tt>Formula</tt> hebben voorrang boven de oude methode <tt>PutCell</tt> van StarOffice 5 voor het instellen van de waarden van een tabelcel.}} |
− | {{OOo}} | + | {{OOo}} behandelt ingevoerde celinhoud met gebruik van de eigenschap <tt>String</tt> als tekst, zelfs als de inhoud een getal is. Getallen worden links uitgelijnd in de cel in plaats van uitgelijnd op rechts. U zou het verschil tussen tekst en formules ook moeten bemerken als u formules gebruikt: |
<source lang="oobas"> | <source lang="oobas"> | ||
Dim Doc As Object | Dim Doc As Object | ||
− | Dim | + | Dim Blad As Object |
− | Dim | + | Dim Cel As Object |
Doc = ThisComponent | Doc = ThisComponent | ||
− | + | Blad = Doc.Sheets(0) | |
− | + | Cel = Blad.getCellByPosition(0, 0) | |
− | + | Cel.Value = 100 | |
− | + | Cel = Blad.getCellByPosition(0, 1) | |
− | + | Cel.String = 1000 | |
− | + | Cel = Blad.getCellByPosition(0, 2) | |
− | + | Cel.Formula = "=A1+A2" | |
− | MsgBox | + | MsgBox Cel.Value |
</source> | </source> | ||
− | + | Hoewel cel A1 de waarde 100 bevat en cel A2 de waarde 1000, geeft de formule A1+A2 de waarde 100 weer. Dit komt omdat de inhoud van cel A2 werd ingevoerd als een tekenreeks en niet als een getal. | |
− | + | Gebruik de eigenschap <tt>Type</tt> om te controleren of een cel een tekenreeks of een getal bevat: | |
<source lang="oobas"> | <source lang="oobas"> | ||
Dim Doc As Object | Dim Doc As Object | ||
− | Dim | + | Dim Blad As Object |
− | Dim | + | Dim Cel As Object |
Doc = ThisComponent | Doc = ThisComponent | ||
− | + | Blad = Doc.Sheets(0) | |
− | + | Cel = Blad.getCellByPosition(1,1) | |
− | + | Cel.Value = 1000 | |
− | Select Case | + | Select Case Cel.Type |
Case com.sun.star.table.CellContentType.EMPTY | Case com.sun.star.table.CellContentType.EMPTY | ||
− | MsgBox " | + | MsgBox "Inhoud: Leeg" |
Case com.sun.star.table.CellContentType.VALUE | Case com.sun.star.table.CellContentType.VALUE | ||
− | MsgBox " | + | MsgBox "Inhoud: Getal" |
Case com.sun.star.table.CellContentType.TEXT | Case com.sun.star.table.CellContentType.TEXT | ||
− | MsgBox " | + | MsgBox "Inhoud: Tekst" |
Case com.sun.star.table.CellContentType.FORMULA | Case com.sun.star.table.CellContentType.FORMULA | ||
− | MsgBox " | + | MsgBox "Inhoud: Formule" |
End Select | End Select | ||
</source> | </source> | ||
− | + | De eigenschap <tt>Cel.Type</tt> geeft een waarde terug voor de opsomming <idl>com.sun.star.table.CellContentType</idl> die het type inhoud van een cel identificeert. De mogelijke waarden zijn: | |
− | ;<tt>EMPTY</tt>: | + | ;<tt>EMPTY</tt>:geen waarde |
− | ;<tt>VALUE</tt>: | + | ;<tt>VALUE</tt>:getal |
− | ;<tt>TEXT</tt>: | + | ;<tt>TEXT</tt>:tekenreeksen |
− | ;<tt>FORMULA</tt>: | + | ;<tt>FORMULA</tt>:formule |
− | == | + | == Cellen invoegen, verwijderen, kopiëren en verplaatsen == |
In addition to directly modifying cell content, {{OOo}} Calc also provides an interface that allows you to insert, delete, copy, or merge cells. The interface (<idl>com.sun.star.sheet.XCellRangeMovement</idl>) is available through the spreadsheet object and provides four methods for modifying cell content. | In addition to directly modifying cell content, {{OOo}} Calc also provides an interface that allows you to insert, delete, copy, or merge cells. The interface (<idl>com.sun.star.sheet.XCellRangeMovement</idl>) is available through the spreadsheet object and provides four methods for modifying cell content. |
Revision as of 13:26, 2 March 2013
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 : De eigenschappen Value, String en Formula hebben voorrang boven de oude methode PutCell van StarOffice 5 voor het instellen van de waarden van een tabelcel. |
Apache OpenOffice behandelt ingevoerde celinhoud met gebruik van de eigenschap String als tekst, zelfs als de inhoud een getal is. Getallen worden links uitgelijnd in de cel in plaats van uitgelijnd op rechts. U zou het verschil tussen tekst en formules ook moeten bemerken als u formules gebruikt:
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 = 1000 Cel = Blad.getCellByPosition(0, 2) Cel.Formula = "=A1+A2" MsgBox Cel.Value
Hoewel cel A1 de waarde 100 bevat en cel A2 de waarde 1000, geeft de formule A1+A2 de waarde 100 weer. Dit komt omdat de inhoud van cel A2 werd ingevoerd als een tekenreeks en niet als een getal.
Gebruik de eigenschap Type om te controleren of een cel een tekenreeks of een getal bevat:
Dim Doc As Object Dim Blad As Object Dim Cel As Object Doc = ThisComponent Blad = Doc.Sheets(0) Cel = Blad.getCellByPosition(1,1) Cel.Value = 1000 Select Case Cel.Type Case com.sun.star.table.CellContentType.EMPTY MsgBox "Inhoud: Leeg" Case com.sun.star.table.CellContentType.VALUE MsgBox "Inhoud: Getal" Case com.sun.star.table.CellContentType.TEXT MsgBox "Inhoud: Tekst" Case com.sun.star.table.CellContentType.FORMULA MsgBox "Inhoud: Formule" End Select
De eigenschap Cel.Type geeft een waarde terug voor de opsomming com.sun.star.table.CellContentType die het type inhoud van een cel identificeert. De mogelijke waarden zijn:
- EMPTY
- geen waarde
- VALUE
- getal
- TEXT
- tekenreeksen
- FORMULA
- formule
Cellen invoegen, verwijderen, kopiëren en verplaatsen
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). |