From bbf341069078701abb22fa1598dbd186c1185e62 Mon Sep 17 00:00:00 2001 From: Tünde Tóth Date: Wed, 24 Mar 2021 14:07:15 +0100 Subject: tdf#99913 XLSX import: set filtered flag for rows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hidden by AutoFilter to support copying the result of filtering. Unlike ODS and XLS, XLSX doesn't differentiates filtered and manually hidden rows, and without this fix, copy of the unupdated data of the loaded filtering contained the hidden rows, too. Change-Id: I390d1a84b9bf275f3d3782756553b2f236487758 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113040 Tested-by: László Németh Reviewed-by: László Németh Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113382 Tested-by: Jenkins Reviewed-by: Xisco Fauli (cherry picked from commit adef1cc017517882f4630e31fa643102c21faad0) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114914 Tested-by: Gabor Kelemen Reviewed-by: Gabor Kelemen --- sc/inc/document.hxx | 2 +- sc/qa/unit/uicalc/data/tdf99913.xlsx | Bin 0 -> 8829 bytes sc/qa/unit/uicalc/uicalc.cxx | 9 +++++ sc/source/filter/oox/worksheethelper.cxx | 65 ++++++++++++++++++++++++++----- 4 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 sc/qa/unit/uicalc/data/tdf99913.xlsx diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx index fa13d9bae60e..f3af1b1be236 100644 --- a/sc/inc/document.hxx +++ b/sc/inc/document.hxx @@ -1943,7 +1943,7 @@ public: SC_DLLPUBLIC SCROW LastVisibleRow(SCROW nStartRow, SCROW nEndRow, SCTAB nTab) const; SCROW CountVisibleRows(SCROW nStartRow, SCROW nEndRow, SCTAB nTab) const; - bool RowFiltered(SCROW nRow, SCTAB nTab, SCROW* pFirstRow = nullptr, SCROW* pLastRow = nullptr) const; + SC_DLLPUBLIC bool RowFiltered(SCROW nRow, SCTAB nTab, SCROW* pFirstRow = nullptr, SCROW* pLastRow = nullptr) const; bool HasFilteredRows(SCROW nStartRow, SCROW nEndRow, SCTAB nTab) const; bool ColFiltered(SCCOL nCol, SCTAB nTab) const; SC_DLLPUBLIC void SetRowFiltered(SCROW nStartRow, SCROW nEndRow, SCTAB nTab, bool bFiltered); diff --git a/sc/qa/unit/uicalc/data/tdf99913.xlsx b/sc/qa/unit/uicalc/data/tdf99913.xlsx new file mode 100644 index 000000000000..3fd18a12984a Binary files /dev/null and b/sc/qa/unit/uicalc/data/tdf99913.xlsx differ diff --git a/sc/qa/unit/uicalc/uicalc.cxx b/sc/qa/unit/uicalc/uicalc.cxx index d19103d0d261..6b774cfe988f 100644 --- a/sc/qa/unit/uicalc/uicalc.cxx +++ b/sc/qa/unit/uicalc/uicalc.cxx @@ -256,6 +256,15 @@ CPPUNIT_TEST_FIXTURE(ScUiCalcTest, testTdf123202) CPPUNIT_ASSERT(pDoc->RowHidden(2, 0)); } +CPPUNIT_TEST_FIXTURE(ScUiCalcTest, testTdf99913) +{ + ScModelObj* pModelObj = createDoc("tdf99913.xlsx"); + ScDocument* pDoc = pModelObj->GetDocument(); + CPPUNIT_ASSERT(pDoc); + + CPPUNIT_ASSERT(pDoc->RowFiltered(2, 0)); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/filter/oox/worksheethelper.cxx b/sc/source/filter/oox/worksheethelper.cxx index 5fc46ecbfd30..29c661537218 100644 --- a/sc/source/filter/oox/worksheethelper.cxx +++ b/sc/source/filter/oox/worksheethelper.cxx @@ -68,6 +68,8 @@ #include #include #include +#include +#include #include #include @@ -340,9 +342,12 @@ private: void convertColumns( OutlineLevelVec& orColLevels, const ValueRange& rColRange, const ColumnModel& rModel ); /** Converts row properties for all rows in the sheet. */ - void convertRows(); + void convertRows(const std::vector& rSpans); /** Converts row properties. */ - void convertRows( OutlineLevelVec& orRowLevels, const ValueRange& rRowRange, const RowModel& rModel, double fDefHeight = -1.0 ); + void convertRows(OutlineLevelVec& orRowLevels, const ValueRange& rRowRange, + const RowModel& rModel, + const std::vector& rSpans, + double fDefHeight = -1.0); /** Converts outline grouping for the passed column or row. */ void convertOutlines( OutlineLevelVec& orLevels, sal_Int32 nColRow, sal_Int32 nLevel, bool bCollapsed, bool bRows ); @@ -946,7 +951,37 @@ void WorksheetGlobals::finalizeWorksheetImport() lclUpdateProgressBar( mxFinalProgress, 0.5 ); convertColumns(); - convertRows(); + + // tdf#99913 rows hidden by filter need extra flag + ScDocument& rDoc = getScDocument(); + std::vector aSpans; + SCTAB nTab = getSheetIndex(); + ScDBData* pDBData = rDoc.GetAnonymousDBData(nTab); + if (pDBData && pDBData->HasAutoFilter()) + { + ScRange aRange; + pDBData->GetArea(aRange); + SCCOLROW nStartRow = static_cast(aRange.aStart.Row()); + SCCOLROW nEndRow = static_cast(aRange.aEnd.Row()); + aSpans.push_back(sc::ColRowSpan(nStartRow, nEndRow)); + } + ScDBCollection* pDocColl = rDoc.GetDBCollection(); + if (!pDocColl->empty()) + { + ScDBCollection::NamedDBs& rDBs = pDocColl->getNamedDBs(); + for (const auto& rxDB : rDBs) + { + if (rxDB->GetTab() == nTab && rxDB->HasAutoFilter()) + { + ScRange aRange; + rxDB->GetArea(aRange); + SCCOLROW nStartRow = static_cast(aRange.aStart.Row()); + SCCOLROW nEndRow = static_cast(aRange.aEnd.Row()); + aSpans.push_back(sc::ColRowSpan(nStartRow, nEndRow)); + } + } + } + convertRows(aSpans); lclUpdateProgressBar( mxFinalProgress, 1.0 ); } @@ -1200,7 +1235,7 @@ void WorksheetGlobals::convertColumns( OutlineLevelVec& orColLevels, convertOutlines( orColLevels, rColRange.mnFirst, rModel.mnLevel, rModel.mbCollapsed, false ); } -void WorksheetGlobals::convertRows() +void WorksheetGlobals::convertRows(const std::vector& rSpans) { sal_Int32 nNextRow = 0; sal_Int32 nMaxRow = mrMaxApiPos.Row(); @@ -1213,21 +1248,23 @@ void WorksheetGlobals::convertRows() ValueRange aRowRange( ::std::max( rowModel.first, nNextRow ), ::std::min( rowModel.second.second, nMaxRow ) ); // process gap between two row models, use default row model if( nNextRow < aRowRange.mnFirst ) - convertRows( aRowLevels, ValueRange( nNextRow, aRowRange.mnFirst - 1 ), maDefRowModel ); + convertRows(aRowLevels, ValueRange(nNextRow, aRowRange.mnFirst - 1), maDefRowModel, + rSpans); // process the row model - convertRows( aRowLevels, aRowRange, rowModel.second.first, maDefRowModel.mfHeight ); + convertRows(aRowLevels, aRowRange, rowModel.second.first, rSpans, maDefRowModel.mfHeight); // cache next row to be processed nNextRow = aRowRange.mnLast + 1; } // remaining default rows to end of sheet - convertRows( aRowLevels, ValueRange( nNextRow, nMaxRow ), maDefRowModel ); + convertRows(aRowLevels, ValueRange(nNextRow, nMaxRow), maDefRowModel, rSpans); // close remaining row outlines spanning to end of sheet convertOutlines( aRowLevels, nMaxRow + 1, 0, false, true ); } -void WorksheetGlobals::convertRows( OutlineLevelVec& orRowLevels, - const ValueRange& rRowRange, const RowModel& rModel, double fDefHeight ) +void WorksheetGlobals::convertRows(OutlineLevelVec& orRowLevels, const ValueRange& rRowRange, + const RowModel& rModel, + const std::vector& rSpans, double fDefHeight) { // row height: convert points to row height in 1/100 mm double fHeight = (rModel.mfHeight >= 0.0) ? rModel.mfHeight : fDefHeight; @@ -1249,6 +1286,16 @@ void WorksheetGlobals::convertRows( OutlineLevelVec& orRowLevels, { ScDocument& rDoc = getScDocument(); rDoc.SetRowHidden( nStartRow, nEndRow, nTab, true ); + for (const auto& rSpan : rSpans) + { + // tdf#99913 rows hidden by filter need extra flag + if (rSpan.mnStart <= nStartRow && nStartRow <= rSpan.mnEnd) + { + SCROW nLast = ::std::min(nEndRow, rSpan.mnEnd); + rDoc.SetRowFiltered(nStartRow, nLast, nTab, true); + break; + } + } } // outline settings for this row range -- cgit