diff options
author | Dennis Francis <dennis.francis@collabora.com> | 2019-04-05 22:04:13 +0530 |
---|---|---|
committer | Andras Timar <andras.timar@collabora.com> | 2019-04-08 17:55:31 +0200 |
commit | 97af58093978d8e6b9d90eedcc59141304e7200e (patch) | |
tree | 6d197b84068b5ca7e096cd60b22e1ed61b4721da | |
parent | ea3a1b075154a665d30aaac6513812ceb839f64b (diff) |
tdf#123421 : xlsx export : Don't write data field entry...
under colFields tag if there is only one data-field.
<colFields count=[*]>
<field x="-2"/> <--- -2 indicates data field.
</colFields>
Excel 2013/2016 seems to crash at the presence of '<field x="-2"/>'
in colFields when there is only one data-field.
Additionally, call GetOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE)
on all ScDPObject's in non-const mode, so that the internal
pOuput member of ScDPObject is populated. Otherwise the
const GetOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE)
call always return an invalid range.
This also adds 2 unit tests :-
1. To check the presence of <field x="-2"/> in colFields tag
if there are more than one data-fields.
2. To ensure the absence of <field x="-2"/> in colFields tag
if there is only one data-field.
Change-Id: I8f470bd1ab883f73586f04a3fcc30e3fbf948c4a
Reviewed-on: https://gerrit.libreoffice.org/70316
Tested-by: Jenkins
Reviewed-by: Andras Timar <andras.timar@collabora.com>
-rw-r--r-- | sc/inc/dpsave.hxx | 2 | ||||
-rw-r--r-- | sc/qa/unit/data/ods/tdf123421_1datafield.ods | bin | 0 -> 12293 bytes | |||
-rw-r--r-- | sc/qa/unit/data/ods/tdf123421_2datafields.ods | bin | 0 -> 12195 bytes | |||
-rw-r--r-- | sc/qa/unit/pivottable_filters_test.cxx | 56 | ||||
-rw-r--r-- | sc/source/filter/excel/xepivotxml.cxx | 11 |
5 files changed, 65 insertions, 4 deletions
diff --git a/sc/inc/dpsave.hxx b/sc/inc/dpsave.hxx index 7558a6b2588d..49cb240ffd24 100644 --- a/sc/inc/dpsave.hxx +++ b/sc/inc/dpsave.hxx @@ -315,7 +315,7 @@ public: ScDPSaveDimension* GetInnermostDimension(css::sheet::DataPilotFieldOrientation nOrientation); ScDPSaveDimension* GetFirstDimension(css::sheet::DataPilotFieldOrientation eOrientation); - long GetDataDimensionCount() const; + SC_DLLPUBLIC long GetDataDimensionCount() const; void SetPosition( ScDPSaveDimension* pDim, long nNew ); SC_DLLPUBLIC void SetColumnGrand( bool bSet ); diff --git a/sc/qa/unit/data/ods/tdf123421_1datafield.ods b/sc/qa/unit/data/ods/tdf123421_1datafield.ods Binary files differnew file mode 100644 index 000000000000..985d6c0c44b6 --- /dev/null +++ b/sc/qa/unit/data/ods/tdf123421_1datafield.ods diff --git a/sc/qa/unit/data/ods/tdf123421_2datafields.ods b/sc/qa/unit/data/ods/tdf123421_2datafields.ods Binary files differnew file mode 100644 index 000000000000..c5d8a09647b0 --- /dev/null +++ b/sc/qa/unit/data/ods/tdf123421_2datafields.ods diff --git a/sc/qa/unit/pivottable_filters_test.cxx b/sc/qa/unit/pivottable_filters_test.cxx index 014c1fdab035..739af618ec7d 100644 --- a/sc/qa/unit/pivottable_filters_test.cxx +++ b/sc/qa/unit/pivottable_filters_test.cxx @@ -58,6 +58,8 @@ public: // Export void testPivotTableExportXLSX(); + void testPivotTableExportXLSXSingleDataField(); + void testPivotTableExportXLSXMultipleDataFields(); void testPivotCacheExportXLSX(); void testPivotTableXLSX(); void testPivotTableTwoDataFieldsXLSX(); @@ -98,6 +100,8 @@ public: CPPUNIT_TEST(testTdf112501); CPPUNIT_TEST(testPivotTableExportXLSX); + CPPUNIT_TEST(testPivotTableExportXLSXSingleDataField); + CPPUNIT_TEST(testPivotTableExportXLSXMultipleDataFields); CPPUNIT_TEST(testPivotCacheExportXLSX); CPPUNIT_TEST(testPivotTableXLSX); CPPUNIT_TEST(testPivotTableTwoDataFieldsXLSX); @@ -758,6 +762,58 @@ void ScPivotTableFiltersTest::testPivotTableExportXLSX() xShell->DoClose(); } +void ScPivotTableFiltersTest::testPivotTableExportXLSXSingleDataField() +{ + ScDocShellRef xShell = loadDoc("tdf123421_1datafield.", FORMAT_ODS); + CPPUNIT_ASSERT(xShell.is()); + + std::shared_ptr<utl::TempFile> pXPathFile + = ScBootstrapFixture::exportTo(&(*xShell), FORMAT_XLSX); + xmlDocPtr pTable + = XPathHelper::parseExport(pXPathFile, m_xSFactory, "xl/pivotTables/pivotTable1.xml"); + CPPUNIT_ASSERT(pTable); + + assertXPath(pTable, "/x:pivotTableDefinition/x:location", "ref", "A3:B6"); + assertXPath(pTable, "/x:pivotTableDefinition/x:location", "firstHeaderRow", "1"); + assertXPath(pTable, "/x:pivotTableDefinition/x:location", "firstDataRow", "1"); + assertXPath(pTable, "/x:pivotTableDefinition/x:location", "firstDataCol", "1"); + assertXPath(pTable, "/x:pivotTableDefinition/x:dataFields", "count", "1"); + + // There should not be any colFields tag, before the fix there used to be a singleton with + // <field x="-2"/> as child node. + assertXPath(pTable, "/x:pivotTableDefinition/x:colFields", 0); + + xShell->DoClose(); +} + +void ScPivotTableFiltersTest::testPivotTableExportXLSXMultipleDataFields() +{ + ScDocShellRef xShell = loadDoc("tdf123421_2datafields.", FORMAT_ODS); + CPPUNIT_ASSERT(xShell.is()); + + std::shared_ptr<utl::TempFile> pXPathFile + = ScBootstrapFixture::exportTo(&(*xShell), FORMAT_XLSX); + xmlDocPtr pTable + = XPathHelper::parseExport(pXPathFile, m_xSFactory, "xl/pivotTables/pivotTable1.xml"); + CPPUNIT_ASSERT(pTable); + + assertXPath(pTable, "/x:pivotTableDefinition/x:location", "ref", "A1:C6"); + assertXPath(pTable, "/x:pivotTableDefinition/x:location", "firstHeaderRow", "1"); + assertXPath(pTable, "/x:pivotTableDefinition/x:location", "firstDataRow", "2"); + assertXPath(pTable, "/x:pivotTableDefinition/x:location", "firstDataCol", "1"); + + assertXPath(pTable, "/x:pivotTableDefinition/x:dataFields", "count", "2"); + + // There should be a single colFields tag with sole child node + // <field x="-2"/>. + assertXPath(pTable, "/x:pivotTableDefinition/x:colFields", 1); + assertXPath(pTable, "/x:pivotTableDefinition/x:colFields", "count", "1"); + assertXPath(pTable, "/x:pivotTableDefinition/x:colFields/x:field", 1); + assertXPath(pTable, "/x:pivotTableDefinition/x:colFields/x:field", "x", "-2"); + + xShell->DoClose(); +} + void ScPivotTableFiltersTest::testPivotCacheExportXLSX() { // tdf#89139 FILESAVE xlsx pivot table corrupted after save with LO and re-open with MS Office diff --git a/sc/source/filter/excel/xepivotxml.cxx b/sc/source/filter/excel/xepivotxml.cxx index 30d4fa7a8424..1a47e2cfeb5b 100644 --- a/sc/source/filter/excel/xepivotxml.cxx +++ b/sc/source/filter/excel/xepivotxml.cxx @@ -426,6 +426,7 @@ void XclExpXmlPivotTableManager::Initialize() { ScDPObject& rDPObj = (*pDPColl)[i]; rDPObj.SyncAllDimensionMembers(); + (void)rDPObj.GetOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE); } // Go through the caches first. @@ -606,6 +607,7 @@ void XclExpXmlPivotTables::SavePivotTableXml( XclExpXmlStream& rStrm, const ScDP std::vector<long> aPageFields; std::vector<DataField> aDataFields; + long nDataDimCount = rSaveData.GetDataDimensionCount(); // Use dimensions in the save data to get their correct ordering. // Dimension order here is significant as they specify the order of // appearance in each axis. @@ -637,6 +639,8 @@ void XclExpXmlPivotTables::SavePivotTableXml( XclExpXmlStream& rStrm, const ScDP switch (eOrient) { case sheet::DataPilotFieldOrientation_COLUMN: + if (nPos == -2 && nDataDimCount <= 1) + break; aColFields.push_back(nPos); break; case sheet::DataPilotFieldOrientation_ROW: @@ -684,15 +688,16 @@ void XclExpXmlPivotTables::SavePivotTableXml( XclExpXmlStream& rStrm, const ScDP sal_Int32 nFirstDataRow = 2; sal_Int32 nFirstDataCol = 1; ScRange aResRange = rDPObj.GetOutputRangeByType(sheet::DataPilotOutputRangeType::RESULT); + + if (!aOutRange.IsValid()) + aOutRange = rDPObj.GetOutRange(); + if (aOutRange.IsValid() && aResRange.IsValid()) { nFirstDataRow = aResRange.aStart.Row() - aOutRange.aStart.Row(); nFirstDataCol = aResRange.aStart.Col() - aOutRange.aStart.Col(); } - if (!aOutRange.IsValid()) - aOutRange = rDPObj.GetOutRange(); - pPivotStrm->write("<")->writeId(XML_location); rStrm.WriteAttributes(XML_ref, XclXmlUtils::ToOString(aOutRange), |