Related: tdf#150312 "Repair" broken table:base-cell-address of defined names

... to not lose them entirely, though this may shift relative
addressing, which may be equally bad because there is no
indication and can go unnoticed. Most defined ranges use absolute
addressing though and formula expressions are rarely affected.

Change-Id: If57ff9274e673a4eb43c656698f6ed43e8415458
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139706
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Jenkins
diff --git a/sc/source/filter/xml/xmlimprt.cxx b/sc/source/filter/xml/xmlimprt.cxx
index b72cf35..5f65439 100644
--- a/sc/source/filter/xml/xmlimprt.cxx
+++ b/sc/source/filter/xml/xmlimprt.cxx
@@ -1258,10 +1258,11 @@ class RangeNameInserter
{
    ScDocument&  mrDoc;
    ScRangeName& mrRangeName;
    SCTAB        mnTab;

public:
    RangeNameInserter(ScDocument& rDoc, ScRangeName& rRangeName) :
        mrDoc(rDoc), mrRangeName(rRangeName) {}
    RangeNameInserter(ScDocument& rDoc, ScRangeName& rRangeName, SCTAB nTab) :
        mrDoc(rDoc), mrRangeName(rRangeName), mnTab(nTab) {}

    void operator() (const ScMyNamedExpression& p) const
    {
@@ -1282,6 +1283,17 @@ public:
        bool bSuccess = ScRangeStringConverter::GetAddressFromString(
            aPos, p.sBaseCellAddress, mrDoc, FormulaGrammar::CONV_OOO, nOffset);

        if (!bSuccess)
        {
            SAL_WARN("sc.filter", "No conversion from table:base-cell-address '" << p.sBaseCellAddress
                    << "' for name '" << p.sName << "' on sheet " << mnTab);
            // Do not lose the defined name. Relative addressing in
            // content/expression, if any, will be broken though.
            // May had happened due to tdf#150312.
            aPos.SetTab(mnTab < 0 ? 0 : mnTab);
            bSuccess = true;
        }

        if (bSuccess)
        {
            OUString aContent = p.sContent;
@@ -1307,7 +1319,8 @@ void ScXMLImport::SetNamedRanges()

    // Insert the namedRanges
    ScRangeName* pRangeNames = pDoc->GetRangeName();
    ::std::for_each(m_aMyNamedExpressions.begin(), m_aMyNamedExpressions.end(), RangeNameInserter(*pDoc, *pRangeNames));
    ::std::for_each(m_aMyNamedExpressions.begin(), m_aMyNamedExpressions.end(),
            RangeNameInserter(*pDoc, *pRangeNames, -1));
}

void ScXMLImport::SetSheetNamedRanges()
@@ -1323,7 +1336,7 @@ void ScXMLImport::SetSheetNamedRanges()
            continue;

        const ScMyNamedExpressions& rNames = itr.second;
        ::std::for_each(rNames.begin(), rNames.end(), RangeNameInserter(*pDoc, *pRangeNames));
        ::std::for_each(rNames.begin(), rNames.end(), RangeNameInserter(*pDoc, *pRangeNames, nTab));
    }
}