diff options
author | Xisco Fauli <xiscofauli@libreoffice.org> | 2023-05-18 13:31:14 +0200 |
---|---|---|
committer | Caolán McNamara <caolan.mcnamara@collabora.com> | 2023-05-19 10:06:45 +0200 |
commit | 286a1c03fa10acf60f076a0af987112d24cb2ff5 (patch) | |
tree | 506aa529a3c3bc0eb705603866180aba2bdc2e44 /sc/source/ui/vba | |
parent | 63707596eefa4f141a5c7146e69bb5684ed51cf6 (diff) |
sc: check excel::getDocShell
There seems to be some crashes related. See
https://crashreport.libreoffice.org/stats/signature/ScDocShell::GetDocument()
or
https://crashreport.libreoffice.org/stats/signature/ScDocShell::RegisterAutomationWorkbookObject(com::sun::star::uno::Reference%3Cooo::vba::excel::XWorkbook%3E%20const%20&)
Change-Id: I9affce5a28cb1198eb472d369debf83fd7dd8c0e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151955
Tested-by: Jenkins
Tested-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Diffstat (limited to 'sc/source/ui/vba')
-rw-r--r-- | sc/source/ui/vba/vbaapplication.cxx | 13 | ||||
-rw-r--r-- | sc/source/ui/vba/vbapagesetup.cxx | 7 | ||||
-rw-r--r-- | sc/source/ui/vba/vbaworkbook.cxx | 26 | ||||
-rw-r--r-- | sc/source/ui/vba/vbaworksheet.cxx | 102 |
4 files changed, 85 insertions, 63 deletions
diff --git a/sc/source/ui/vba/vbaapplication.cxx b/sc/source/ui/vba/vbaapplication.cxx index d9e5ba984f5a..f951cbd4581b 100644 --- a/sc/source/ui/vba/vbaapplication.cxx +++ b/sc/source/ui/vba/vbaapplication.cxx @@ -1203,7 +1203,8 @@ uno::Reference< excel::XRange > lclCreateVbaRange( const ListOfScRange& rList ) { ScDocShell* pDocShell = excel::getDocShell( rxModel ); - if( !pDocShell ) throw uno::RuntimeException(); + if( !pDocShell ) + throw uno::RuntimeException(); ScRangeList aCellRanges; for( const auto& rItem : rList ) @@ -1350,8 +1351,11 @@ ScVbaApplication::Volatile( const uno::Any& aVolatile ) if ( pMeth ) { uno::Reference< frame::XModel > xModel( getCurrentDocument() ); - ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument(); - rDoc.GetMacroManager()->SetUserFuncVolatile( pMeth->GetName(), bVolatile); + if ( ScDocShell* pShell = excel::getDocShell( xModel )) + { + ScDocument& rDoc = pShell->GetDocument(); + rDoc.GetMacroManager()->SetUserFuncVolatile( pMeth->GetName(), bVolatile); + } } // this is bound to break when loading the document @@ -1451,7 +1455,10 @@ void SAL_CALL ScVbaApplication::setScreenUpdating(sal_Bool bUpdate) VbaApplicationBase::setScreenUpdating( bUpdate ); uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW ); + ScDocShell* pDocShell = excel::getDocShell( xModel ); + if (!pDocShell) + return; ScDocument& rDoc = pDocShell->GetDocument(); if( bUpdate ) diff --git a/sc/source/ui/vba/vbapagesetup.cxx b/sc/source/ui/vba/vbapagesetup.cxx index f487b74e0c7b..f945802e1082 100644 --- a/sc/source/ui/vba/vbapagesetup.cxx +++ b/sc/source/ui/vba/vbapagesetup.cxx @@ -77,8 +77,11 @@ OUString SAL_CALL ScVbaPageSetup::getPrintArea() ScUnoConversion::FillScRange( aRange, rRange ); aRangeList.push_back( aRange ); } - ScDocument& rDoc = excel::getDocShell( mxModel )->GetDocument(); - aRangeList.Format( aPrintArea, ScRefFlags::RANGE_ABS, rDoc, formula::FormulaGrammar::CONV_XL_A1, ',' ); + if ( ScDocShell* pShell = excel::getDocShell( mxModel )) + { + ScDocument& rDoc = pShell->GetDocument(); + aRangeList.Format( aPrintArea, ScRefFlags::RANGE_ABS, rDoc, formula::FormulaGrammar::CONV_XL_A1, ',' ); + } } return aPrintArea; diff --git a/sc/source/ui/vba/vbaworkbook.cxx b/sc/source/ui/vba/vbaworkbook.cxx index fad0ef2d1e20..c6cc1edd908d 100644 --- a/sc/source/ui/vba/vbaworkbook.cxx +++ b/sc/source/ui/vba/vbaworkbook.cxx @@ -184,9 +184,8 @@ ScVbaWorkbook::init() { if ( !ColorData.hasElements() ) ResetColors(); - uno::Reference< frame::XModel > xModel = getModel(); - if ( xModel.is() ) - excel::getDocShell( xModel )->RegisterAutomationWorkbookObject( this ); + if ( ScDocShell* pShell = excel::getDocShell( getModel() )) + pShell->RegisterAutomationWorkbookObject( this ); } ScVbaWorkbook::ScVbaWorkbook( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, css::uno::Reference< css::frame::XModel > const & xModel ) : ScVbaWorkbook_BASE( xParent, xContext, xModel ) @@ -264,18 +263,23 @@ ScVbaWorkbook::getProtectStructure() sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed() { - uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW ); - ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument(); - return rDoc.GetDocOptions().IsCalcAsShown(); + if ( ScDocShell* pShell = excel::getDocShell( getModel() )) + { + ScDocument& rDoc = pShell->GetDocument(); + return rDoc.GetDocOptions().IsCalcAsShown(); + } + return false; } void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed ) { - uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW ); - ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument(); - ScDocOptions aOpt = rDoc.GetDocOptions(); - aOpt.SetCalcAsShown( _precisionAsDisplayed ); - rDoc.SetDocOptions( aOpt ); + if ( ScDocShell* pShell = excel::getDocShell( getModel() )) + { + ScDocument& rDoc = pShell->GetDocument(); + ScDocOptions aOpt = rDoc.GetDocOptions(); + aOpt.SetCalcAsShown( _precisionAsDisplayed ); + rDoc.SetDocOptions( aOpt ); + } } OUString SAL_CALL ScVbaWorkbook::getAuthor() diff --git a/sc/source/ui/vba/vbaworksheet.cxx b/sc/source/ui/vba/vbaworksheet.cxx index 1160cd275032..46b0e131f7e0 100644 --- a/sc/source/ui/vba/vbaworksheet.cxx +++ b/sc/source/ui/vba/vbaworksheet.cxx @@ -199,7 +199,8 @@ ScVbaWorksheet::createSheetCopyInNewDoc(const OUString& aCurrSheetName) ScDocShell* pShell = excel::getDocShell( xModel ); OUString aCodeName; - pShell->GetDocument().GetCodeName( 0, aCodeName ); + if (pShell) + pShell->GetDocument().GetCodeName( 0, aCodeName ); return uno::Reference< excel::XWorksheet >( getUnoDocModule( aCodeName, pShell ), uno::UNO_QUERY_THROW ); } @@ -309,20 +310,22 @@ ScVbaWorksheet::getEnableSelection() if ( !ScVbaWorksheets::nameExists(xSpreadDoc, getName(), nTab) ) throw uno::RuntimeException("Sheet Name does not exist." ); - uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW ); - ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument(); - const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab); - bool bLockedCells = false; - bool bUnlockedCells = false; - if( pProtect ) + if ( ScDocShell* pShell = excel::getDocShell( getModel() )) { - bLockedCells = pProtect->isOptionEnabled(ScTableProtection::SELECT_LOCKED_CELLS); - bUnlockedCells = pProtect->isOptionEnabled(ScTableProtection::SELECT_UNLOCKED_CELLS); + ScDocument& rDoc = pShell->GetDocument(); + const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab); + bool bLockedCells = false; + bool bUnlockedCells = false; + if( pProtect ) + { + bLockedCells = pProtect->isOptionEnabled(ScTableProtection::SELECT_LOCKED_CELLS); + bUnlockedCells = pProtect->isOptionEnabled(ScTableProtection::SELECT_UNLOCKED_CELLS); + } + if( bLockedCells ) + return excel::XlEnableSelection::xlNoRestrictions; + if( bUnlockedCells ) + return excel::XlEnableSelection::xlUnlockedCells; } - if( bLockedCells ) - return excel::XlEnableSelection::xlNoRestrictions; - if( bUnlockedCells ) - return excel::XlEnableSelection::xlUnlockedCells; return excel::XlEnableSelection::xlNoSelection; } @@ -342,46 +345,49 @@ ScVbaWorksheet::setEnableSelection( sal_Int32 nSelection ) if ( !ScVbaWorksheets::nameExists(xSpreadDoc, getName(), nTab) ) throw uno::RuntimeException("Sheet Name does not exist." ); - uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW ); - ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument(); - const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab); - // default is xlNoSelection - bool bLockedCells = false; - bool bUnlockedCells = false; - if( nSelection == excel::XlEnableSelection::xlNoRestrictions ) + if ( ScDocShell* pShell = excel::getDocShell( getModel() )) { - bLockedCells = true; - bUnlockedCells = true; - } - else if( nSelection == excel::XlEnableSelection::xlUnlockedCells ) - { - bUnlockedCells = true; - } - if( pProtect ) - { - ScTableProtection aNewProtect(*pProtect); - aNewProtect.setOption(ScTableProtection::SELECT_LOCKED_CELLS, bLockedCells); - aNewProtect.setOption(ScTableProtection::SELECT_UNLOCKED_CELLS, bUnlockedCells); - rDoc.SetTabProtection(nTab, &aNewProtect); + ScDocument& rDoc = pShell->GetDocument(); + const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab); + // default is xlNoSelection + bool bLockedCells = false; + bool bUnlockedCells = false; + if( nSelection == excel::XlEnableSelection::xlNoRestrictions ) + { + bLockedCells = true; + bUnlockedCells = true; + } + else if( nSelection == excel::XlEnableSelection::xlUnlockedCells ) + { + bUnlockedCells = true; + } + if( pProtect ) + { + ScTableProtection aNewProtect(*pProtect); + aNewProtect.setOption(ScTableProtection::SELECT_LOCKED_CELLS, bLockedCells); + aNewProtect.setOption(ScTableProtection::SELECT_UNLOCKED_CELLS, bUnlockedCells); + rDoc.SetTabProtection(nTab, &aNewProtect); + } } - - } sal_Bool SAL_CALL ScVbaWorksheet::getAutoFilterMode() { - uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW ); - ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument(); - ScDBData* pDBData = rDoc.GetAnonymousDBData(getSheetID()); - if (pDBData) - return pDBData->HasAutoFilter(); + if ( ScDocShell* pShell = excel::getDocShell( getModel() )) + { + ScDocument& rDoc = pShell->GetDocument(); + ScDBData* pDBData = rDoc.GetAnonymousDBData(getSheetID()); + if (pDBData) + return pDBData->HasAutoFilter(); + } return false; } void SAL_CALL ScVbaWorksheet::setAutoFilterMode( sal_Bool bAutoFilterMode ) { - uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW ); - ScDocShell* pDocShell = excel::getDocShell( xModel ); + ScDocShell* pDocShell = excel::getDocShell( getModel() ); + if (!pDocShell) + return; ScDocument& rDoc = pDocShell->GetDocument(); ScDBData* pDBData = rDoc.GetAnonymousDBData(getSheetID()); if (!pDBData) @@ -481,11 +487,13 @@ ScVbaWorksheet::getProtectDrawingObjects() bool bSheetExists = ScVbaWorksheets::nameExists (xSpreadDoc, aSheetName, nTab); if ( bSheetExists ) { - uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW ); - ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument(); - const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab); - if ( pProtect ) - return pProtect->isOptionEnabled( ScTableProtection::OBJECTS ); + if ( ScDocShell* pShell = excel::getDocShell( getModel() )) + { + ScDocument& rDoc = pShell->GetDocument(); + const ScTableProtection* pProtect = rDoc.GetTabProtection(nTab); + if ( pProtect ) + return pProtect->isOptionEnabled( ScTableProtection::OBJECTS ); + } } return false; } |