Home > 備忘録 > libreoffice の macro(basic) > 内部関数を使う( 95 )
sub goukei '合計を求める
Dim sheet,range As Object
Dim kei As Long
sheet=ThisComponent.CurrentController.Activesheet
range=sheet.getCellRangeByName("A1:C5")
kei=range.computeFunction(com.sun.star.sheet.GeneralFunction.SUM) '内部関数Sum
end subsub kauntoif 'Countif("A1:C5",5)
Dim faccess,range As Object
Dim args(1) As Variant
faccess=CreateUnoService("com.sun.star.sheet.FunctionAccess")
range=sheet.getCellRangeByName("A1:C5")
aArgs(0)=range
aArgs(1)=5
faccess.callFunction("COUNTIF",args())
end subsub torikomi
Dim sheet,range,cols,rows As Object
Dim col,row,mdata(2,4) As Long
sheet=ThisComponent.CurrentController.Activesheet
range=sheet.getCellRangeByName("A1:C5")
cols=range.Columns
rows=range.Rows
for col=0 to cols.getCount()-1
for row=0 to rows.getCount()-1
mdata(col,row)=sheet.getCellByPosition(range.getRangeAddress().StartColumn+col,range.getRangeAddress().StartRow+row)
next row
next col
end subsub torikomi
Dim sheet As Object
Dim col,row,mdata(2,4) As Long
Dim data1() As Variant
sheet=ThisComponent.CurrentController.Activesheet
data1=sheet.getCellRangeByName("A1:C5").getDataArray()
for row=0 to Ubound(data1)
for col=0 to Ubound(data1(row))
mdata(col,row)=data1(row)(col) '取得したデータへのアクセス
next col
next row
end subsub demo
dim ndata() As String
ndata=test 'test を呼ぶ
end sub
sub test(col,row) As Object 'Object 型として定義する
Dim mdata=Array("aaaa","bbbb","cccc")
test=mdata
end sub下記のようなマクロをボタンに割り当てると、情報取得や背景色などの変更もできる
sub demo(evnt As Object)
'取得
MsgBox "名前: " & evnt.Source.getModel.Name & chr(10) & _
"タイトル: " & evnt.Source.getModel.Label & chr(10) & _
"タブの順序: " & evnt.Source.getModel.TabIndex
'変更
evnt.Source.getModel.BackgroundColor=RGB(255,0,0)
evnt.Source.getModel.TextColor=RGB(255,0,0)
end subsub demo
'様式
MsgBox xxxxx.xxxxx.DBG_Methods
MsgBox xxxxx.xxxxx.DBG_Properties
'例
MsgBox ThisComponent.DBG_Methods
MsgBox ThisComponent.DBG_Properties
end subsub Demo
'※列幅の数値はmmの100倍
Dim objSheet As Object
objSheet=ThisComponent.CurrentController.Activesheet
objSheet.Columns.Width=350 'sheet全体(3.5mm)
objSheet.Columns(1).Width=1000 'B列のみ(10mm)
objSheet.getCellRangeByName("A:H").Columns.Width=750 'AからH列まで(7.5mm)
end subsub Demo2
'※行高の数値はmmの100倍
Dim objSheet As Object
objSheet=ThisComponent.CurrentController.Activesheet
objSheet.Rows.Height=530 'sheet全体(5.3mm)
objSheet.Rows(1).Height=1000 '2行のみ(10mm)
objSheet.getCellRangeByName("5:8").Rows.Height=750 '5から8行(7.5mm)
end sub