If you use a formula pointing to a user defined function/macro (tested with: Basic Macro), which uses the ThisComponent object, the call fails, or gives unexpected values during the initial recalculation of the spreadsheet cells.
Steps to Reproduce:
1. Sample macro code:
Dim Doc As Object
Dim Sheet As Object
Dim Result as String
Dim S as String
on Error Goto ExitFunction
rem Result = "Unable to access XRay"
rem xRay ThisComponent
Result = "Unable to access ThisComomnent"
Doc = ThisComponent
Result = "Unable to access ThisComponent.Sheets(0)"
Sheet = Doc.Sheets (0)
Result = "Unable to access Sheet Name"
S = Sheet.Name
Result = S
TestThisComponent = Result
2. In a cell, enter formula: =TestThisComponent()
3. Save and reload the sheet.
After entering the forumla, everything looks fine, the worksheet name appears in the cell. When reloading the file, the call/formula fails.
If the reloaded sheet is the only open document, you get "Unable to access ThisComponent.Sheets(0)" or "Unable to access ThisComponent", depending on whether you have the macro editor open or not. If there is another calc spreadsheet open and activated, you get the other spreadsheet name inserted in your current document. If any other LibreOffice document (not a spreadsheet) is active, ThisComponent points to that document, and most likely fails because it won't have a "Sheets" property.
The name of the current worksheet stays in the cell if I save and re-load the file.
User Profile Reset: No
If I recalculate the spreadsheet cell manually after load [F9], the macro works.
Conclusions: ThisComponent points to the document object last activated. Spreadsheet recalculation on document load does obviously take place before the document gets activated, so this ThisComponent is either uninitialized or pointing to the wrong document. From testing I'd guess that the initial recalculation indeed takes place before any document event fires, so there isn't a workaround.
As far as I have read this behaviour is undocumented, contrary, the documentation does always state that ThisComponent points to the current document, which is not correct while the document recalculates upon load.
The only workaround I found is to ignore/suppress any errors, and trigger another recalculation in the "View Created" event handler which fires after the document has finished loading. This will, however, have the unwanted side-effect that the document recalculates twice on load.
Version: 126.96.36.199 (x64)
CPU-Threads: 4; BS: Windows 10.0; UI-Render: Standard;
Gebietsschema: de-DE (de_DE); Calc: group threaded
i think this issue is a duplicate of:
FILEOPEN: Error on creating getCurrentController().getActiveSheet() for basic script from location=document ?
*** This bug has been marked as a duplicate of bug 123005 ***