Difference between revisions of "Documentation/BASIC Guide/Cells and Ranges"

From Apache OpenOffice Wiki
Jump to: navigation, search
 
m
 
(20 intermediate revisions by 7 users not shown)
Line 2: Line 2:
 
|ShowPrevNext=block
 
|ShowPrevNext=block
 
|ShowPrevPage=block
 
|ShowPrevPage=block
|PrevPage=Documentation/BASIC Guide/Spreadsheets
+
|PrevPage=Documentation/BASIC Guide/Rows and Columns
 
|NextPage=Documentation/BASIC Guide/Formatting Spreadsheet Documents
 
|NextPage=Documentation/BASIC Guide/Formatting Spreadsheet Documents
 
|spread=block
 
|spread=block
}}<!-- {{DISPLAYTITLE:Cells and Ranges}} -->
+
}}
 
+
{{DISPLAYTITLE:Cells and Ranges}}
= Cells and Ranges =
+
 
+
 
A spreadsheet consists of a two-dimensional list containing cells. Each cell is defined by its X and Y-position with respect to the top left cell which has the position (0,0).
 
A spreadsheet consists of a two-dimensional list containing cells. Each cell is defined by its X and Y-position with respect to the top left cell which has the position (0,0).
 +
 +
== Addressing and Editing Individual Cells ==
  
 
The following example creates an object that references the top left cell and inserts a text in the cell:
 
The following example creates an object that references the top left cell and inserts a text in the cell:
  
Dim Doc As Object
+
<syntaxhighlight lang="oobas">
Dim Sheet As Object
+
Dim Doc As Object
Dim Cell As Object   
+
Dim Sheet As Object
+
Dim Cell As Object   
Doc = StarDesktop.CurrentComponent
+
 
Sheet = Doc.Sheets(0)
+
Doc = ThisComponent
+
Sheet = Doc.Sheets(0)
Cell = Sheet.getCellByPosition(0, 0)
+
 
Cell.String = "Test"
+
Cell = Sheet.getCellByPosition(0, 0)
 +
Cell.String = "Test"
 +
</syntaxhighlight>
 +
 
  
 
In addition to numerical coordinates, each cell in a sheet has a name, for example, the top left cell (0,0) of a spreadsheet is called <tt>A1</tt>. The letter <tt>A</tt> stands for the column and the number 1 for the row. It is important that the '''name''' and '''position''' of a cell are not confused because row counting for names begins with 1 but the counting for position begins with 0.
 
In addition to numerical coordinates, each cell in a sheet has a name, for example, the top left cell (0,0) of a spreadsheet is called <tt>A1</tt>. The letter <tt>A</tt> stands for the column and the number 1 for the row. It is important that the '''name''' and '''position''' of a cell are not confused because row counting for names begins with 1 but the counting for position begins with 0.
  
In {{OOo}}, a table cell can be empty or contain text, numbers, or formulas. The cell type is not determined by the content that is saved in the cell, but rather the object property which was used for its entry. Numbers can be inserted and called up with the <tt>Value</tt> property, text with the <tt>String</tt> property, and formulas with the <tt>Formula</tt> property.
+
If the position of the cell is fixed, it is more clear to use the following code:
  
Dim Doc As Object
+
<syntaxhighlight lang="oobas">
Dim Sheet As Object
+
Dim Doc As Object
Dim Cell As Object   
+
Dim Sheet As Object
+
Dim Cell As Object   
Doc = StarDesktop.CurrentComponent
+
 
Sheet = Doc.Sheets(0)
+
Doc = ThisComponent
+
Sheet = Doc.Sheets(0)
Cell = Sheet.getCellByPosition(0, 0)
+
 
Cell.Value = 100
+
Cell = Sheet.getCellRangeByName("A1")
+
Cell.String = "Test"
Cell = Sheet.getCellByPosition(0, 1)
+
</syntaxhighlight>
Cell.String = "Test"
+
 
+
The above code also works with a named cell.
Cell = Sheet.getCellByPosition(0, 2)
+
 
Cell.Formula = "=A1"
+
In {{AOo}}, a table cell can be empty or contain text, numbers, or formulas. The cell type is not determined by the content that is saved in the cell, but rather the object property which was used for its entry. Numbers can be inserted and called up with the <tt>Value</tt> property, text with the <tt>String</tt> property, and formulas with the <tt>Formula</tt> property.
 +
 
 +
<syntaxhighlight lang="oobas">
 +
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 = "Test"
 +
 
 +
Cell = Sheet.getCellByPosition(0, 2)
 +
Cell.Formula = "=A1"
 +
</syntaxhighlight>
  
 
The example inserts one number, one text, and one formula in the fields A1 to A3.
 
The example inserts one number, one text, and one formula in the fields A1 to A3.
  
{{Documentation/Note|The <tt>Value</tt>, <tt>String</tt>, and <tt>Formula</tt> properties supersede the <tt>PutCell</tt> method for setting the values of a table cell.}}
+
{{Documentation/SO5note|The <tt>Value</tt>, <tt>String</tt>, and <tt>Formula</tt> properties supersede the old <tt>PutCell</tt> method of StarOffice 5 for setting the values of a table cell.}}
  
{{OOo}} treats cell content that is entered using the <tt>String</tt> 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:
+
{{AOo}} treats cell content that is entered using the <tt>String</tt> 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
+
<syntaxhighlight lang="oobas">
Dim Sheet As Object
+
Dim Doc As Object
Dim Cell As Object
+
Dim Sheet As Object
+
Dim Cell As Object
Doc = StarDesktop.CurrentComponent
+
 
Sheet = Doc.Sheets(0)
+
Doc = ThisComponent
+
Sheet = Doc.Sheets(0)
Cell = Sheet.getCellByPosition(0, 0)
+
 
Cell.Value = 100
+
Cell = Sheet.getCellByPosition(0, 0)
+
Cell.Value = 100
Cell = Sheet.getCellByPosition(0, 1)
+
 
Cell.String = 1000
+
Cell = Sheet.getCellByPosition(0, 1)
+
Cell.String = 1000
Cell = Sheet.getCellByPosition(0, 2)
+
 
Cell.Formula = "=A1+A2"
+
Cell = Sheet.getCellByPosition(0, 2)
+
Cell.Formula = "=A1+A2"
MsgBox Cell.Value  
+
 
 +
MsgBox Cell.Value  
 +
</syntaxhighlight>
  
 
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.
 
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.
Line 71: Line 95:
 
To check if the contents of a cell contains a number or a string, use the <tt>Type</tt> property:
 
To check if the contents of a cell contains a number or a string, use the <tt>Type</tt> property:
  
Dim Doc As Object
+
<syntaxhighlight lang="oobas">
Dim Sheet As Object
+
Dim Doc As Object
Dim Cell As Object
+
Dim Sheet As Object
+
Dim Cell As Object
Doc = StarDesktop.CurrentComponent
+
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 <tt>Cell.Type</tt> property returns a value for the <tt>com.sun.star.table.CellContentType</tt> enumeration which identifies the contents type of a cell. The possible values are:
+
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
 +
</syntaxhighlight>
 +
 
 +
The <tt>Cell.Type</tt> property returns a value for the <idl>com.sun.star.table.CellContentType</idl> enumeration which identifies the contents type of a cell. The possible values are:
  
 
;<tt>EMPTY</tt>:no value
 
;<tt>EMPTY</tt>:no value
Line 101: Line 127:
 
== Inserting, Deleting, Copying and Moving Cells ==
 
== Inserting, Deleting, Copying and Moving Cells ==
  
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 (<tt>com.sun.star.sheet.XRangeMovement</tt>) is available through the spreadsheet object and provides four methods for modifying cell content.
+
In addition to directly modifying cell content, {{AOo}} 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.
  
 
The <tt>insertCell</tt> method is used to insert cells into a sheet.
 
The <tt>insertCell</tt> method is used to insert cells into a sheet.
  
Dim Doc As Object
+
<syntaxhighlight lang="oobas">
Dim Sheet As Object
+
Dim Doc As Object
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
+
Dim Sheet As Object
+
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
Doc = StarDesktop.CurrentComponent
+
 
Sheet = Doc.Sheets(0)
+
Doc = ThisComponent
+
Sheet = Doc.Sheets(0)
CellRangeAddress.Sheet = 0
+
 
CellRangeAddress.StartColumn = 1
+
CellRangeAddress.Sheet = 0
CellRangeAddress.StartRow = 1
+
CellRangeAddress.StartColumn = 1
CellRangeAddress.EndColumn = 2
+
CellRangeAddress.StartRow = 1
CellRangeAddress.EndRow = 2
+
CellRangeAddress.EndColumn = 2
+
CellRangeAddress.EndRow = 2
Sheet.insertCells(CellRangeAddress, com.sun.star.sheet.CellInsertMode.DOWN)
+
 
 +
Sheet.insertCells(CellRangeAddress, com.sun.star.sheet.CellInsertMode.DOWN)
 +
</syntaxhighlight>
  
 
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.
 
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 <tt>com.sun.star.table.CellRangeAddress</tt> structure. The following values are included in this structure:
+
To define the cell range that you want to insert, use the <idl>com.sun.star.table.CellRangeAddress</idl> structure. The following values are included in this structure:
  
 
;<tt>Sheet (short)</tt>:number of the sheet (numbering begins with 0).
 
;<tt>Sheet (short)</tt>:number of the sheet (numbering begins with 0).
Line 130: Line 158:
 
;<tt>EndRow (long)</tt>:final row in the cell range (numbering begins with 0).
 
;<tt>EndRow (long)</tt>:final row in the cell range (numbering begins with 0).
  
The completed <tt>CellRangeAddress</tt> structure must be passed as the first parameter to the <tt>insertCells</tt> method. The second parameter of <tt>insertCells</tt> contains a value of the <tt>com.sun.star.sheet.CellInsertMode</tt> enumeration and defines what is to be done with the values that are located in front of the insert position. The <tt>CellInsertMode</tt> enumeration recognizes the following values:  
+
The completed <tt>CellRangeAddress</tt> structure must be passed as the first parameter to the <tt>insertCells</tt> method. The second parameter of <tt>insertCells</tt> contains a value of the <idl>com.sun.star.sheet.CellInsertMode</idl> enumeration and defines what is to be done with the values that are located in front of the insert position. The <tt>CellInsertMode</tt> enumeration recognizes the following values:  
  
 
;<tt>NONE</tt>:the current values remain in their present position.
 
;<tt>NONE</tt>:the current values remain in their present position.
Line 140: Line 168:
 
The <tt>removeRange</tt> method is the counterpart to the <tt>insertCells</tt> method. This method deletes the range that is defined in the <tt>CellRangeAddress</tt> structure from the sheet.
 
The <tt>removeRange</tt> method is the counterpart to the <tt>insertCells</tt> method. This method deletes the range that is defined in the <tt>CellRangeAddress</tt> structure from the sheet.
  
Dim Doc As Object
+
<syntaxhighlight lang="oobas">
Dim Sheet As Object
+
Dim Doc As Object
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
+
Dim Sheet As Object
+
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
Doc = StarDesktop.CurrentComponent
+
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 <tt>B2:C3</tt> 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 <tt>com.sun.star.sheet.CellDeleteMode</tt> enumeration:
+
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)
 +
</syntaxhighlight>
 +
 
 +
This example removes the <tt>B2:C3</tt> 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 <idl>com.sun.star.sheet.CellDeleteMode</idl> enumeration:
  
 
;<tt>NONE</tt>:the current values remain in their current position.
 
;<tt>NONE</tt>:the current values remain in their current position.
Line 165: Line 195:
 
The <tt>XRangeMovement</tt> interface provides two additional methods for moving (<tt>moveRange</tt>) or copying (<tt>copyRange</tt>) cell ranges. The following example moves the <tt>B2:C3</tt> range so that the range starts at position <tt>A6</tt>:
 
The <tt>XRangeMovement</tt> interface provides two additional methods for moving (<tt>moveRange</tt>) or copying (<tt>copyRange</tt>) cell ranges. The following example moves the <tt>B2:C3</tt> range so that the range starts at position <tt>A6</tt>:
  
Dim Doc As Object
+
<syntaxhighlight lang="oobas">
Dim Sheet As Object
+
Dim Doc As Object
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
+
Dim Sheet As Object
Dim CellAddress As New com.sun.star.table.CellAddress
+
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress
+
Dim CellAddress As New com.sun.star.table.CellAddress
Doc = StarDesktop.CurrentComponent
+
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 <tt>CellRangeAdress</tt> structure, the <tt>moveRange</tt> method expects a <tt>com.sun.star.table.CellAddress</tt> structure to define the origin of the move's target region. The <tt>CellAddress</tt> method provides the following values:
+
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)
 +
</syntaxhighlight>
 +
 
 +
In addition to the <tt>CellRangeAdress</tt> structure, the <tt>moveRange</tt> method expects a <idl>com.sun.star.table.CellAddress</idl> structure to define the origin of the move's target region. The <tt>CellAddress</tt> structure provides the following values:
  
 
;<tt>Sheet (short)</tt>:number of the spreadsheet (numbering begins with 0).
 
;<tt>Sheet (short)</tt>:number of the spreadsheet (numbering begins with 0).
Line 191: Line 223:
 
;<tt>Row (long)</tt>:number of the addressed row (numbering begins with 0).
 
;<tt>Row (long)</tt>:number of the addressed row (numbering begins with 0).
  
The cell contents in the target range are always overwritten by the <tt>moveRange</tt> method. Unlike in the <tt>InsertCells</tt> method , a parameter for performing automatic moves is not provided in the <tt>removeRange</tt> method.
+
The cell contents in the target range are always overwritten by the <tt>moveRange</tt> method. Unlike in the <tt>InsertCells</tt> method , a parameter for performing automatic moves is not provided in the <tt>moveRange</tt> method.
  
 
The <tt>copyRange</tt> method functions in the same way as the <tt>moveRange</tt> method, except that <tt>copyRange</tt> inserts a copy of the cell range instead of moving it.
 
The <tt>copyRange</tt> method functions in the same way as the <tt>moveRange</tt> method, except that <tt>copyRange</tt> inserts a copy of the cell range instead of moving it.
  
 +
{{Documentation/VBAnote|In terms of their function, the {{AOo}} Basic <tt>insertCell</tt>, <tt>removeRange</tt>, and <tt>copyRange</tt> methods are comparable with the VBA <tt>Range.Insert</tt>, <tt>Range.Delete</tt> ,and <tt>Range.Copy</tt> methods. Whereas in VBA, the methods are applied to the corresponding <tt>Range</tt> object, in {{AOo}} Basic they are applied to the associated <tt>Sheet</tt> object.}}
  
{{Documentation/Note|In terms of their function, the {{OOo}} Basic <tt>insertCell</tt>, <tt>removeRange</tt>, and <tt>copyRange</tt> methods are comparable with the VBA <tt>Range.Insert</tt>, <tt>Range.Delete</tt> ,and <tt>Range.Copy</tt> methods. Whereas in VBA, the methods are applied to the corresponding <tt>Range</tt> object, in {{OOo}} Basic they are applied to the associated <tt>Sheet</tt> object.}}
+
 
+
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Cells and Ranges}}
 
{{PDL1}}
 
{{PDL1}}

Latest revision as of 13:28, 30 January 2021


A spreadsheet consists of a two-dimensional list containing cells. Each cell is defined by its X and Y-position with respect to the top left cell which has the position (0,0).

Addressing and Editing Individual Cells

The following example creates an object that references the top left cell and inserts a text in the cell:

Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object   
 
Doc = ThisComponent
Sheet = Doc.Sheets(0)
 
Cell = Sheet.getCellByPosition(0, 0)
Cell.String = "Test"


In addition to numerical coordinates, each cell in a sheet has a name, for example, the top left cell (0,0) of a spreadsheet is called A1. The letter A stands for the column and the number 1 for the row. It is important that the name and position of a cell are not confused because row counting for names begins with 1 but the counting for position begins with 0.

If the position of the cell is fixed, it is more clear to use the following code:

Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object   
 
Doc = ThisComponent
Sheet = Doc.Sheets(0)
 
Cell = Sheet.getCellRangeByName("A1")
Cell.String = "Test"

The above code also works with a named cell.

In Apache OpenOffice, a table cell can be empty or contain text, numbers, or formulas. The cell type is not determined by the content that is saved in the cell, but rather the object property which was used for its entry. Numbers can be inserted and called up with the Value property, text with the String property, and formulas with the Formula property.

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 = "Test"
 
Cell = Sheet.getCellByPosition(0, 2)
Cell.Formula = "=A1"

The example inserts one number, one text, and one formula in the fields A1 to A3.

Documentation note.png 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.

Documentation note.png VBA : In terms of their function, the Apache OpenOffice Basic insertCell, removeRange, and copyRange methods are comparable with the VBA Range.Insert, Range.Delete ,and Range.Copy methods. Whereas in VBA, the methods are applied to the corresponding Range object, in Apache OpenOffice Basic they are applied to the associated Sheet object.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools