Difference between revisions of "Documentation/BASIC Guide/Rows and Columns"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
 
(14 intermediate revisions by 6 users not shown)
Line 7: Line 7:
 
}}
 
}}
 
{{DISPLAYTITLE:Rows and Columns}}
 
{{DISPLAYTITLE:Rows and Columns}}
 +
 +
Each sheet contains a list of its rows and columns. These are available through the <tt>Rows</tt> and <tt>Columns</tt> properties of the spreadsheet object and support the <idl>com.sun.star.table.TableColumns</idl> and/or <idl>com.sun.star.table.TableRows</idl> services.
  
Each sheet contains a list of its rows and columns. These are available through the <tt>Rows</tt> and <tt>Columns</tt> properties of the spreadsheet object and support the <tt>com.sun.star.table.TableColumns</tt> and/or <tt>com.sun.star.table.TableRows</tt> services.
+
The following example creates two objects that reference the first row and the first column of a sheet and stores the references in the <tt>FirstCol</tt> and <tt>FirstRow</tt> object variables.
  
The following example creates two objects that reference the first row and the first column of a sheet and stores the references in the <tt>FirstCol</tt> and <tt>FirstRow</tt> object variables.
+
<syntaxhighlight lang="oobas">
 +
Dim Doc As Object
 +
Dim Sheet As Object
 +
Dim FirstRow As Object
 +
Dim FirstCol As Object
 +
 
 +
Doc = ThisComponent
 +
Sheet = Doc.Sheets(0)
  
Dim Doc As Object
+
FirstCol = Sheet.Columns(0)
Dim Sheet As Object
+
FirstRow = Sheet.Rows(0)
Dim FirstRow As Object
+
</syntaxhighlight>
Dim FirstCol As Object
 
 
Doc = StarDesktop.CurrentComponent
 
Sheet = Doc.Sheets(0)
 
 
FirstCol = Sheet.Columns(0)
 
FirstRow = Sheet.Rows(0)
 
  
The column objects support the <tt>com.sun.star.table.TableColumn</tt> service that has the following properties:
+
The column objects support the <idl>com.sun.star.table.TableColumn</idl> service that has the following properties:
  
 
;<tt>Width (long)</tt>:width of a column in hundredths of a millimeter.
 
;<tt>Width (long)</tt>:width of a column in hundredths of a millimeter.
Line 32: Line 34:
 
The width of a column is only optimized when the <tt>OptimalWidth</tt> property is set to <tt>True</tt>. If the width of an individual cell is changed, the width of the column that contains the cell is not changed. In terms of functionality, <tt>OptimalWidth</tt> is more of a method than a property.
 
The width of a column is only optimized when the <tt>OptimalWidth</tt> property is set to <tt>True</tt>. If the width of an individual cell is changed, the width of the column that contains the cell is not changed. In terms of functionality, <tt>OptimalWidth</tt> is more of a method than a property.
  
The row objects are based on the <tt>com.sun.star.table.RowColumn</tt> service that has the following properties:  
+
The row objects are based on the <idl>com.sun.star.table.TableRow</idl> service that has the following properties:  
  
 
;<tt>Height (long)</tt>:height of the row in 100ths of a millimeter.
 
;<tt>Height (long)</tt>:height of the row in 100ths of a millimeter.
Line 43: Line 45:
 
The following example activates the automatic height optimization for the first five rows in the sheet and makes the second column invisible.
 
The following example activates the automatic height optimization for the first five rows in the sheet and makes the second column invisible.
  
Dim Doc As Object
+
<syntaxhighlight lang="oobas">
Dim Sheet As Object
+
Dim Doc As Object
Dim Row As Object
+
Dim Sheet As Object
Dim Col As Object
+
Dim Row As Object
Dim I As Integer
+
Dim Col As Object
+
Dim I As Integer
Doc = StarDesktop.CurrentComponent
+
 
Sheet = Doc.Sheets(0)
+
Doc = ThisComponent
+
Sheet = Doc.Sheets(0)
For I = 0 To 4
+
 
    Row = Sheet.Rows(I)
+
For I = 0 To 4
    Row.OptimalHeight = True
+
  Row = Sheet.Rows(I)
Next I
+
  Row.OptimalHeight = True
+
Next I
Col = Sheet.Columns(1)
+
 
Col.IsVisible = False
+
Col = Sheet.Columns(1)
 +
Col.IsVisible = False
 +
</syntaxhighlight>
 +
 
 +
{{Note|The <tt>Rows</tt> and <tt>Columns</tt> lists can be accessed through an index in {{AOo}} Basic. The real API call is : <tt>Sheet.getColumns.getByIndex(1)</tt> }}
  
{{Documentation/Note|The <tt>Rows</tt> and <tt>Columns</tt> lists can be accessed through an index in {{OOo}} Basic. Unlike in VBA, the first column has the index 0 and not the index 1.}}
+
{{Documentation/VBAnote|Unlike in VBA, the first column has the index 0 and not the index 1.}}
  
 
== Inserting and Deleting Rows and Columns ==
 
== Inserting and Deleting Rows and Columns ==
Line 66: Line 72:
 
The <tt>Rows</tt> and <tt>Columns</tt> objects of a sheet can access existing rows and columns as well as insert and delete them.
 
The <tt>Rows</tt> and <tt>Columns</tt> objects of a sheet can access existing rows and columns as well as insert and delete them.
  
Dim Doc As Object
+
<syntaxhighlight lang="oobas">
Dim Sheet As Object
+
Dim Doc As Object
Dim NewColumn As Object
+
Dim Sheet As Object
+
Dim NewColumn As Object
Doc = StarDesktop.CurrentComponent
+
 
Sheet = Doc.Sheets(0)
+
Doc = ThisComponent
+
Sheet = Doc.Sheets(0)
Sheet.Columns.insertByIndex(3, 1)
+
 
Sheet.Columns.removeByIndex(5, 1)
+
Sheet.Columns.insertByIndex(3, 1)
 +
Sheet.Columns.removeByIndex(5, 1)
 +
</syntaxhighlight>
  
 
This example uses the <tt>insertByIndex</tt> method to insert a new column into the fourth column position in the sheet (index 3 - numbering starts at 0). The second parameter specifies the number of columns to be inserted (in this example: one).
 
This example uses the <tt>insertByIndex</tt> method to insert a new column into the fourth column position in the sheet (index 3 - numbering starts at 0). The second parameter specifies the number of columns to be inserted (in this example: one).
Line 82: Line 90:
 
The methods for inserting and deleting rows use the <tt>Rows</tt> object function in the same way as the methods shown for editing columns using the <tt>Columns</tt> object.
 
The methods for inserting and deleting rows use the <tt>Rows</tt> object function in the same way as the methods shown for editing columns using the <tt>Columns</tt> object.
  
 +
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Rows and Columns}}
 
{{PDL1}}
 
{{PDL1}}

Latest revision as of 13:26, 30 January 2021


Each sheet contains a list of its rows and columns. These are available through the Rows and Columns properties of the spreadsheet object and support the com.sun.star.table.TableColumns and/or com.sun.star.table.TableRows services.

The following example creates two objects that reference the first row and the first column of a sheet and stores the references in the FirstCol and FirstRow object variables.

Dim Doc As Object
Dim Sheet As Object
Dim FirstRow As Object
Dim FirstCol As Object

Doc = ThisComponent
Sheet = Doc.Sheets(0)

FirstCol = Sheet.Columns(0)
FirstRow = Sheet.Rows(0)

The column objects support the com.sun.star.table.TableColumn service that has the following properties:

Width (long)
width of a column in hundredths of a millimeter.
OptimalWidth (Boolean)
sets a column to its optimum width.
IsVisible (Boolean)
displays a column.
IsStartOfNewPage (Boolean)
when printing, creates a page break before a column.

The width of a column is only optimized when the OptimalWidth property is set to True. If the width of an individual cell is changed, the width of the column that contains the cell is not changed. In terms of functionality, OptimalWidth is more of a method than a property.

The row objects are based on the com.sun.star.table.TableRow service that has the following properties:

Height (long)
height of the row in 100ths of a millimeter.
OptimalHeight (Boolean)
sets the row to its optimum height.
IsVisible (Boolean)
displays the row.
IsStartOfNewPage (Boolean)
when printing, creates a page break before the row.

If the OptimalHeight property of a row is set to the True, the row height changes automatically when the height of a cell in the row is changed. Automatic optimization continues until the row is assigned an absolute height through the Height property.

The following example activates the automatic height optimization for the first five rows in the sheet and makes the second column invisible.

Dim Doc As Object
Dim Sheet As Object
Dim Row As Object
Dim Col As Object
Dim I As Integer

Doc = ThisComponent
Sheet = Doc.Sheets(0)

For I = 0 To 4
   Row = Sheet.Rows(I)
   Row.OptimalHeight = True
Next I

Col = Sheet.Columns(1)
Col.IsVisible = False
Documentation note.png The Rows and Columns lists can be accessed through an index in Apache OpenOffice Basic. The real API call is : Sheet.getColumns.getByIndex(1)
Documentation note.png VBA : Unlike in VBA, the first column has the index 0 and not the index 1.


Inserting and Deleting Rows and Columns

The Rows and Columns objects of a sheet can access existing rows and columns as well as insert and delete them.

Dim Doc As Object
Dim Sheet As Object
Dim NewColumn As Object

Doc = ThisComponent
Sheet = Doc.Sheets(0)

Sheet.Columns.insertByIndex(3, 1)
Sheet.Columns.removeByIndex(5, 1)

This example uses the insertByIndex method to insert a new column into the fourth column position in the sheet (index 3 - numbering starts at 0). The second parameter specifies the number of columns to be inserted (in this example: one).

The removeByIndex method deletes the sixth column (index 5). Again, the second parameter specifies the number of columns that you want to delete.

The methods for inserting and deleting rows use the Rows object function in the same way as the methods shown for editing columns using the Columns object.


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



Personal tools