diff --git a/sw/inc/cellfml.hxx b/sw/inc/cellfml.hxx index e0f421aa8ef5..bbe413002a17 100644 --- a/sw/inc/cellfml.hxx +++ b/sw/inc/cellfml.hxx @@ -141,6 +141,8 @@ public: m_eNmType = NameType::External; m_bValidValue = false; } + /// Update table references in the formula. Example: "" -> "". + void RenameTableReference(std::u16string_view rOldName, std::u16string_view rNewName); void GetBoxesOfFormula(const SwTable& rTable, SwSelBoxes& rBoxes); // are all boxes valid which this formula relies on? diff --git a/sw/qa/core/uwriter.cxx b/sw/qa/core/uwriter.cxx index 7ac2c3c8364a..7ebbfc652843 100644 --- a/sw/qa/core/uwriter.cxx +++ b/sw/qa/core/uwriter.cxx @@ -1115,6 +1115,26 @@ CPPUNIT_TEST_FIXTURE(SwDocTest, testFormulas) CPPUNIT_ASSERT_EQUAL(sal_Int32(0), val.GetLong()); } +// tdf#83196 +CPPUNIT_TEST_FIXTURE(SwDocTest, testRenameTableReference) +{ + SwInsertTableOptions aOpt(SwInsertTableFlags::DefaultBorder, 0); + SwNodeIndex aIdx(m_pDoc->GetNodes().GetEndOfContent(), -1); + SwPosition aPos(aIdx); + const SwTable* pTable1 = m_pDoc->InsertTable(aOpt, aPos, 1, 1, 0); + + aIdx.Assign(m_pDoc->GetNodes().GetEndOfContent(), -1); + aPos.Assign(aIdx.GetNode(), 0); + m_pDoc->InsertTable(aOpt, aPos, 1, 2, 0); + + SwTableFormulaTest aFormula("", pTable1->GetTableNode()); + aFormula.SetFormula(u"= * + "_ustr); + aFormula.BoxNmToPtr(pTable1); + + aFormula.RenameTableReference(u"Table2", u"NewTable"); + CPPUNIT_ASSERT_EQUAL(u"= * + "_ustr, aFormula.GetFormula()); +} + CPPUNIT_TEST_FIXTURE(SwDocTest, testMarkMove) { IDocumentMarkAccess* pMarksAccess = m_pDoc->getIDocumentMarkAccess(); diff --git a/sw/source/core/fields/cellfml.cxx b/sw/source/core/fields/cellfml.cxx index 99cb9a0a58ba..349e0387da46 100644 --- a/sw/source/core/fields/cellfml.cxx +++ b/sw/source/core/fields/cellfml.cxx @@ -924,6 +924,30 @@ static OUString lcl_BoxNmToRel( const SwTable& rTable, const SwTableNode& rTable return sTmp; } +void SwTableFormula::RenameTableReference(std::u16string_view rOldName, std::u16string_view rNewName) +{ + const SwNode* pNode = GetNodeOfFormula(); + if (!pNode) + return; + + const SwTableNode* pTableNode = pNode->FindTableNode(); + if (!pTableNode) + return; + + const SwTable* pTable = &pTableNode->GetTable(); + // Convert internal pointers to text names before replacing the table name + PtrToBoxNm(pTable); + + OUString sOldPrefix = u"<"_ustr + rOldName + u"."_ustr; + OUString sFormulaText = GetFormula(); + if (sFormulaText.indexOf(sOldPrefix) == -1) + return; + + OUString sNewPrefix = u"<"_ustr + rNewName + u"."_ustr; + sFormulaText = sFormulaText.replaceAll(sOldPrefix, sNewPrefix); + SetFormula(sFormulaText); +} + void SwTableFormula::GetBoxesOfFormula( const SwTable& rTable, SwSelBoxes& rBoxes ) {