sc/inc/rangeutl.hxx              |    9 +++++++++
 sc/source/core/tool/rangenam.cxx |    4 ++--
 sc/source/core/tool/rangeutl.cxx |    2 +-
 sc/source/core/tool/token.cxx    |    4 ++--
 4 files changed, 14 insertions(+), 5 deletions(-)

New commits:
commit 9a6c350d8dff4b5228ec7451006115e387f819f2
Author:     Eike Rathke <er...@redhat.com>
AuthorDate: Fri Sep 9 12:54:14 2022 +0200
Commit:     Eike Rathke <er...@redhat.com>
CommitDate: Fri Sep 9 15:53:46 2022 +0200

    Resolves: tdf#150312 Move base-cell-address to a valid sheet upon deletion
    
    ... and adjust references accordingly. For
    ScTokenArray::AdjustReferenceOnDeletedTab() that now also moves
    the base of the first deleted sheet not only after, which was a
    one-off before.
    
    Change-Id: I4c19e8b8a7318ba03b6a7d0b2dfaae5d05a51a29
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139720
    Reviewed-by: Eike Rathke <er...@redhat.com>
    Tested-by: Jenkins

diff --git a/sc/inc/rangeutl.hxx b/sc/inc/rangeutl.hxx
index 33f531444280..4524226e43ca 100644
--- a/sc/inc/rangeutl.hxx
+++ b/sc/inc/rangeutl.hxx
@@ -276,6 +276,15 @@ struct SC_DLLPUBLIC ScRangeUpdater
     ScRangeUpdater() = delete;
 
     static void UpdateInsertTab(ScAddress& rAddr, const 
sc::RefUpdateInsertTabContext& rCxt);
+
+    /** This is for the base-cell-address of a defined name or conditional
+        format, not for references. A sheet position on or after the start of
+        the deleted range is moved towards the beginning by the amount of
+        deleted sheets, within the deleted range to the front of that or set to
+        0 (as there is always at least one sheet in a document) if the position
+        would result in a negative value, e.g. if position was 0 and (only)
+        sheet 0 is deleted it would had become -1.
+     */
     static void UpdateDeleteTab(ScAddress& rAddr, const 
sc::RefUpdateDeleteTabContext& rCxt);
 };
 
diff --git a/sc/source/core/tool/rangenam.cxx b/sc/source/core/tool/rangenam.cxx
index 66cdf2b74c9e..b5578ca26590 100644
--- a/sc/source/core/tool/rangenam.cxx
+++ b/sc/source/core/tool/rangenam.cxx
@@ -27,6 +27,7 @@
 #include <token.hxx>
 #include <tokenarray.hxx>
 #include <rangenam.hxx>
+#include <rangeutl.hxx>
 #include <global.hxx>
 #include <compiler.hxx>
 #include <refupdat.hxx>
@@ -407,8 +408,7 @@ void ScRangeData::UpdateDeleteTab( 
sc::RefUpdateDeleteTabContext& rCxt, SCTAB nL
     if (aRes.mbReferenceModified)
         rCxt.maUpdatedNames.setUpdatedName(nLocalTab, nIndex);
 
-    if (rCxt.mnDeletePos <= aPos.Tab())
-        aPos.IncTab(-rCxt.mnSheets);
+    ScRangeUpdater::UpdateDeleteTab( aPos, rCxt);
 }
 
 void ScRangeData::UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt, SCTAB 
nLocalTab )
diff --git a/sc/source/core/tool/rangeutl.cxx b/sc/source/core/tool/rangeutl.cxx
index c5901c834b40..6eb1cf52ff48 100644
--- a/sc/source/core/tool/rangeutl.cxx
+++ b/sc/source/core/tool/rangeutl.cxx
@@ -1061,7 +1061,7 @@ void ScRangeUpdater::UpdateDeleteTab(ScAddress& rAddr, 
const sc::RefUpdateDelete
 {
     if (rCxt.mnDeletePos <= rAddr.Tab())
     {
-        rAddr.IncTab(-rCxt.mnSheets);
+        rAddr.SetTab( std::max<SCTAB>(0, rAddr.Tab() - rCxt.mnSheets));
     }
 }
 
diff --git a/sc/source/core/tool/token.cxx b/sc/source/core/tool/token.cxx
index 1ad3df0889ef..4315f017e67c 100644
--- a/sc/source/core/tool/token.cxx
+++ b/sc/source/core/tool/token.cxx
@@ -34,6 +34,7 @@
 #include <formula/opcode.hxx>
 #include <jumpmatrix.hxx>
 #include <rangeseq.hxx>
+#include <rangeutl.hxx>
 #include <externalrefmgr.hxx>
 #include <document.hxx>
 #include <refupdatecontext.hxx>
@@ -4234,8 +4235,7 @@ sc::RefUpdateResult 
ScTokenArray::AdjustReferenceOnDeletedTab( const sc::RefUpda
 {
     sc::RefUpdateResult aRes;
     ScAddress aNewPos = rOldPos;
-    if (rCxt.mnDeletePos < rOldPos.Tab())
-        aNewPos.IncTab(-1*rCxt.mnSheets);
+    ScRangeUpdater::UpdateDeleteTab( aNewPos, rCxt);
 
     TokenPointers aPtrs( pCode.get(), nLen, pRPN, nRPN);
     for (size_t j=0; j<2; ++j)

Reply via email to