This macro will calculate the average of cells G31 through G42 on Sheet1 and place the result in G45. The phrase “Average_Marks” will be placed in cell H45:
Sub SetAverage
dim oFunction as variant
dim aArgument(0) as variant
dim oSheet as Object
dim myRange as Object
dim TotalCount as Double
'Need service'
oFunction = createUnoService("com.sun.star.sheet.FunctionAccess")
'Sheet to be used'
oSheet = ThisComponent.Sheets.getByName("Sheet1")
'Range for the function'
myRange = oSheet.getCellRangeByName( "G31:G42" )
'Set arguement for function'
aArgument(0)=myRange
'Run & get result of function'
TotalCount = oFunction.callFunction( "Average", aArgument )
'Place result by getting location & setting Value'
myRange = oSheet.GetCellRangeByName("G45")
myRange.setValue(TotalCount)
'Place wording by getting location & setting String'
myRange = oSheet.GetCellRangeByName("H45")
myRange.setString("Average_Marks")
End sub
You may want to review some documentation on macros. You can start with Calc Macros