表 
OOo 3.0 から Draw および Impress で表が作成できるようになりました。com.sun.star.drawing.TableShape を利用します。
挿入 
ドキュメントの XMultiServiceFactory インターフェースから com.sun.star.drawing.TableShape サービスをインスタンス化してドローページに挿入します。
Sub tableshape_1
oDoc = ThisComponent
oDrawPage = oDoc.getCurrentController().getCurrentPage()
oShape = oDoc.createInstance("com.sun.star.drawing.TableShape")
aPoint = CreateUnoStruct("com.sun.star.awt.Point")
aSize = CreateUnoStruct("com.sun.star.awt.Size")
aPoint.X = 1000
aPoint.Y = 1000
aSize.Width = 5000
aSize.Height = 5000
oShape.setPosition(aPoint)
oShape.setSize(aSize)
oDrawPage.add(oShape)
End Sub
削除、その他の操作は他の図形描写オブジェクトと同じように行えます。
表の操作 
挿入したすぐの表にはセルが一つしかありません。セルを追加したり、データを入力します。
oTable = oShape.Model
列と行 
列コンテナと行コンテナ。Calc のシートなどと同じように操作します。
セルの挿入 
セルを挿入します。
列を挿入。
oColumns = oTable.getColumns()
oColumns.insertByIndex(0, 2)
行も同様に行えます。
セルの削除 
列を削除。指定したインデックスから個数分だけ列を削除します。
oColumns.removeByIndex(1,1)
セル 
セルは Writer の表のセルと同じような操作が行えます。
oCell = oTable.getCellByPosition(0,1)
oCell.setString("cell 1")
表デザイン 
表のデザインはスタイルで管理されています。GUI では表示されませんが table および cell スタイルファミリがありそれらでスタイルを管理します。
追加したデザインは表デザイン一覧に表示されます。しかし、利用していない表は保存時に削除されます。
表スタイルファミリ 
表デザインの各スタイルを定義します。
oDoc = ThisComponent
oStyleFamiles = oDoc.getStyleFamilies()
oTableStyles = oStyleFamilies.getByName("table")
各表スタイルは次のような要素を持ち、それぞれがセルスタイルを示します。
background | 背景 |
body | 一般セル |
even-columns | 偶数列 |
even-rows | 偶数行 |
first-column | 最初の列 |
first-row | 最初の行 |
last-column | 最初の行 |
last-row | 最後の行 |
odd-columns | 奇数列 |
odd-rows | 奇数行 |
新しいスタイルを作成するには次のようにします。
' create new style
sCustomStyle = "custom2"
oTableStyle = oTableStyles.createInstance()
oTableStyle.setName(sCustomStyle)
oTableStyles.insertByName(sCustomStyle, oTableStyle)
表のスタイルはドキュメント内で使用されていない場合にはファイルに保存されません。
セルスタイルファミリ 
oCellStyles = oStyleFamilies.getByName("cell")
例 
表デザインを追加します。背景、枠線を設定していますが枠線は反映されません。デザインの設定では枠線は利用されないようです。
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
43
44
45
46
| | Sub ImpressTable
oDoc = ThisComponent
oStyleFamilies = oDoc.getStyleFamilies()
oCellStyles = oStyleFamilies.getByName("cell")
oTableStyles = oStyleFamilies.getByName("table")
sCustomStyle = "custom2"
oTableStyle = oTableStyles.createInstance()
oTableStyle.setParentStyle("default")
oTableStyle.setName(sCustomStyle)
If oTableStyles.hasByName(sCustomStyle) Then
oTableStyles.replaceByName(sCustomStyle, oTableStyle)
Else
oTableStyles.insertByName(sCustomStyle, oTableStyle)
End If
oCellStyle = CreateCellStyle(oTableStyle, oCellStyles, "custom1", "body", _
Array("FillColor", "LineColor", "LineWidth", "LineStyle", "CharHeight"), _
Array(1234567, 0, 10, com.sun.star.drawing.LineStyle.NONE, 24.0))
oPage = oDoc.getDrawPages().getByIndex(0)
oShape = oPage.getByIndex(0)
oShape.TableTemplate = oTableStyle
End Sub
Function CreateCellStyle( _
oTableStyle As Object, oCellStyles As Object, _
sStyleName As String, sCategory As String, _
sNames As Object, oValues As Object) As Object
oCellStyle = oCellStyles.createInstance()
oCellStyle.setParentStyle("default")
oCellStyle.setName(sStyleName)
For i = 0 To UBound(sNames) step 1
oCellStyle.setPropertyValue(sNames(i), oValues(i))
Next
If oCellStyles.hasByName(sStyleName) Then
oCellStyles.replaceByName(sStyleName, oCellStyle)
Else
oCellStyles.insertByName(sStyleName, oCellStyle)
End If
oTableStyle.replaceByName(sCategory, oCellStyle)
CreateCellStyle = oCellStyle
End Function
|