From c692b9cda192c8e31ddd59a9a9598c7e798e80d9 Mon Sep 17 00:00:00 2001 From: Eike Rathke Date: Thu, 28 Apr 2016 19:07:56 +0200 Subject: adjustCopyRangeName: pass rSheet, rIndex; encapsulate nNewSheet Change-Id: I3de5982b8b8ad179010787ed7be721be54f761af --- sc/source/core/data/formulacell.cxx | 55 +++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 18 deletions(-) (limited to 'sc') diff --git a/sc/source/core/data/formulacell.cxx b/sc/source/core/data/formulacell.cxx index 280c1eb9f3b7..ec7b454d1ae3 100644 --- a/sc/source/core/data/formulacell.cxx +++ b/sc/source/core/data/formulacell.cxx @@ -636,20 +636,38 @@ ScRangeData* copyRangeNames( SheetIndexMap& rSheetIndexMap, std::vector(pOldDoc)->GetPool()); - if (bSameDoc && ((nOldSheet < 0 && !bGlobalNamesToLocal) || (nOldSheet >= 0 && nOldSheet != rOldPos.Tab()))) + if (bSameDoc && ((rSheet < 0 && !bGlobalNamesToLocal) || (rSheet >= 0 && rSheet != rOldPos.Tab()))) // Same doc and global name, if not copied to local name, or // sheet-local name on other sheet stays the same. return false; + // Ensure we don't fiddle with the references until exit. + const SCTAB nOldSheet = rSheet; + const sal_uInt16 nOldIndex = rIndex; + SAL_WARN_IF( !bSameDoc && nOldSheet >= 0 && nOldSheet != rOldPos.Tab(), "sc.core", "adjustCopyRangeName - sheet-local name was on other sheet in other document"); /* TODO: can we do something about that? e.g. loop over sheets? */ @@ -689,8 +707,8 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, // Find corresponding range name in new document. // First search for local range name then global range names. - rNewSheet = rNewPos.Tab(); - ScRangeName* pRangeName = rNewDoc.GetRangeName(rNewSheet); + SCTAB nNewSheet = rNewPos.Tab(); + ScRangeName* pRangeName = rNewDoc.GetRangeName(nNewSheet); // Search local range names. if (pRangeName) { @@ -699,7 +717,7 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, // Search global range names. if (!rpRangeData && !bGlobalNamesToLocal) { - rNewSheet = -1; + nNewSheet = -1; pRangeName = rNewDoc.GetRangeName(); if (pRangeName) rpRangeData = pRangeName->findByUpperName(aRangeName); @@ -730,10 +748,10 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, if (!aReferencingNames.isEmpty(nOldTokenTabReplacement)) { const SCTAB nTmpOldSheet = (nOldSheet < 0 ? nOldTab : nOldSheet); - rNewSheet = rNewPos.Tab(); + nNewSheet = rNewPos.Tab(); rpRangeData = copyRangeNames( aSheetIndexMap, aRangeDataVec, aReferencingNames, nOldTab, pOldRangeData, rNewDoc, pOldDoc, rNewPos, rOldPos, - bGlobalNamesToLocal, nTmpOldSheet, rNewSheet, bSameDoc); + bGlobalNamesToLocal, nTmpOldSheet, nNewSheet, bSameDoc); } if ((bGlobalNamesToLocal || !bSameDoc) && !aReferencingNames.isEmpty(-1)) { @@ -745,7 +763,7 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, if (!rpRangeData) { rpRangeData = pTmpData; - rNewSheet = nTmpNewSheet; + nNewSheet = nTmpNewSheet; } } @@ -777,11 +795,13 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, } else { - rNewSheet = ((nOldSheet < 0 && !bGlobalNamesToLocal) ? -1 : rNewPos.Tab()); + nNewSheet = ((nOldSheet < 0 && !bGlobalNamesToLocal) ? -1 : rNewPos.Tab()); rpRangeData = copyRangeName( pOldRangeData, rNewDoc, pOldDoc, rNewPos, rOldPos, bGlobalNamesToLocal, - nOldSheet, rNewSheet, bSameDoc); + nOldSheet, nNewSheet, bSameDoc); } } + rSheet = nNewSheet; + rIndex = rpRangeData ? rpRangeData->GetIndex() : 0; // 0 means not inserted return true; } @@ -789,9 +809,9 @@ void adjustRangeName(formula::FormulaToken* pToken, ScDocument& rNewDoc, const S const ScAddress& rNewPos, const ScAddress& rOldPos, bool bGlobalNamesToLocal) { ScRangeData* pRangeData = nullptr; - SCTAB nNewSheet = rNewPos.Tab(); - if (!adjustCopyRangeName( pToken->GetSheet(), pToken->GetIndex(), pRangeData, nNewSheet, rNewDoc, pOldDoc, - rNewPos, rOldPos, bGlobalNamesToLocal)) + SCTAB nSheet = pToken->GetSheet(); + sal_uInt16 nIndex = pToken->GetIndex(); + if (!adjustCopyRangeName( nSheet, nIndex, pRangeData, rNewDoc, pOldDoc, rNewPos, rOldPos, bGlobalNamesToLocal)) return; // nothing to do if (!pRangeData) @@ -802,9 +822,8 @@ void adjustRangeName(formula::FormulaToken* pToken, ScDocument& rNewDoc, const S return; } - sal_Int32 nIndex = pRangeData->GetIndex(); pToken->SetIndex(nIndex); - pToken->SetSheet(nNewSheet); + pToken->SetSheet(nSheet); } void adjustDBRange(formula::FormulaToken* pToken, ScDocument& rNewDoc, const ScDocument* pOldDoc) -- cgit