tdf#145059: sc: always write dxf node for color filter for XLSX

If there is no dxf reference to color filter (for example if selected
color is not in range of available colors for current range) Excel
is not able to show color filter correctly: it is not marked as used
filter, no ability to reset, etc.

Change-Id: I65378ddd6f8f8233cda147ff9dcd28f455a58225
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/127745
Tested-by: Jenkins
Reviewed-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
diff --git a/sc/qa/unit/data/ods/tdf145059.ods b/sc/qa/unit/data/ods/tdf145059.ods
new file mode 100644
index 0000000..a76da3b
--- /dev/null
+++ b/sc/qa/unit/data/ods/tdf145059.ods
Binary files differ
diff --git a/sc/qa/unit/subsequent_export_test2.cxx b/sc/qa/unit/subsequent_export_test2.cxx
index 59fa0bf..3aef467 100644
--- a/sc/qa/unit/subsequent_export_test2.cxx
+++ b/sc/qa/unit/subsequent_export_test2.cxx
@@ -216,6 +216,7 @@ public:
    void testTdf142264ManyChartsToXLSX();
    void testTdf143929MultiColumnToODS();
    void testTdf142578();
    void testTdf145059();
    void testTdf130104_XLSXIndent();

    CPPUNIT_TEST_SUITE(ScExportTest2);
@@ -331,6 +332,7 @@ public:
    CPPUNIT_TEST(testTdf142264ManyChartsToXLSX);
    CPPUNIT_TEST(testTdf143929MultiColumnToODS);
    CPPUNIT_TEST(testTdf142578);
    CPPUNIT_TEST(testTdf145059);
    CPPUNIT_TEST(testTdf130104_XLSXIndent);

    CPPUNIT_TEST_SUITE_END();
@@ -3054,6 +3056,35 @@ void ScExportTest2::testTdf142578()
    xDocSh->DoClose();
}

void ScExportTest2::testTdf145059()
{
    ScDocShellRef xDocSh = loadDoc(u"tdf145059.", FORMAT_ODS);
    CPPUNIT_ASSERT(xDocSh);

    // Export to xlsx.
    std::shared_ptr<utl::TempFile> pXPathFile
        = ScBootstrapFixture::exportTo(&(*xDocSh), FORMAT_XLSX);
    xmlDocUniquePtr pSheet
        = XPathHelper::parseExport(pXPathFile, m_xSFactory, "xl/worksheets/sheet1.xml");
    CPPUNIT_ASSERT(pSheet);
    xmlDocUniquePtr pStyle = XPathHelper::parseExport(pXPathFile, m_xSFactory, "xl/styles.xml");
    CPPUNIT_ASSERT(pStyle);

    sal_Int32 nColorFilterDxdId
        = getXPath(pSheet, "/x:worksheet/x:autoFilter/x:filterColumn/x:colorFilter", "dxfId")
              .toInt32();

    // Ensure that dxf id is not -1
    CPPUNIT_ASSERT(nColorFilterDxdId >= 0);

    // Find color by this dxfid
    OString sDxfIdPath = "/x:styleSheet/x:dxfs/x:dxf[" + OString::number(nColorFilterDxdId + 1)
                         + "]/x:fill/x:patternFill/x:fgColor";
    assertXPath(pStyle, sDxfIdPath, "rgb", "FF4472C4");

    xDocSh->DoClose();
}

void ScExportTest2::testTdf130104_XLSXIndent()
{
    ScDocShellRef xDocSh = loadDoc(u"tdf130104_indent.", FORMAT_XLSX);
diff --git a/sc/source/filter/excel/excrecds.cxx b/sc/source/filter/excel/excrecds.cxx
index 65edd87..570167b 100644
--- a/sc/source/filter/excel/excrecds.cxx
+++ b/sc/source/filter/excel/excrecds.cxx
@@ -796,6 +796,10 @@ void XclExpAutofilter::AddColorEntry(const ScQueryEntry& rEntry)
    {
        maColorValues.push_back(
            std::make_pair(rItem.maColor, rItem.meType == ScQueryEntry::ByBackgroundColor));
        // Ensure that selected color(s) will be added to dxf: selection can be not in list
        // of already added to dfx colors taken from filter range
        if (GetDxfs().GetDxfByColor(rItem.maColor) == -1)
            GetDxfs().AddColor(rItem.maColor);
    }
}

diff --git a/sc/source/filter/excel/xestyle.cxx b/sc/source/filter/excel/xestyle.cxx
index 3898c5c..291b51c 100644
--- a/sc/source/filter/excel/xestyle.cxx
+++ b/sc/source/filter/excel/xestyle.cxx
@@ -3176,22 +3176,30 @@ XclExpDxfs::XclExpDxfs( const XclExpRoot& rRoot )
    }
}

sal_Int32 XclExpDxfs::GetDxfId( const OUString& rStyleName )
sal_Int32 XclExpDxfs::GetDxfId( const OUString& rStyleName ) const
{
    std::map<OUString, sal_Int32>::iterator itr = maStyleNameToDxfId.find(rStyleName);
    std::map<OUString, sal_Int32>::const_iterator itr = maStyleNameToDxfId.find(rStyleName);
    if(itr!= maStyleNameToDxfId.end())
        return itr->second;
    return -1;
}

sal_Int32 XclExpDxfs::GetDxfByColor(Color aColor)
sal_Int32 XclExpDxfs::GetDxfByColor(Color aColor) const
{
    std::map<Color, sal_Int32>::iterator itr = maColorToDxfId.find(aColor);
    std::map<Color, sal_Int32>::const_iterator itr = maColorToDxfId.find(aColor);
    if (itr != maColorToDxfId.end())
        return itr->second;
    return -1;
}

void XclExpDxfs::AddColor(Color aColor)
{
    maColorToDxfId.emplace(aColor, maDxf.size());

    std::unique_ptr<XclExpCellArea> pExpCellArea(new XclExpCellArea(aColor, 0));
    maDxf.push_back(std::make_unique<XclExpDxf>(GetRoot(), std::move(pExpCellArea)));
}

void XclExpDxfs::SaveXml( XclExpXmlStream& rStrm )
{
    if(maDxf.empty())
diff --git a/sc/source/filter/inc/xestyle.hxx b/sc/source/filter/inc/xestyle.hxx
index 7bacfb6..d922b45 100644
--- a/sc/source/filter/inc/xestyle.hxx
+++ b/sc/source/filter/inc/xestyle.hxx
@@ -748,8 +748,9 @@ class XclExpDxfs : public XclExpRecordBase, protected XclExpRoot
public:
    XclExpDxfs( const XclExpRoot& rRoot );

    sal_Int32 GetDxfId(const OUString& rName);
    sal_Int32 GetDxfByColor(Color aColor);
    sal_Int32 GetDxfId(const OUString& rName) const;
    sal_Int32 GetDxfByColor(Color aColor) const;
    void AddColor(Color aColor);

    virtual void SaveXml( XclExpXmlStream& rStrm) override;
private: