Bug 125042 - ThisComponent undefined/outdated when a spreadsheet loads
Summary: ThisComponent undefined/outdated when a spreadsheet loads
Status: NEW
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: Macro
  Show dependency treegraph
 
Reported: 2019-04-30 10:50 UTC by Armin Linder
Modified: 2023-12-01 20:48 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


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 ***
Comment 2 Mike Kaganski 2023-11-29 11:08:29 UTC
This is not a duplicate of bug 123005. It doesn't try to access an active controller or an active sheet (things that may *legitimately* be absent when functions run).

I haven't tried to repro, so set to UNCONFIRMED. If repro, it's an own issue, and needs to be fixed. ThisComponent may provide information necessary for functions (e.g., calculation settings), so it must be available to the UDFs at all times.
Comment 3 Mike Kaganski 2023-11-29 11:46:18 UTC
Repro using Version: 7.6.3.2 (X86_64) / LibreOffice Community
Build ID: 29d686fea9f6705b262d369fede658f824154cc0
CPU threads: 12; OS: Windows 10.0 Build 19045; UI render: Skia/Raster; VCL: win
Locale: ru-RU (ru_RU); UI: en-US
Calc: CL threaded