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:
: 133464 (view as bug list)
Depends on:
Blocks: Macro
  Show dependency treegraph
 
Reported: 2019-04-30 10:50 UTC by Armin Linder
Modified: 2024-12-23 15:18 UTC (History)
3 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
Comment 4 Mike Kaganski 2024-12-11 07:16:51 UTC
*** Bug 133464 has been marked as a duplicate of this bug. ***
Comment 5 Mike Kaganski 2024-12-11 10:17:10 UTC
As with bug, this is a regression after commit 1e55a47e89a9d9d6cf9cb3993484022aaf2c097b.

The call stack at the moment of the failure is


> sblo.dll!SbiRuntime::FindElement(SbxObject * pObj, unsigned long nOp1, unsigned long nOp2, ErrCode nNotFound, bool bLocal, bool bStatic) Line 3627	C++
> sblo.dll!SbiRuntime::StepELEM(unsigned long nOp1, unsigned long nOp2) Line 4187	C++
> sblo.dll!SbiRuntime::Step() Line 808	C++
> sblo.dll!`anonymous namespace'::RunInitGuard::run() Line 1017	C++
> sblo.dll!SbModule::Run(SbMethod * pMeth) Line 1177	C++
> sblo.dll!SbModule::Notify(SfxBroadcaster & rBC, const SfxHint & rHint) Line 776	C++
> svllo.dll!SfxBroadcaster::Broadcast(const SfxHint & rHint) Line 43	C++
> sblo.dll!SbMethod::Broadcast(SfxHintId nHintId) Line 2115	C++
> sblo.dll!SbxValue::Get(SbxValues & rRes) Line 290	C++
> sblo.dll!SbMethod::Call(SbxValue * pRet, SbxVariable * pCaller) Line 2071	C++
> sblo.dll!BasicManager::ExecuteMacro(const rtl::OUString & i_fullyQualifiedName, SbxArray * i_arguments, SbxValue * i_retValue) Line 1499	C++
> sfxlo.dll!SfxApplication::CallBasic(const rtl::OUString & rCode, BasicManager * pMgr, SbxArray * pArgs, SbxValue * pRet) Line 542	C++
> sfxlo.dll!SfxObjectShell::CallBasic(std::basic_string_view<char16_t,std::char_traits<char16_t>> rMacro, std::basic_string_view<char16_t,std::char_traits<char16_t>> rBasic, SbxArray * pArgs, SbxValue * pRet) Line 1446	C++
> sclo.dll!ScInterpreter::ScMacro() Line 3469	C++
> sclo.dll!ScInterpreter::Interpret() Line 4125	C++
> sclo.dll!ScFormulaCell::InterpretTail(ScInterpreterContext & rContext, ScFormulaCell::ScInterpretTailParameter eTailParam) Line 1946	C++
> sclo.dll!ScFormulaCell::Interpret(long nStartOffset, long nEndOffset) Line 1619	C++
> sclo.dll!ScFormulaCell::MaybeInterpret() Line 470	C++
> sclo.dll!ScFormulaCell::GetErrCode() Line 2973	C++
> sclo.dll!ScCellFormat::GetString(const ScRefCellValue & rCell, unsigned long nFormat, const Color * * ppColor, ScInterpreterContext * pContext, const ScDocument & rDoc, bool bNullVals, bool bFormula, bool bUseStarFormat) Line 90	C++
> sclo.dll!ScColumn::UpdateScriptType(sc::CellTextAttr & rAttr, long nRow, mdds::mtv::soa::detail::iterator_base<mdds::mtv::soa::multi_type_vector<sc::CellStoreTraits>::iterator_trait> & itr) Line 876	C++
> sclo.dll!ScColumn::GetRangeScriptType(mdds::mtv::soa::detail::iterator_base<mdds::mtv::soa::multi_type_vector<sc::CellTextAttrTraits>::iterator_trait> & itPos, long nRow1, long nRow2, const mdds::mtv::soa::detail::iterator_base<mdds::mtv::soa::multi_type_vector<sc::CellStoreTraits>::iterator_trait> & itrCells_) Line 2351	C++
> sclo.dll!`anonymous namespace'::FindEditCellsHandler::operator()(unsigned __int64 nRow, const ScFormulaCell * p) Line 2955	C++
> sclo.dll!sc::CheckElem<mdds::mtv::noncopyable_managed_element_block<54,ScFormulaCell,mdds::mtv::delayed_delete_vector>,mdds::mtv::soa::multi_type_vector<sc::CellStoreTraits>,`anonymous namespace'::FindEditCellsHandler>(const mdds::mtv::soa::multi_type_vector<sc::CellStoreTraits> & rStore, const mdds::mtv::soa::detail::const_iterator_base<mdds::mtv::soa::multi_type_vector<sc::CellStoreTraits>::const_iterator_trait,mdds::mtv::soa::detail::iterator_base<mdds::mtv::soa::multi_type_vector<sc::CellStoreTraits>::iterator_trait>> & it, unsigned __int64 nOffset, unsigned __int64 nDataSize, `anonymous-namespace'::FindEditCellsHandler & rFuncElem) Line 159	C++
> sclo.dll!sc::FindElement2<mdds::mtv::soa::multi_type_vector<sc::CellStoreTraits>,mdds::mtv::noncopyable_managed_element_block<53,EditTextObject,mdds::mtv::delayed_delete_vector>,mdds::mtv::noncopyable_managed_element_block<54,ScFormulaCell,mdds::mtv::delayed_delete_vector>,`anonymous namespace'::FindEditCellsHandler,A0x17ab1684::FindEditCellsHandler>(const mdds::mtv::soa::multi_type_vector<sc::CellStoreTraits> & rStore, unsigned __int64 nStart, unsigned __int64 nEnd, `anonymous-namespace'::FindEditCellsHandler & rFuncElem, `anonymous-namespace'::FindEditCellsHandler & rFuncElse) Line 506	C++
> sclo.dll!sc::FindFormulaEditText<`anonymous namespace'::FindEditCellsHandler>(const mdds::mtv::soa::multi_type_vector<sc::CellStoreTraits> & rStore, long nRow1, long nRow2, `anonymous-namespace'::FindEditCellsHandler & rFunc) Line 140	C++
> sclo.dll!ScColumn::HasEditCells(long nStartRow, long nEndRow, long & rFirst) Line 3294	C++
> sclo.dll!ScColumn::GetOptimalHeight(sc::RowHeightContext & rCxt, long nStartRow, long nEndRow, unsigned short nMinHeight, long nMinStart) Line 942	C++
> sclo.dll!`anonymous namespace'::GetOptimalHeightsInColumn(sc::RowHeightContext & rCxt, ScColContainer & rCol, long nStartRow, long nEndRow, ScProgress * pProgress, unsigned __int64 nProgressStart) Line 104	C++
> sclo.dll!ScTable::SetOptimalHeight(sc::RowHeightContext & rCxt, long nStartRow, long nEndRow, bool bApi, ScProgress * pOuterProgress, unsigned __int64 nProgressStart) Line 509	C++
> sclo.dll!ScDocRowHeightUpdater::update(const bool bOnlyUsedRows) Line 1664	C++
> sclo.dll!ScXMLImport::endDocument() Line 1438	C++
> saxlo.dll!sax_fastparser::FastSaxParserImpl::parseStream(const com::sun::star::xml::sax::InputSource & rStructSource) Line 896	C++
> saxlo.dll!sax_fastparser::FastSaxParser::parseStream(const com::sun::star::xml::sax::InputSource & aInputSource) Line 1470	C++
> xolo.dll!SvXMLImport::parseStream(const com::sun::star::xml::sax::InputSource & aInputSource) Line 528	C++
> sclo.dll!ScXMLImportWrapper::ImportFromComponent(const com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> & xContext, const com::sun::star::uno::Reference<com::sun::star::frame::XModel> & xModel, com::sun::star::xml::sax::InputSource & aParserInput, const rtl::OUString & sComponentName, const rtl::OUString & sDocName, const com::sun::star::uno::Sequence<com::sun::star::uno::Any> & aArgs, bool bMustBeSuccessful) Line 180	C++
> sclo.dll!ScXMLImportWrapper::Import(ImportFlags nMode, ErrCodeMsg & rError) Line 500	C++
> sclo.dll!ScDocShell::LoadXML(SfxMedium * pLoadMedium, const com::sun::star::uno::Reference<com::sun::star::embed::XStorage> & xStor) Line 584	C++
> sclo.dll!ScDocShell::Load(SfxMedium & rMedium) Line 718	C++
> sfxlo.dll!SfxObjectShell::LoadOwnFormat(SfxMedium & rMedium) Line 3648	C++
> sfxlo.dll!SfxObjectShell::DoLoad(SfxMedium * pMed) Line 710	C++
> sfxlo.dll!SfxBaseModel::load(const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> & seqArguments) Line 1991	C++
> sfxlo.dll!`anonymous namespace'::SfxFrameLoader_Impl::load(const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> & rArgs, const com::sun::star::uno::Reference<com::sun::star::frame::XFrame> & _rTargetFrame) Line 725	C++
> fwklo.dll!framework::LoadEnv::impl_loadContent() Line 1180	C++
> fwklo.dll!framework::LoadEnv::start() Line 415	C++
> fwklo.dll!framework::LoadEnv::startLoading(const rtl::OUString & sURL, const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> & lMediaDescriptor, const com::sun::star::uno::Reference<com::sun::star::frame::XFrame> & xBaseFrame, const rtl::OUString & sTarget, long nSearchFlags, LoadEnvFeatures eFeature) Line 311	C++

and only later ThisComponent is set in

> sfxlo.dll!SfxObjectShell::SetCurrentComponent(const com::sun::star::uno::Reference<com::sun::star::uno::XInterface> & _rxComponent) Line 929	C++
> sfxlo.dll!SfxViewShell::SetCurrentDocument() Line 3638	C++
> sfxlo.dll!SfxViewShell::Activate(bool bMDI) Line 2536	C++
> sclo.dll!ScTabViewShell::Activate(bool bMDI) Line 112	C++
> sfxlo.dll!SfxShell::DoActivate_Impl(SfxViewFrame * pFrame, bool bMDI) Line 326	C++
> sfxlo.dll!SfxDispatcher::DoActivate_Impl(bool bMDI) Line 600	C++
> sfxlo.dll!SfxViewFrame::DoActivate(bool bUI) Line 1197	C++
> sfxlo.dll!SfxApplication::SetViewFrame_Impl(SfxViewFrame * pFrame) Line 261	C++
> sfxlo.dll!SfxViewFrame::SetViewFrame(SfxViewFrame * pFrame) Line 3731	C++
> sfxlo.dll!SfxViewFrame::MakeActive_Impl(bool bGrabFocus) Line 2332	C++
> sfxlo.dll!SfxBaseController::ConnectSfxFrame_Impl(const SfxBaseController::ConnectSfxFrame i_eConnect) Line 1211	C++
> sfxlo.dll!SfxBaseController::attachFrame(const com::sun::star::uno::Reference<com::sun::star::frame::XFrame> & xFrame) Line 532	C++
> sfxlo.dll!utl::ConnectFrameControllerModel(const com::sun::star::uno::Reference<com::sun::star::frame::XFrame> & xFrame, const com::sun::star::uno::Reference<com::sun::star::frame::XController2> & xController, const com::sun::star::uno::Reference<com::sun::star::frame::XModel> & xModel) Line 57	C++
> sfxlo.dll!`anonymous namespace'::SfxFrameLoader_Impl::impl_createDocumentView(const com::sun::star::uno::Reference<com::sun::star::frame::XModel2> & i_rModel, const com::sun::star::uno::Reference<com::sun::star::frame::XFrame> & i_rFrame, const comphelper::NamedValueCollection & i_rViewFactoryArgs, const rtl::OUString & i_rViewName) Line 585	C++
> sfxlo.dll!`anonymous namespace'::SfxFrameLoader_Impl::load(const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> & rArgs, const com::sun::star::uno::Reference<com::sun::star::frame::XFrame> & _rTargetFrame) Line 763	C++
> fwklo.dll!framework::LoadEnv::impl_loadContent() Line 1180	C++
> fwklo.dll!framework::LoadEnv::start() Line 415	C++
> fwklo.dll!framework::LoadEnv::startLoading(const rtl::OUString & sURL, const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> & lMediaDescriptor, const com::sun::star::uno::Reference<com::sun::star::frame::XFrame> & xBaseFrame, const rtl::OUString & sTarget, long nSearchFlags, LoadEnvFeatures eFeature) Line 311	C++

Indeed, the evaluation of formulas (if needed(?)) before the document is loaded is bad - it should be postponed until after the call to SfxFrameLoader_Impl::impl_createDocumentView.
Comment 6 Mike Kaganski 2024-12-11 10:27:00 UTC
Note: the macro must be in My Macros/Standard.