Cell操作
[ Cellの書式設定(Property) / Format ]
Sub SampleCode()
Dim oDoc as Object
Dim oSheet as Object
Dim NumberFormats As Object
Dim NumberFormatString As String
Dim NumberFormatId As Long
Dim LocalSettings As New com.sun.star.lang.Locale
'
oDoc=ThisComponent
oSheet=oDoc.getSheets().getByName("sheet1")
oCell=oSheet.getCellByPosition(0,0) '←設定範囲
NumberFormats = oDoc.NumberFormats
NumberFormatString = "#,##0.#0円"
'
NumberFormatId = NumberFormats.queryKey(NumberFormatString, _
LocalSettings, True)
'書式がなければ書式コードを追加
If NumberFormatId = -1 Then
NumberFormatId = NumberFormats.addNew(NumberFormatString, _
LocalSettings)
End If
oCell.NumberFormat = NumberFormatId
'
msgbox("Success",0,"LO7.5.2.2")
End Sub
← クリックして拡大
【 解説 】
A1セルの書式を設定するコードです。
書式の設定は 表示形式キー で設定します。
NG: oCell.NumberFormat = "#,##0.#0円"
そのため、 表示形式キー が無ければ書式コードを追加した上で設定します。
書式コードがあり、その表示形式キーが分かっていれば、以下でも設定できます。
Sub SampleCode()
Dim oNumberFormats As Object
Dim oLocale As New com.sun.star.lang.Locale
oLocale.Language = "ja"
oLocale.Country = "JP"
oDoc = ThisComponent
oSheet = oDoc.getSheets.getByIndex(0)
oCell = oSheet.getCellByPosition(0,0)
oCell.value = 10000
oCell.NumberFormat = 10003
End Sub
注意: LO7.5.2.2 では 通貨を設定する場合、10103 の場合は 103
Sub SampleCode()
Dim oDoc as Object, oCtrl as Object, oFrame as Object
Dim oDispatcher as Object
Dim oProp(0) as new com.sun.star.beans.PropertyValue
oDoc = ThisComponent
oCtrl = oDoc.getCurrentController()
oFrame = oCtrl.getFrame()
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
'
oProp(0).Name = "ToPoint"
oProp(0).Value = "A1"
oDispatcher.executeDispatch(oFrame, ".uno:GoToCell", "", 0, oProp())
'
oProp(0).Name = "NumberFormatValue"
oProp(0).Value = 103
oDispatcher.executeDispatch(oFrame, ".uno:NumberFormatValue", "", 0, oProp())
msgbox "Success" & Chr$(10) & "( DispatchHelper )"
End Sub
'
' [ Note ]
1) Standard = 0
{ 数字 }
2) 0 = 1
3) 0.00 = 2
4) #,##0 = 3
5) #,##0.00 = 4
6) #,###.00 = 5
{ Percent }
7) 0% = 10
8) 0.00% = 11
{ 通貨 }
9) [$¥-411]#,##0;-[$¥-411]#,##0 = 101
10) [$¥-411]#,##0.00;-[$¥-411]#,##0.00 = 103
11) [$¥-411]#,##0;[RED]-[$¥-411]#,##0 = 103
12) [$¥-411]#,##0.00;[RED]-[$¥-411]#,##0.00 = 104
13) [$¥-411]#,##0.--;[RED]-[$¥-411]#,##0.-- = 105
14) #,##0 [$JPY];[RED]-#,##0 [$JPY] = 110
15) ¥#,##0;-¥#,##0 = 111
16) ¥#,##0.00;-¥#,##0.00 = 20
17) ¥#,##0;[RED]-¥#,##0 = 21
18) ¥#,##0.00;[RED]-¥#,##0.00 = 22
19) #,##0 CCC = 24
20) ¥#,##0.--;[RED]-¥#,##0.-- = 25
{ 日付 }
21) YY/M/D = 30
22) YYYY年MM月DD日(AAAA) = 38
23) YY/MM/DD = 37
24) YYYY/MM/DD = 36
25) YY年M月D日 = 39
26) YYYY年M月D日 = 75
27) GGGE年M月D日 = 80
28) YYYY年M月D日 = 76
29) GGGE年M月D日(AAAA) = 81
30) YY年M月D日(AAA) = 77
31) GGGE年M月D日(AAA) = 31
32) YYYY年M月D日(AAA) = 78
33) YYYY年M月D日(AAAA) = 79
34) MM.DD = 82
35) GE.M.D = 83
36) YYYY-MM-DD = 84
37) YY/MM = 32
38) M月D日 = 33
39) M月 = 34
40) YY年 QQ = 35
41) WW = 85
{ 時刻 }
42) YY/MM/DD HH:MM = 50
43) YYYY/M/D H:MM = 51
44) H:MM = 40
45) HH:MM:SS = 41
46) AM/PM H:MM = 42
47) AM/PM H:MM:SS = 43
48) [HH]:MM:SS = 44
49) MM:SS.00 = 45
50) [HH]:MM:SS.00 = 46
51) YY/MM/DD HH:MM = 50
52) YYYY/M/D H:MM = 51
{ 指数 }
53) 0.00E+000 = 60
54) 0.00E+00 = 61
{ 分数 }
55) # ?/? = 70
56) # ??/?? = 71
{ プール値 }
57) BOOLEAN = 99
{ テキスト }
58) @ = 100
Dim oDoc as Object
Dim oSheets as Object
Dim oCell as Object
Dim oDisp as String
oDoc = ThisComponent
oSheets = oDoc.Sheets(0)
oDisp = "[ セルの形式 ]"
for i = 0 to 3
oCell = oSheets.getCellByPosition(0,i)
oType = oCell.getType()
oDisp = oDisp & Chr$(10) & Chr$(9) & "A" & i+1 & "セル: "
Select Case oType
case com.sun.star.table.CellContentType.EMPTY
oDisp = oDisp & "Empty"
case com.sun.star.table.CellContentType.VALUE
oDisp = oDisp & "Value"
case com.sun.star.table.CellContentType.TEXT
oDisp = oDisp & "Text"
case com.sun.star.table.CellContentType.FORMULA
oDisp = oDisp & "Formula"
case Else
oDisp = oDisp & "UnKnown"
End Select
next i
msgbox(oDisp,0,"LO7.5.2.2")
End SUb
Sub SampleCode()
Dim oDoc as Object
Dim oSheets as Object
Dim oCell as Object
Dim oDisp as String
Dim oUserData as Object
Dim oUserAttr as new com.sun.star.xml.AttributeData
oDoc = ThisComponent
oSheets = oDoc.Sheets(0)
oCell =oSheets.getCellByPosition(0,0)
'xray oUserAtrr
oUserAttr.Type ="CDATA"
oUserAttr.Value = "NewOOo3 macro"
oUserData = oCell.UserDefinedAttributes
If NOT oUserData.hasByName("home") then
oUserData.insertByName("home",oUserAttr)
oCell.UserDefinedAttributes = oUserData
End If
'xray oUserData
oUser = oUserData.ElementNames
for i= 0 to UBound(oUser)
oDisp =oDisp & oUser(i) & Chr$(10)
next i
msgbox(oDisp,0,"LO7.5.2.2")
End Sub
'
LibreOffice 7.4 SDK API Reference / AttributeData Struct Reference
【 解説 】
ドキュメントはXMLとして保存され、読み込時に、XMLパーサーによって解析されます。
本コードの目的は、パーサーがXMLファイルを読み込む際に、自身で処理できない属性を保存できるすることです。
content.xml に独自に設定した スタイルが追加される(以下が追加される)。
<style:style style:name="ce1" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties home="NewOOo3 macro"/></style:style>
LibreOffice 7.4 SDK API Reference / AttributeData Struct Reference