sw: add SwTableFormula::RenameTableReference

Add a method for updating table references inside formulas.

Example:
Formula: =<Table1.A1> + <Table2.B1>
Renaming "Table1" to "NewTable"
Result:  =<NewTable.A1> + <Table2.B1>

Note: Tables with a dot ('.') in their name may not update correctly.
See: tdf#171783

Change-Id: I4f3695afcd7003b8253ae2f9247f193245e21f07
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/203802
Tested-by: Jenkins
Reviewed-by: Jonathan Clark <jonathan@libreoffice.org>
This commit is contained in:
Jakub Trzebiatowski
2026-04-25 11:26:51 +02:00
committed by Jakub Trzebiatowski
parent d17afd6400
commit 741e249764
3 changed files with 46 additions and 0 deletions
+2
View File
@@ -141,6 +141,8 @@ public:
m_eNmType = NameType::External;
m_bValidValue = false;
}
/// Update table references in the formula. Example: "<Table1.A1>" -> "<NewTable.A1>".
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?
+20
View File
@@ -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"=<Table2.A1> * <Table1.A1> + <Table2.B1>"_ustr);
aFormula.BoxNmToPtr(pTable1);
aFormula.RenameTableReference(u"Table2", u"NewTable");
CPPUNIT_ASSERT_EQUAL(u"=<NewTable.A1> * <Table1.A1> + <NewTable.B1>"_ustr, aFormula.GetFormula());
}
CPPUNIT_TEST_FIXTURE(SwDocTest, testMarkMove)
{
IDocumentMarkAccess* pMarksAccess = m_pDoc->getIDocumentMarkAccess();
+24
View File
@@ -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 )
{