Kaydet (Commit) c692b9cd authored tarafından Eike Rathke's avatar Eike Rathke

adjustCopyRangeName: pass rSheet, rIndex; encapsulate nNewSheet

Change-Id: I3de5982b8b8ad179010787ed7be721be54f761af
üst 1d4974ba
...@@ -636,20 +636,38 @@ ScRangeData* copyRangeNames( SheetIndexMap& rSheetIndexMap, std::vector<ScRangeD ...@@ -636,20 +636,38 @@ ScRangeData* copyRangeNames( SheetIndexMap& rSheetIndexMap, std::vector<ScRangeD
return pRangeData; return pRangeData;
} }
/** Adjust or copy name. /** Adjust or copy name from sheet-local to sheet-local, or global to
@return TRUE if copied and caller may need to evaluate rpRangeData and rNewSheet. sheet-local if bGlobalNamesToLocal==true.
@param rSheet
On entry, the original sheet of the named expression/range, <0 global.
On return TRUE, the new sheet. Else unchanged.
@param rIndex
On entry, the original index of the named expression/range.
On return TRUE, the new index, or 0 if a new copied name couldn't be inserted. Else unchanged.
@param rpRangeData
On entry, the pointer to the original named expression/range.
On return TRUE, the pointer to the new copied name, or nullptr if hit shappened.
@return TRUE if copied and caller may need to evaluate rpRangeData and rSheet and rIndex.
FALSE if nothing to be done. FALSE if nothing to be done.
*/ */
bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, bool adjustCopyRangeName( SCTAB& rSheet, sal_uInt16& rIndex, ScRangeData*& rpRangeData,
ScRangeData*& rpRangeData, SCTAB& rNewSheet, ScDocument& rNewDoc, const ScDocument* pOldDoc, ScDocument& rNewDoc, const ScDocument* pOldDoc,
const ScAddress& rNewPos, const ScAddress& rOldPos, const bool bGlobalNamesToLocal) const ScAddress& rNewPos, const ScAddress& rOldPos, const bool bGlobalNamesToLocal)
{ {
const bool bSameDoc = (rNewDoc.GetPool() == const_cast<ScDocument*>(pOldDoc)->GetPool()); const bool bSameDoc = (rNewDoc.GetPool() == const_cast<ScDocument*>(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 // Same doc and global name, if not copied to local name, or
// sheet-local name on other sheet stays the same. // sheet-local name on other sheet stays the same.
return false; 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(), SAL_WARN_IF( !bSameDoc && nOldSheet >= 0 && nOldSheet != rOldPos.Tab(),
"sc.core", "adjustCopyRangeName - sheet-local name was on other sheet in other document"); "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? */ /* 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, ...@@ -689,8 +707,8 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex,
// Find corresponding range name in new document. // Find corresponding range name in new document.
// First search for local range name then global range names. // First search for local range name then global range names.
rNewSheet = rNewPos.Tab(); SCTAB nNewSheet = rNewPos.Tab();
ScRangeName* pRangeName = rNewDoc.GetRangeName(rNewSheet); ScRangeName* pRangeName = rNewDoc.GetRangeName(nNewSheet);
// Search local range names. // Search local range names.
if (pRangeName) if (pRangeName)
{ {
...@@ -699,7 +717,7 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, ...@@ -699,7 +717,7 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex,
// Search global range names. // Search global range names.
if (!rpRangeData && !bGlobalNamesToLocal) if (!rpRangeData && !bGlobalNamesToLocal)
{ {
rNewSheet = -1; nNewSheet = -1;
pRangeName = rNewDoc.GetRangeName(); pRangeName = rNewDoc.GetRangeName();
if (pRangeName) if (pRangeName)
rpRangeData = pRangeName->findByUpperName(aRangeName); rpRangeData = pRangeName->findByUpperName(aRangeName);
...@@ -730,10 +748,10 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, ...@@ -730,10 +748,10 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex,
if (!aReferencingNames.isEmpty(nOldTokenTabReplacement)) if (!aReferencingNames.isEmpty(nOldTokenTabReplacement))
{ {
const SCTAB nTmpOldSheet = (nOldSheet < 0 ? nOldTab : nOldSheet); const SCTAB nTmpOldSheet = (nOldSheet < 0 ? nOldTab : nOldSheet);
rNewSheet = rNewPos.Tab(); nNewSheet = rNewPos.Tab();
rpRangeData = copyRangeNames( aSheetIndexMap, aRangeDataVec, aReferencingNames, nOldTab, rpRangeData = copyRangeNames( aSheetIndexMap, aRangeDataVec, aReferencingNames, nOldTab,
pOldRangeData, rNewDoc, pOldDoc, rNewPos, rOldPos, pOldRangeData, rNewDoc, pOldDoc, rNewPos, rOldPos,
bGlobalNamesToLocal, nTmpOldSheet, rNewSheet, bSameDoc); bGlobalNamesToLocal, nTmpOldSheet, nNewSheet, bSameDoc);
} }
if ((bGlobalNamesToLocal || !bSameDoc) && !aReferencingNames.isEmpty(-1)) if ((bGlobalNamesToLocal || !bSameDoc) && !aReferencingNames.isEmpty(-1))
{ {
...@@ -745,7 +763,7 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, ...@@ -745,7 +763,7 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex,
if (!rpRangeData) if (!rpRangeData)
{ {
rpRangeData = pTmpData; rpRangeData = pTmpData;
rNewSheet = nTmpNewSheet; nNewSheet = nTmpNewSheet;
} }
} }
...@@ -777,11 +795,13 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex, ...@@ -777,11 +795,13 @@ bool adjustCopyRangeName( const SCTAB nOldSheet, const sal_uInt16 nOldIndex,
} }
else else
{ {
rNewSheet = ((nOldSheet < 0 && !bGlobalNamesToLocal) ? -1 : rNewPos.Tab()); nNewSheet = ((nOldSheet < 0 && !bGlobalNamesToLocal) ? -1 : rNewPos.Tab());
rpRangeData = copyRangeName( pOldRangeData, rNewDoc, pOldDoc, rNewPos, rOldPos, bGlobalNamesToLocal, 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; return true;
} }
...@@ -789,9 +809,9 @@ void adjustRangeName(formula::FormulaToken* pToken, ScDocument& rNewDoc, const S ...@@ -789,9 +809,9 @@ void adjustRangeName(formula::FormulaToken* pToken, ScDocument& rNewDoc, const S
const ScAddress& rNewPos, const ScAddress& rOldPos, bool bGlobalNamesToLocal) const ScAddress& rNewPos, const ScAddress& rOldPos, bool bGlobalNamesToLocal)
{ {
ScRangeData* pRangeData = nullptr; ScRangeData* pRangeData = nullptr;
SCTAB nNewSheet = rNewPos.Tab(); SCTAB nSheet = pToken->GetSheet();
if (!adjustCopyRangeName( pToken->GetSheet(), pToken->GetIndex(), pRangeData, nNewSheet, rNewDoc, pOldDoc, sal_uInt16 nIndex = pToken->GetIndex();
rNewPos, rOldPos, bGlobalNamesToLocal)) if (!adjustCopyRangeName( nSheet, nIndex, pRangeData, rNewDoc, pOldDoc, rNewPos, rOldPos, bGlobalNamesToLocal))
return; // nothing to do return; // nothing to do
if (!pRangeData) if (!pRangeData)
...@@ -802,9 +822,8 @@ void adjustRangeName(formula::FormulaToken* pToken, ScDocument& rNewDoc, const S ...@@ -802,9 +822,8 @@ void adjustRangeName(formula::FormulaToken* pToken, ScDocument& rNewDoc, const S
return; return;
} }
sal_Int32 nIndex = pRangeData->GetIndex();
pToken->SetIndex(nIndex); pToken->SetIndex(nIndex);
pToken->SetSheet(nNewSheet); pToken->SetSheet(nSheet);
} }
void adjustDBRange(formula::FormulaToken* pToken, ScDocument& rNewDoc, const ScDocument* pOldDoc) void adjustDBRange(formula::FormulaToken* pToken, ScDocument& rNewDoc, const ScDocument* pOldDoc)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment