sc/qa/extras/macros-test.cxx     |   19 +++++++++++++++++++
 sc/source/ui/unoobj/cellsuno.cxx |   39 ++++++++++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 9 deletions(-)

New commits:
commit b8df40c4fbef42c2c51258aaed03591b1ff0934c
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Tue Jun 24 20:13:25 2025 +0500
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Jul 1 09:09:17 2025 +0200

    tdf#167178: make sure to synchronize the tab in AdjustUpdatedRanges
    
    The change could adjust the tab in the aRanges; but aRange still has
    the original tab number. It needs to be updated.
    
    Regression after commit a0403b6eb25a99f6225e0f77f4b14d78f4045c9e
    (tdf#47479: Do not modify range of sheet / column / row in
    notifications, 2025-05-02).
    
    Change-Id: I248f5575ffc7fe388a65cb8bc0007ec72d9f2129
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/186906
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Tested-by: Jenkins
    (cherry picked from commit 1761796355277ba3a47143729e027a829802b4a3)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/186926
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/sc/qa/extras/macros-test.cxx b/sc/qa/extras/macros-test.cxx
index c9148f64854d..df8bb841f5ea 100644
--- a/sc/qa/extras/macros-test.cxx
+++ b/sc/qa/extras/macros-test.cxx
@@ -12,6 +12,7 @@
 #include <sal/log.hxx>
 #include <svx/svdpage.hxx>
 #include <comphelper/processfactory.hxx>
+#include <comphelper/propertyvalue.hxx>
 
 #include <conditio.hxx>
 #include <document.hxx>
@@ -938,6 +939,7 @@ CPPUNIT_TEST_FIXTURE(ScMacrosTest, testTdf47479)
     css::table::CellRangeAddress origColRange = 
xColAddressable->getRangeAddress();
     css::table::CellRangeAddress origRowRange = 
xRowAddressable->getRangeAddress();
     css::table::CellRangeAddress addressToRemove(origSheetRange.Sheet, 1, 1, 
1, 1);
+    CPPUNIT_ASSERT_EQUAL(SCTAB(0), origSheetRange.Sheet);
 
     xSheet->removeRange(addressToRemove, css::sheet::CellDeleteMode_UP);
 
@@ -974,6 +976,23 @@ CPPUNIT_TEST_FIXTURE(ScMacrosTest, testTdf47479)
     CPPUNIT_ASSERT_EQUAL(origRowRange.StartRow, currentRange.StartRow);
     CPPUNIT_ASSERT_EQUAL(origRowRange.EndColumn, currentRange.EndColumn);
     CPPUNIT_ASSERT_EQUAL(origRowRange.EndRow, currentRange.EndRow);
+
+    // tdf#167178: make sure that adding a sheet before the current one keeps 
the addressables
+    // pointing to the correct sheet
+
+    ScDocument& rDoc = *getScDoc();
+    CPPUNIT_ASSERT_EQUAL(SCTAB(1), rDoc.GetTableCount());
+
+    dispatchCommand(mxComponent, u".uno:Insert"_ustr,
+                    { comphelper::makePropertyValue(u"Name"_ustr, 
u"NewTab"_ustr),
+                      comphelper::makePropertyValue(u"Index"_ustr, 
sal_Int16(1)) });
+
+    CPPUNIT_ASSERT_EQUAL(SCTAB(2), rDoc.GetTableCount());
+
+    // Without the fix, these were 0.
+    CPPUNIT_ASSERT_EQUAL(SCTAB(1), xSheetAddressable->getRangeAddress().Sheet);
+    CPPUNIT_ASSERT_EQUAL(SCTAB(1), xColAddressable->getRangeAddress().Sheet);
+    CPPUNIT_ASSERT_EQUAL(SCTAB(1), xRowAddressable->getRangeAddress().Sheet);
 }
 
 ScMacrosTest::ScMacrosTest()
diff --git a/sc/source/ui/unoobj/cellsuno.cxx b/sc/source/ui/unoobj/cellsuno.cxx
index 0f2c66da6997..d5a92d43dc5a 100644
--- a/sc/source/ui/unoobj/cellsuno.cxx
+++ b/sc/source/ui/unoobj/cellsuno.cxx
@@ -6463,10 +6463,17 @@ void ScTableSheetObj::AdjustUpdatedRanges(UpdateRefMode 
mode)
 {
     if (mode == URM_INSDEL)
     {
+        ScRange restore = GetRange();
         ScRangeList& rRanges = AccessRanges();
-        // #101755#, tdf#47479: the range of a sheet does not change
-        rRanges.RemoveAll();
-        rRanges.push_back(GetRange());
+        if (!rRanges.empty())
+        {
+            // The tab could get changed because of the edit
+            restore.aStart.SetTab(rRanges[0].aStart.Tab());
+            restore.aEnd.SetTab(rRanges[0].aStart.Tab());
+            // #101755#, tdf#47479: the cell range of a sheet does not change
+            rRanges.RemoveAll();
+        }
+        rRanges.push_back(restore);
     }
 }
 
@@ -8277,10 +8284,17 @@ void 
ScTableColumnObj::AdjustUpdatedRanges(UpdateRefMode mode)
 {
     if (mode == URM_INSDEL)
     {
+        ScRange restore = GetRange();
         ScRangeList& rRanges = AccessRanges();
-        // tdf#47479: the range of a column does not change
-        rRanges.RemoveAll();
-        rRanges.push_back(GetRange());
+        if (!rRanges.empty())
+        {
+            // The tab could get changed because of the edit
+            restore.aStart.SetTab(rRanges[0].aStart.Tab());
+            restore.aEnd.SetTab(rRanges[0].aStart.Tab());
+            // tdf#47479: the cell range of a column does not change
+            rRanges.RemoveAll();
+        }
+        rRanges.push_back(restore);
     }
 }
 
@@ -8437,10 +8451,17 @@ void ScTableRowObj::AdjustUpdatedRanges(UpdateRefMode 
mode)
 {
     if (mode == URM_INSDEL)
     {
+        ScRange restore = GetRange();
         ScRangeList& rRanges = AccessRanges();
-        // tdf#47479: the range of a row does not change
-        rRanges.RemoveAll();
-        rRanges.push_back(GetRange());
+        if (!rRanges.empty())
+        {
+            // The tab could get changed because of the edit
+            restore.aStart.SetTab(rRanges[0].aStart.Tab());
+            restore.aEnd.SetTab(rRanges[0].aStart.Tab());
+            // tdf#47479: the cell range of a row does not change
+            rRanges.RemoveAll();
+        }
+        rRanges.push_back(restore);
     }
 }
 

Reply via email to