sc/source/filter/excel/xename.cxx | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-)
New commits: commit 96a07be28a28945dbf45cb91e81dec0a3a444d67 Author: Karthik Godha <[email protected]> AuthorDate: Wed Jan 21 18:36:43 2026 +0530 Commit: Michael Stahl <[email protected]> CommitDate: Thu Jan 22 16:43:48 2026 +0100 sc: Invalid external references in defined names External links are exported through `XclExpSupbookBuffer::SaveXML`, though defined names are written after external links, they are generated before writing external links through `XclExpNameManagerImpl::CreateName` . Because of this, defined names containing external references may contain invalid index. During export regenerate defined names if they contain external refs bug document: forum-mso-de-18719.xls Change-Id: I62d87d3556c474a822c91342dc5cc7bced006af6 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/197746 Tested-by: Jenkins CollaboraOffice <[email protected]> Reviewed-by: Michael Stahl <[email protected]> diff --git a/sc/source/filter/excel/xename.cxx b/sc/source/filter/excel/xename.cxx index 092ab5e53e7f..7d8de704b8a5 100644 --- a/sc/source/filter/excel/xename.cxx +++ b/sc/source/filter/excel/xename.cxx @@ -69,6 +69,8 @@ public: @param sValue the name's symbolic value */ void SetSymbol( const OUString& rValue ); + void SetScTokenArray( std::unique_ptr<ScTokenArray> pScTokArr, const ScAddress& rPos ); + /** Returns the original name (title) of this defined name. */ const OUString& GetOrigName() const { return maOrigName; } /** Returns the Excel built-in name index of this defined name. @@ -78,6 +80,9 @@ public: /** Returns the symbol value for this defined name. */ const OUString& GetSymbol() const { return msSymbol; } + const ScAddress& GetPos() const { return maPos; } + ScTokenArray* GetScTokenArray() const { return mpScTokenArray.get(); } + /** Returns true, if this is a document-global defined name. */ bool IsGlobal() const { return mnXclTab == EXC_NAME_GLOBAL; } /** Returns the Calc sheet of a local defined name. */ @@ -105,6 +110,8 @@ private: OUString msSymbol; /// The value of the symbol XclExpStringRef mxName; /// The name as Excel string object. XclTokenArrayRef mxTokArr; /// The definition of the defined name. + std::unique_ptr<ScTokenArray> mpScTokenArray; + ScAddress maPos; sal_Unicode mcBuiltIn; /// The built-in index for built-in names. SCTAB mnScTab; /// The Calc sheet index for local names. sal_uInt16 mnFlags; /// Additional flags for this defined name. @@ -242,6 +249,12 @@ void XclExpName::SetTokenArray( const XclTokenArrayRef& xTokArr ) mxTokArr = xTokArr; } +void XclExpName::SetScTokenArray( std::unique_ptr<ScTokenArray> pScTokArr, const ScAddress& rPos ) +{ + mpScTokenArray = std::move(pScTokArr); + maPos = rPos; +} + void XclExpName::SetLocalTab( SCTAB nScTab ) { OSL_ENSURE( GetTabInfo().IsExportTab( nScTab ), "XclExpName::SetLocalTab - invalid sheet index" ); @@ -348,6 +361,18 @@ void XclExpName::SaveXml( XclExpXmlStream& rStrm ) sName = "_" + sName; } + // Regenerate symbol for external references + if (ScTokenArray* pScTokArr = GetScTokenArray()) + { + formula::FormulaTokenArrayPlainIterator aIter(*pScTokArr); + formula::FormulaToken* t = aIter.First(); + while (t && !t->IsExternalRef()) + t = aIter.Next(); + + if (t) + msSymbol = XclXmlUtils::ToOUString(GetCompileFormulaContext(), GetPos(), pScTokArr); + } + rWorkbook->startElement( XML_definedName, // OOXTODO: XML_comment, "", // OOXTODO: XML_customMenu, "", @@ -658,17 +683,19 @@ sal_uInt16 XclExpNameManagerImpl::CreateName( SCTAB nTab, const ScRangeData& rRa { XclTokenArrayRef xTokArr; OUString sSymbol; + std::unique_ptr<ScTokenArray> pScTokArrCopy + = std::make_unique<ScTokenArray>(pScTokArr->CloneValue()); + // MSO requires named ranges to have absolute sheet references if ( rRangeData.HasType( ScRangeData::Type::AbsPos ) || rRangeData.HasType( ScRangeData::Type::AbsArea ) ) { // Don't modify the actual document; use a temporary copy to create the export formulas. - ScTokenArray aTokenCopy( pScTokArr->CloneValue() ); - lcl_EnsureAbs3DToken(nTab, aTokenCopy.FirstToken()); + lcl_EnsureAbs3DToken(nTab, pScTokArrCopy->FirstToken()); - xTokArr = GetFormulaCompiler().CreateFormula(EXC_FMLATYPE_NAME, aTokenCopy); + xTokArr = GetFormulaCompiler().CreateFormula(EXC_FMLATYPE_NAME, *pScTokArrCopy); if ( GetOutput() != EXC_OUTPUT_BINARY ) { - ScCompiler aComp(GetDoc(), rRangeData.GetPos(), aTokenCopy, + ScCompiler aComp(GetDoc(), rRangeData.GetPos(), *pScTokArrCopy, formula::FormulaGrammar::GRAM_OOXML); aComp.CreateStringFromTokenArray( sSymbol ); } @@ -683,6 +710,7 @@ sal_uInt16 XclExpNameManagerImpl::CreateName( SCTAB nTab, const ScRangeData& rRa } xName->SetTokenArray( xTokArr ); xName->SetSymbol( sSymbol ); + xName->SetScTokenArray( std::move(pScTokArrCopy), rRangeData.GetPos() ); /* Try to replace by existing built-in name - complete token array is needed for comparison, and due to the recursion problem above this
