Bug 125042 - ThisComponent undefined/outdated when a spreadsheet loads
Summary: ThisComponent undefined/outdated when a spreadsheet loads
Status: RESOLVED DUPLICATE of bug 123005
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: x86-64 (AMD64) Windows (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-04-30 10:50 UTC by Armin Linder
Modified: 2019-05-01 07:40 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:
Regression By:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Armin Linder 2019-04-30 10:50:25 UTC
Description:
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:

Function TestThisComponent()

	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
ExitFunction:
	TestThisComponent = Result
End Function

2. In a cell, enter formula: =TestThisComponent()

3. Save and reload the sheet.

Actual Results:
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.

Expected Results:
The name of the current worksheet stays in the cell if I save and re-load the file.


Reproducible: Always


User Profile Reset: No



Additional Info:
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: 6.1.5.2 (x64)
Build-ID: 90f8dcf33c87b3705e78202e3df5142b201bd805
CPU-Threads: 4; BS: Windows 10.0; UI-Render: Standard; 
Gebietsschema: de-DE (de_DE); Calc: group threaded
Comment 1 Oliver Brinzing 2019-05-01 07:40:37 UTC
i think this issue is a duplicate of:

FILEOPEN: Error on creating getCurrentController().getActiveSheet() for basic script from location=document ?
https://bugs.documentfoundation.org/show_bug.cgi?id=123005

*** This bug has been marked as a duplicate of bug 123005 ***