In a LibreOffice macro, is there any way to specify a default parameter if one is not explicitly passed when invoking a function, and the parameter in the function declaration is optional?
I have two "global" variables (Private Variants) in my module, docObj and docObj2
docObj is a reference to the main document, i.e, ThisComponent
docObj2 is a reference to a second document which the macro opens, i.e., stardesktop.LoadComponentFromURL(convertToURL("/path/to/the/2nd/document"), "_blank", 0, Array())
I have a function that returns a sheet object, but since I have two document objects, it needs to know which one to use, defaulting to the main document:
Code: Select all
Function getSheet(sheetName, Optional documentObject As Variant)
If IsEmpty(documentObject) Then
documentObject = docObj
End If
getSheet = documentObject.getSheets().getByName(sheetName)
End Function
docSheet = getSheet("Sheet1")
or
docSheet = getSheet("Sheet1", docObj)
works as expected, returing the correct sheet object.
However, I have other routines that do things like clearing ranges, and they are dependent upon getSheet:
Code: Select all
Sub clearRange(sheetName As String, sheetRange As String, Optional clearFlags As Integer, Optional documentObject As Variant)
' Clear everything if not specified
If IsEmpty(clearFlags) Then
clearFlags = 1023
End If
' Clear the range
getSheet(sheetName, documentObject).getCellRangeByName(sheetRange).clearContents(clearFlags)
End Sub
Of course, if I was trying to work on the second document, I'd need to include it in the call:
clearRange("Sheet1", "A2:G7", , docObj2)
but, since I'm mostly working with the the primary document, thought this would work just as well:
clearRange("Sheet1", "A2:G7")
The first call works fine, but the second results in an "Object variable not set" error.
Inspecting documentObject:
When clearRange is called, its value is "<missing parameter>"
When clearRange calls getSheet, its value becomes Null
Sure, I could have code in every routine that calls getSheet to make sure I have passed it an object, but thought that making parameters optional meant I wouldn't have to explicitly specify them when calling the function.
Have fiddled with Option Explicit but having it or not doesn't seem to affect anything.
Is there any way around this, without going back and explicitly adding the desired document object into every function call?
Please advise. Thanks!