sc/inc/table.hxx                  |    4 ++++
 sc/qa/unit/data/ods/tdf156815.ods |binary
 sc/qa/unit/ucalc_solver.cxx       |   33 +++++++++++++++++++++++++++++++++
 sc/source/core/data/document.cxx  |    5 +++++
 sc/source/ui/inc/docfunc.hxx      |    2 +-
 5 files changed, 43 insertions(+), 1 deletion(-)

New commits:
commit cb46ad4c4602fbb6aeab482e9370e31495e12cfe
Author:     Rafael Lima <rafael.palma.l...@gmail.com>
AuthorDate: Tue Sep 12 19:17:47 2023 +0200
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Wed Oct 11 00:05:52 2023 +0200

    tdf#156815 Reset solver settings when a sheet is renamed
    
    When a sheet is renamed, the SolverSettings object needs to be reset so 
that the updated references of the named ranges are reloaded the next time the 
Solver dialog is opened.
    
    Change-Id: I8d501bb5b52f6a69bc899a62863893744d80dc69
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/156872
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git a/sc/inc/table.hxx b/sc/inc/table.hxx
index 34e0f9d27784..68b4c614c68b 100644
--- a/sc/inc/table.hxx
+++ b/sc/inc/table.hxx
@@ -451,6 +451,10 @@ public:
 
     SC_DLLPUBLIC std::shared_ptr<sc::SolverSettings> GetSolverSettings();
 
+    // tdf#156815 Sets the solver settings object to nullptr to force 
reloading Solver settings the
+    // next time the dialog is opened. This is required when sheets are renamed
+    void ResetSolverSettings() { m_pSolverSettings.reset(); }
+
     /**
      * Takes ownership of pCell
      *
diff --git a/sc/qa/unit/data/ods/tdf156815.ods 
b/sc/qa/unit/data/ods/tdf156815.ods
new file mode 100644
index 000000000000..cc797ee8619b
Binary files /dev/null and b/sc/qa/unit/data/ods/tdf156815.ods differ
diff --git a/sc/qa/unit/ucalc_solver.cxx b/sc/qa/unit/ucalc_solver.cxx
index 7a8d76cc7534..47770ec0c0e5 100644
--- a/sc/qa/unit/ucalc_solver.cxx
+++ b/sc/qa/unit/ucalc_solver.cxx
@@ -10,6 +10,7 @@
 #include <sal/config.h>
 #include "helper/qahelper.hxx"
 #include <document.hxx>
+#include <docfunc.hxx>
 #include <table.hxx>
 #include <SolverSettings.hxx>
 
@@ -130,4 +131,36 @@ CPPUNIT_TEST_FIXTURE(SolverTest, testSingleModel)
     TestConstraintsModelA(pSettings.get());
 }
 
+// Tests if references remain valid after a sheet is renamed
+CPPUNIT_TEST_FIXTURE(SolverTest, tdf156815)
+{
+    createScDoc("ods/tdf156815.ods");
+    ScDocument* pDoc = getScDoc();
+    ScTable* pTable = pDoc->FetchTable(0);
+    std::shared_ptr<sc::SolverSettings> pSettings = 
pTable->GetSolverSettings();
+    CPPUNIT_ASSERT(pSettings);
+
+    // Check current values in the solver model
+    CPPUNIT_ASSERT_EQUAL(OUString("$Sheet2.$A$1"), 
pSettings->GetParameter(SP_OBJ_CELL));
+    CPPUNIT_ASSERT_EQUAL(OUString("$Sheet2.$A$3:$B$3"), 
pSettings->GetParameter(SP_VAR_CELLS));
+
+    std::vector<ModelConstraint> aConstraints = pSettings->GetConstraints();
+    CPPUNIT_ASSERT_EQUAL(OUString("$Sheet2.$A$2"), aConstraints[0].aLeftStr);
+    CPPUNIT_ASSERT_EQUAL(OUString("$Sheet2.$B$2"), aConstraints[0].aRightStr);
+
+    // Rename Sheet2 to NewName
+    ScDocFunc& rDocFunc = getScDocShell()->GetDocFunc();
+    rDocFunc.RenameTable(1, "NewName", false, true);
+
+    // Check whether the ranges where updated
+    pSettings = pTable->GetSolverSettings();
+    CPPUNIT_ASSERT(pSettings);
+    CPPUNIT_ASSERT_EQUAL(OUString("$NewName.$A$1"), 
pSettings->GetParameter(SP_OBJ_CELL));
+    CPPUNIT_ASSERT_EQUAL(OUString("$NewName.$A$3:$B$3"), 
pSettings->GetParameter(SP_VAR_CELLS));
+
+    aConstraints = pSettings->GetConstraints();
+    CPPUNIT_ASSERT_EQUAL(OUString("$NewName.$A$2"), aConstraints[0].aLeftStr);
+    CPPUNIT_ASSERT_EQUAL(OUString("$NewName.$B$2"), aConstraints[0].aRightStr);
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sc/source/core/data/document.cxx b/sc/source/core/data/document.cxx
index 0880fdf8a857..8996577b588e 100644
--- a/sc/source/core/data/document.cxx
+++ b/sc/source/core/data/document.cxx
@@ -865,7 +865,12 @@ bool ScDocument::RenameTab( SCTAB nTab, const OUString& 
rName, bool bExternalDoc
             for (const auto& pTable : maTabs)
             {
                 if (pTable)
+                {
                     pTable->SetStreamValid( false );
+                    // tdf#156815 Reset solver settings so next time they're 
loaded they come with
+                    // the updated sheet name
+                    pTable->ResetSolverSettings();
+                }
             }
 
             if (comphelper::LibreOfficeKit::isActive() && GetDrawLayer())
diff --git a/sc/source/ui/inc/docfunc.hxx b/sc/source/ui/inc/docfunc.hxx
index 6419e285d5b7..aa9755566ef3 100644
--- a/sc/source/ui/inc/docfunc.hxx
+++ b/sc/source/ui/inc/docfunc.hxx
@@ -147,7 +147,7 @@ public:
                                        bool bCut, bool bRecord, bool bPaint, 
bool bApi );
 
     SC_DLLPUBLIC bool InsertTable( SCTAB nTab, const OUString& rName, bool 
bRecord, bool bApi );
-    bool            RenameTable( SCTAB nTab, const OUString& rName, bool 
bRecord, bool bApi );
+    SC_DLLPUBLIC bool RenameTable( SCTAB nTab, const OUString& rName, bool 
bRecord, bool bApi );
     bool            DeleteTable( SCTAB nTab, bool bRecord );
 
     bool            SetTabBgColor( SCTAB nTab, const Color& rColor, bool 
bRecord, bool bApi );

Reply via email to