列と行 
列と行の挿入や削除について行います。また,列と行のカウントについても取り扱います。
さらに,列と行の enumeration についても取り上げます。
挿入と削除 
行と列の挿入と削除は Rows または Columns に対して行います。これらは行または列のコンテナオブジェクトで,インデックス指定で挿入と削除を行うメソッドを持っています。
これらの Rows または Columns オブジェクトを取得する際にはシートオブジェクトなどから行います。
Sub rowsandcolumns_1
Dim oDoc As Object
Dim oSheet As Object
Dim oRows As Object
Dim oColumns As Object
oDoc = ThisComponent
oSheet = oDoc.getSheets().getByIndex(0)
oRows = oSheet.Rows
oColumns = oSheet.Columns
End Sub
挿入 
行および列の挿入は insertByIndex メソッドで行います。引数は挿入するインデックスおよび挿入数です。
Sub rowsandcolumns_2
Dim oDoc As Object
Dim oSheet As Object
Dim oRows As Object
Dim oColumns As Object
oDoc = ThisComponent
oSheet = oDoc.getSheets().getByIndex(0)
oRows = oSheet.Rows
oColumns = oSheet.Columns
oRows.insertByIndex(2,3)
End Sub
この例では行 C から 3 行削除します。
削除 
行および列の削除は removeByIndex メソッドで行います。このメソッドの引数は削除を始めるインデックスおよび削除数です。
Sub rowsandcolumns_3
Dim oDoc As Object
Dim oSheet As Object
Dim oRows As Object
Dim oColumns As Object
oDoc = ThisComponent
oSheet = oDoc.getSheets().getByIndex(0)
oRows = oSheet.Rows
oColumns = oSheet.Columns
oColumns.removeByIndex(3,4)
End Sub
上記の例では 4 列目から 4 列削除します。
表示と非表示 
行または列の範囲で表示と非表示を切り替えられます。表示状態の取得及び変更は IsVisible プロパティで行います。
以下のコードではシート上に配置したボタンを押すと行または列範囲の表示状態を切り替えます。
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| | ' assign to the initiated event of the form button
Sub CollapseOrExpand( ev )
btn = ev.Source.getModel()
'If SwitchColumnsVisibility( 0, 3, 13 ) Then
If SwitchRowsVisibility( 0, 3, 13 ) Then
btn.Label = "+"
Else
btn.Label = "-"
End If
End Sub
' nSheet: Sheet index, nStart: start column, nEnd: end column
Function SwitchColumnsVisibility( _
nSheet As Integer, nStart As Long, nEnd As Long ) As Boolean
oDoc = ThisComponent
oSheet = oDoc.getSheets().getByIndex( nSheet )
oRange = oSheet.getCellRangeByPosition( nStart, 0, nEnd, 0 )
oColumns = oRange.getColumns()
If oColumns.IsVisible Then
oColumns.IsVisible = False
SwitchColumnsVisibility = False
Else
oColumns.IsVisible = True
SwitchColumnsVisibility = True
End If
End Function
' nSheet: Sheet index, nStart: start row, nEnd: end row
Function SwitchRowsVisibility( _
nSheet As Integer, nStart As Long, nEnd As Long ) As Boolean
oDoc = ThisComponent
oSheet = oDoc.getSheets().getByIndex( nSheet )
oRange = oSheet.getCellRangeByPosition( 0, nStart, 0, nEnd )
oRows = oRange.getRows()
If oRows.IsVisible Then
oRows.IsVisible = False
SwitchRowsVisibility = False
Else
oRows.IsVisible = True
SwitchRowsVisibility = True
End If
End Function
|