I need to access in a formula of a given sheet the same cell in several distinct sheets. In order to do that I wrote my first basic program which creates a new user function, named OFFSETTEDCELL. This function takes as arguments the relative distance of the foreign sheet (an integer) and the name of the cell (a string), and returns the value of that cell in the foreign sheet. For example, =OFFSETTEDCELL(3, A2) should return the value of the cell A2 of the 3rd sheet located to the right of the current sheet. A negative offset would designate a sheet located to the left of the current sheet.
Here is the macro:
function sheetname(index)
sheetname = ThisComponent.Sheets(index - 1).Name
End function
function mysheet()
Dim oService As Object
Set oService = CreateUnoService("com.sun.star.sheet.FunctionAccess")
mysheet = oService.callFunction("SHEET", Array())
end function
function myindirect(fullcellname as String)
Dim oService As Object
Set oService = CreateUnoService("com.sun.star.sheet.FunctionAccess")
myindirect = oService.callFunction("INDIRECT", Array(fullcellname))
end function
function offsettedcell(sheetoffset, cellname)
thissheet = mysheet()
nameofsheet = sheetname(thissheet + sheetoffset)
offsettedcell = myindirect(nameofsheet & "." & cellname)
end function
The call of the function oService.callFunction("INDIRECT", Array(fullcellname))
raises an exception of type: com.sun.star.lang.IllegalArgumentException
.
The debugger shows that the variable nameofsheet
and the parameter cellname
have the expected values (for example “Sheet4” and “A3”) but it seems that they are not passed to the function myindirect
(or are passed in an invalid way).
Any help would be greatly appreciated.