sc/inc/pivot/PivotTableFormatOutput.hxx                                        
                                |    1 
 sc/qa/unit/PivotTableFormatsImportExport.cxx                                   
                                |  168 ++++++++++
 
sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_10_FormatDefinitionNotMatchingPivotTable.xlsx
       |binary
 
sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_11_WholeDataColumnSelected.xlsx
                     |binary
 
sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_12_WholeLabelColumnSelected.xlsx
                    |binary
 
sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_13_SelectionInLabelAndData.xlsx
                     |binary
 
sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_14_TwoRowsDataFieldInColumn_LabelColor.xlsx
         |binary
 
sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_15_TwoDataFieldColumns_WholeDataColumnSelected.xlsx
 |binary
 
sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_2_DataFieldInRow_ColumnLabelColor.xlsx
              |binary
 
sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_8_DataFieldInRow_DataColor.xlsx
                     |binary
 
sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_9_MultipleSelections.xlsx
                           |binary
 sc/source/core/data/PivotTableFormatOutput.cxx                                 
                                |  161 +++++----
 sc/source/filter/oox/PivotTableFormat.cxx                                      
                                |    2 
 13 files changed, 261 insertions(+), 71 deletions(-)

New commits:
commit c0f44787c0bcbd28ac61dab66483f025578f095c
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Wed Apr 17 16:08:45 2024 +0900
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Mon Apr 22 08:22:04 2024 +0200

    pivot: handle whole row/column selections, added many tests
    
    This adds support for selecting whole rows/columns, where the field
    reference is set but no indices are present. This means that all
    the data cells apply (selecting the whole thing), which are not
    subtotals. For this the support for multiple matching and maybe
    matching results was added (previously max 1 would apply), which
    would apply the pattern to multiple cells in the pivot table.
    
    In addition handle labels of columns with multiple data fields
    correctly.
    
    This also adds many more test scenarios, which all cover the
    changes mentioned above.
    
    Change-Id: Ibcdaa933c69991eaa55a2ad18b9f9a838308d478
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166384
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git a/sc/inc/pivot/PivotTableFormatOutput.hxx 
b/sc/inc/pivot/PivotTableFormatOutput.hxx
index 0aeb492cc34a..3bbd19b28f36 100644
--- a/sc/inc/pivot/PivotTableFormatOutput.hxx
+++ b/sc/inc/pivot/PivotTableFormatOutput.hxx
@@ -41,6 +41,7 @@ struct FormatOutputField
     tools::Long nDimension = -2;
     OUString aName;
     sal_Int32 nIndex = -1;
+    bool bMatchesAll = false;
     bool bSet = false;
 };
 
diff --git a/sc/qa/unit/PivotTableFormatsImportExport.cxx 
b/sc/qa/unit/PivotTableFormatsImportExport.cxx
index f1cf83d225cd..0ea8888867a1 100644
--- a/sc/qa/unit/PivotTableFormatsImportExport.cxx
+++ b/sc/qa/unit/PivotTableFormatsImportExport.cxx
@@ -335,6 +335,174 @@ CPPUNIT_TEST_FIXTURE(ScPivotTableFormatsImportExport,
     CPPUNIT_ASSERT_EQUAL(u"60"_ustr, rDoc.GetString(aAddress));
 }
 
+CPPUNIT_TEST_FIXTURE(ScPivotTableFormatsImportExport,
+                     PivotTableCellFormatsTest_11_WholeDataColumnSelected)
+{
+    auto assertDocument = [](ScDocument& rDoc) {
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"G2"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"G3"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"G4"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"G5"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"G6"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"G7"_ustr));
+
+        // Make sure the other cells have the font color or background set to 
default
+        auto aEmptyAddresses = std::to_array<OUString>({
+            u"F1"_ustr,
+            u"G1"_ustr,
+            u"F2"_ustr,
+            u"F3"_ustr,
+            u"F4"_ustr,
+            u"F5"_ustr,
+            u"F6"_ustr,
+            u"F7"_ustr,
+            u"F8"_ustr,
+            u"G8"_ustr,
+        });
+        CPPUNIT_ASSERT_EQUAL(OUString(), checkNonEmptyAddresses(rDoc, 
aEmptyAddresses));
+    };
+
+    
createScDoc("xlsx/pivot-table/PivotTableCellFormatsTest_11_WholeDataColumnSelected.xlsx");
+    assertDocument(*getScDoc());
+    saveAndReload("Calc Office Open XML");
+    assertDocument(*getScDoc());
+}
+
+CPPUNIT_TEST_FIXTURE(ScPivotTableFormatsImportExport,
+                     PivotTableCellFormatsTest_12_WholeLabelColumnSelected)
+{
+    auto assertDocument = [](ScDocument& rDoc) {
+        CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, getBackgroundColor(rDoc, 
u"F2"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, getBackgroundColor(rDoc, 
u"F3"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, getBackgroundColor(rDoc, 
u"F4"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, getBackgroundColor(rDoc, 
u"F5"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, getBackgroundColor(rDoc, 
u"F6"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, getBackgroundColor(rDoc, 
u"F7"_ustr));
+
+        // Make sure the other cells have the font color or background set to 
default
+        auto aEmptyAddresses = std::to_array<OUString>({
+            u"F1"_ustr,
+            u"G1"_ustr,
+            u"G2"_ustr,
+            u"G3"_ustr,
+            u"G4"_ustr,
+            u"G5"_ustr,
+            u"G6"_ustr,
+            u"G7"_ustr,
+            u"F8"_ustr,
+            u"G8"_ustr,
+        });
+        CPPUNIT_ASSERT_EQUAL(OUString(), checkNonEmptyAddresses(rDoc, 
aEmptyAddresses));
+    };
+
+    
createScDoc("xlsx/pivot-table/PivotTableCellFormatsTest_12_WholeLabelColumnSelected.xlsx");
+    assertDocument(*getScDoc());
+    saveAndReload("Calc Office Open XML");
+    assertDocument(*getScDoc());
+}
+
+CPPUNIT_TEST_FIXTURE(ScPivotTableFormatsImportExport,
+                     PivotTableCellFormatsTest_13_SelectionInLabelAndData)
+{
+    auto assertDocument = [](ScDocument& rDoc) {
+        CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, getBackgroundColor(rDoc, 
u"F5"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, getBackgroundColor(rDoc, 
u"G5"_ustr));
+
+        // Make sure the other cells have the font color or background set to 
default
+        auto aEmptyAddresses = std::to_array<OUString>({
+            u"F1"_ustr,
+            u"G1"_ustr,
+            u"F2"_ustr,
+            u"G2"_ustr,
+            u"F3"_ustr,
+            u"G3"_ustr,
+            u"F4"_ustr,
+            u"G4"_ustr,
+            u"F6"_ustr,
+            u"G6"_ustr,
+            u"F7"_ustr,
+            u"G7"_ustr,
+            u"F8"_ustr,
+            u"G8"_ustr,
+        });
+        CPPUNIT_ASSERT_EQUAL(OUString(), checkNonEmptyAddresses(rDoc, 
aEmptyAddresses));
+    };
+
+    
createScDoc("xlsx/pivot-table/PivotTableCellFormatsTest_13_SelectionInLabelAndData.xlsx");
+    assertDocument(*getScDoc());
+    saveAndReload("Calc Office Open XML");
+    assertDocument(*getScDoc());
+}
+
+CPPUNIT_TEST_FIXTURE(ScPivotTableFormatsImportExport,
+                     
PivotTableCellFormatsTest_14_TwoRowsDataFieldInColumn_LabelColor)
+{
+    auto assertDocument = [](ScDocument& rDoc) {
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"I5"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED, getBackgroundColor(rDoc, 
u"J5"_ustr));
+
+        // Make sure the other cells have the font color or background set to 
default
+        auto aEmptyAddresses = std::to_array<OUString>({
+            u"G5"_ustr,  u"H5"_ustr,  u"G6"_ustr,  u"H6"_ustr,  u"i6"_ustr,  
u"j6"_ustr,
+            u"G7"_ustr,  u"H7"_ustr,  u"i7"_ustr,  u"j7"_ustr,  u"G8"_ustr,  
u"H8"_ustr,
+            u"i8"_ustr,  u"j8"_ustr,  u"G9"_ustr,  u"H9"_ustr,  u"i9"_ustr,  
u"j9"_ustr,
+            u"G10"_ustr, u"H10"_ustr, u"i10"_ustr, u"j10"_ustr, u"G11"_ustr, 
u"H11"_ustr,
+            u"i11"_ustr, u"j11"_ustr, u"G12"_ustr, u"H12"_ustr, u"i12"_ustr, 
u"j12"_ustr,
+            u"G13"_ustr, u"H13"_ustr, u"i13"_ustr, u"j13"_ustr, u"G14"_ustr, 
u"H14"_ustr,
+            u"i14"_ustr, u"j14"_ustr,
+        });
+        CPPUNIT_ASSERT_EQUAL(OUString(), checkNonEmptyAddresses(rDoc, 
aEmptyAddresses));
+    };
+
+    createScDoc(
+        
"xlsx/pivot-table/PivotTableCellFormatsTest_14_TwoRowsDataFieldInColumn_LabelColor.xlsx");
+    assertDocument(*getScDoc());
+    saveAndReload("Calc Office Open XML");
+    assertDocument(*getScDoc());
+}
+
+CPPUNIT_TEST_FIXTURE(ScPivotTableFormatsImportExport,
+                     
PivotTableCellFormatsTest_15_TwoDataFieldColumns_WholeDataColumnSelected)
+{
+    auto assertDocument = [](ScDocument& rDoc) {
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"H3"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"H4"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"H5"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"H6"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"H7"_ustr));
+        CPPUNIT_ASSERT_EQUAL(COL_YELLOW, getBackgroundColor(rDoc, u"H8"_ustr));
+
+        // Make sure the other cells have the font color or background set to 
default
+        auto aEmptyAddresses = std::to_array<OUString>({
+            u"F1"_ustr,
+            u"G1"_ustr,
+            u"H1"_ustr,
+            u"F2"_ustr,
+            u"G2"_ustr,
+            u"H2"_ustr,
+            u"F3"_ustr,
+            u"G3"_ustr,
+            u"F4"_ustr,
+            u"G4"_ustr,
+            u"F5"_ustr,
+            u"G5"_ustr,
+            u"F6"_ustr,
+            u"G6"_ustr,
+            u"F7"_ustr,
+            u"G7"_ustr,
+            u"F8"_ustr,
+            u"G8"_ustr,
+        });
+        CPPUNIT_ASSERT_EQUAL(OUString(), checkNonEmptyAddresses(rDoc, 
aEmptyAddresses));
+    };
+
+    createScDoc("xlsx/pivot-table/"
+                
"PivotTableCellFormatsTest_15_TwoDataFieldColumns_WholeDataColumnSelected.xlsx");
+    assertDocument(*getScDoc());
+    saveAndReload("Calc Office Open XML");
+    assertDocument(*getScDoc());
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_11_WholeDataColumnSelected.xlsx
 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_11_WholeDataColumnSelected.xlsx
new file mode 100644
index 000000000000..8ba00ddf5788
Binary files /dev/null and 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_11_WholeDataColumnSelected.xlsx
 differ
diff --git 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_12_WholeLabelColumnSelected.xlsx
 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_12_WholeLabelColumnSelected.xlsx
new file mode 100644
index 000000000000..63442d84060f
Binary files /dev/null and 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_12_WholeLabelColumnSelected.xlsx
 differ
diff --git 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_13_SelectionInLabelAndData.xlsx
 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_13_SelectionInLabelAndData.xlsx
new file mode 100644
index 000000000000..053af3f3687b
Binary files /dev/null and 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_13_SelectionInLabelAndData.xlsx
 differ
diff --git 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_14_TwoRowsDataFieldInColumn_LabelColor.xlsx
 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_14_TwoRowsDataFieldInColumn_LabelColor.xlsx
new file mode 100644
index 000000000000..43df4ab75130
Binary files /dev/null and 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_14_TwoRowsDataFieldInColumn_LabelColor.xlsx
 differ
diff --git 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_15_TwoDataFieldColumns_WholeDataColumnSelected.xlsx
 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_15_TwoDataFieldColumns_WholeDataColumnSelected.xlsx
new file mode 100644
index 000000000000..7b74a09c4f04
Binary files /dev/null and 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_15_TwoDataFieldColumns_WholeDataColumnSelected.xlsx
 differ
diff --git a/sc/source/core/data/PivotTableFormatOutput.cxx 
b/sc/source/core/data/PivotTableFormatOutput.cxx
index 83bf865573a4..c9073d6be07b 100644
--- a/sc/source/core/data/PivotTableFormatOutput.cxx
+++ b/sc/source/core/data/PivotTableFormatOutput.cxx
@@ -83,6 +83,39 @@ void initLines(std::vector<LineData>& rLines, 
std::vector<ScDPOutLevelData> cons
     }
 }
 
+Selection const* findSelection(PivotTableFormat const& rFormat, tools::Long 
nDimension)
+{
+    for (Selection const& rSelection : rFormat.getSelections())
+    {
+        if (rSelection.nField == nDimension)
+            return &rSelection;
+    }
+    return nullptr;
+}
+
+void fillOutputFieldFromSelection(FormatOutputField& rOutputField, Selection 
const& rSelection,
+                                  size_t nSelectionIndex, NameResolver& 
rNameResolver)
+{
+    if (rSelection.nIndices.empty())
+    {
+        rOutputField.bMatchesAll = true;
+    }
+    else
+    {
+        if (rSelection.nIndices.size() > 1 && rSelection.nIndices.size() > 
nSelectionIndex)
+            rOutputField.nIndex = rSelection.nIndices[nSelectionIndex];
+        else
+            rOutputField.nIndex = rSelection.nIndices[0];
+
+        if (rOutputField.nDimension == -2)
+            rOutputField.aName = "DATA";
+        else
+            rOutputField.aName
+                = rNameResolver.getNameForIndex(rOutputField.nIndex, 
rOutputField.nDimension);
+    }
+    rOutputField.bSet = true;
+}
+
 void initFormatOutputField(size_t nSelectionIndex, 
std::vector<FormatOutputField>& rOutputFields,
                            std::vector<ScDPOutLevelData> const& rFields,
                            PivotTableFormat const& rFormat, NameResolver& 
rNameResolver)
@@ -94,24 +127,11 @@ void initFormatOutputField(size_t nSelectionIndex, 
std::vector<FormatOutputField
         if (!rFields[i].mbDataLayout)
             rOutputField.nDimension = rFields[i].mnDim;
 
-        for (auto const& rSelection : rFormat.aSelections)
-        {
-            if (!rSelection.nIndices.empty() && rSelection.nField == 
rOutputField.nDimension)
-            {
-                if (rSelection.nIndices.size() > nSelectionIndex)
-                    rOutputField.nIndex = rSelection.nIndices[nSelectionIndex];
-                else
-                    rOutputField.nIndex = rSelection.nIndices[0];
-
-                if (rOutputField.nDimension == -2)
-                    rOutputField.aName = "DATA";
-                else
-                    rOutputField.aName = 
rNameResolver.getNameForIndex(rOutputField.nIndex,
-                                                                       
rOutputField.nDimension);
+        Selection const* pSelection = findSelection(rFormat, 
rOutputField.nDimension);
+        if (pSelection == nullptr)
+            continue;
 
-                rOutputField.bSet = true;
-            }
-        }
+        fillOutputFieldFromSelection(rOutputField, *pSelection, 
nSelectionIndex, rNameResolver);
     }
 }
 
@@ -159,9 +179,6 @@ void FormatOutput::prepare(SCTAB nTab, 
std::vector<ScDPOutLevelData> const& rCol
                 nMaxNumberOfIndices = rSelection.nIndices.size();
         }
 
-        if (nMaxNumberOfIndices == 0)
-            continue;
-
         for (size_t nSelectionIndex = 0; nSelectionIndex < 
nMaxNumberOfIndices; nSelectionIndex++)
         {
             sc::FormatOutputEntry aEntry;
@@ -178,19 +195,11 @@ void FormatOutput::prepare(SCTAB nTab, 
std::vector<ScDPOutLevelData> const& rCol
                 // Initialize column output fields to have 1 data output field
                 aEntry.aColumnOutputFields.resize(1);
                 FormatOutputField& rOutputField = 
aEntry.aColumnOutputFields[0];
-
-                for (auto const& rSelection : rFormat.aSelections)
-                {
-                    if (rSelection.nField == -2)
-                    {
-                        if (rSelection.nIndices.size() > 1)
-                            rOutputField.nIndex = 
rSelection.nIndices[nSelectionIndex];
-                        else
-                            rOutputField.nIndex = rSelection.nIndices[0];
-                        rOutputField.aName = "DATA";
-                        rOutputField.bSet = true;
-                    }
-                }
+                rOutputField.nDimension = -2;
+                Selection const* pSelection = findSelection(rFormat, -2);
+                if (pSelection)
+                    fillOutputFieldFromSelection(rOutputField, *pSelection, 
nSelectionIndex,
+                                                 aNameResolver);
             }
             else
             {
@@ -273,6 +282,7 @@ namespace
 {
 void checkForMatchingLines(std::vector<LineData> const& rLines,
                            std::vector<FormatOutputField> const& 
rFormatOutputField,
+                           FormatType eType,
                            std::vector<std::reference_wrapper<const 
LineData>>& rMatches,
                            std::vector<std::reference_wrapper<const 
LineData>>& rMaybeMatches)
 {
@@ -294,12 +304,15 @@ void checkForMatchingLines(std::vector<LineData> const& 
rLines,
             {
                 if (rFormatEntry.bSet)
                 {
-                    if (nDimension == -2 && rFieldData.nIndex == 
rFormatEntry.nIndex)
+                    if (rFormatEntry.bMatchesAll && !rFieldData.bSubtotal)
+                        bFieldMatch = true;
+                    else if (nDimension == -2 && rFieldData.nIndex == 
rFormatEntry.nIndex)
                         bFieldMatch = true;
                     else if (nDimension != -2 && rFieldData.aName == 
rFormatEntry.aName)
                         bFieldMatch = true;
                 }
-                else if (!rFormatEntry.bSet && !rFieldData.bIsMember && 
!rFieldData.bContinue)
+                else if (!rFormatEntry.bSet && eType == FormatType::Data && 
!rFieldData.bIsMember
+                         && !rFieldData.bContinue)
                 {
                     bFieldMatch = true;
                 }
@@ -322,9 +335,8 @@ void checkForMatchingLines(std::vector<LineData> const& 
rLines,
         if (nMatch == nNoOfFields)
         {
             rMatches.push_back(std::cref(rLineData));
-            break;
         }
-        else if (nMatch + nMaybeMatch == nNoOfFields && nMatch != 0)
+        else if (nMatch + nMaybeMatch == nNoOfFields)
         {
             rMaybeMatches.push_back(std::cref(rLineData));
         }
@@ -335,42 +347,46 @@ void checkForMatchingLines(std::vector<LineData> const& 
rLines,
 void evaluateMatches(ScDocument& rDocument,
                      std::vector<std::reference_wrapper<const LineData>> 
const& rMatches,
                      std::vector<std::reference_wrapper<const LineData>> 
const& rMaybeMatches,
-                     std::optional<SCCOLROW>& oRow, std::optional<SCCOLROW>& 
oColumn,
+                     std::vector<SCCOLROW>& aRows, std::vector<SCCOLROW>& 
aColumns,
                      FormatOutputEntry const& rOutputEntry, 
FormatResultDirection eResultDirection)
 {
     // We expect that tab and pattern to be set or this method shouldn't be 
called at all
     assert(rOutputEntry.onTab);
     assert(rOutputEntry.pPattern);
 
-    // We can output only if there is exactly 1 match or 1 maybe match
-    if (rMatches.size() != 1 && rMaybeMatches.size() != 1)
+    if (rMatches.empty() && rMaybeMatches.empty())
         return;
 
-    LineData const& rLineData = !rMatches.empty() ? rMatches.back() : 
rMaybeMatches.back();
+    bool bMaybeExists = rMatches.empty();
 
-    // Can't continue if we don't have complete row/column data
-    if (!rLineData.oLine || !rLineData.oPosition)
-        return;
+    auto const& rLineDataVector = bMaybeExists ? rMaybeMatches : rMatches;
 
-    if (rOutputEntry.eType == FormatType::Label)
+    for (LineData const& rLineData : rLineDataVector)
     {
-        // Primary axis is set to column (line) then row (position)
-        SCCOLROW nColumn = *rLineData.oLine;
-        SCCOLROW nRow = *rLineData.oPosition;
+        // Can't continue if we don't have complete row/column data
+        if (!rLineData.oLine || !rLineData.oPosition)
+            continue;
 
-        // In row orientation, the primary axis is row, then column, so we 
need to swap
-        if (eResultDirection == FormatResultDirection::ROW)
-            std::swap(nRow, nColumn);
+        if (rOutputEntry.eType == FormatType::Label && !bMaybeExists)
+        {
+            // Primary axis is set to column (line) then row (position)
+            SCCOLROW nColumn = *rLineData.oLine;
+            SCCOLROW nRow = *rLineData.oPosition;
 
-        // Set the pattern to the sheet
-        rDocument.ApplyPattern(nColumn, nRow, *rOutputEntry.onTab, 
*rOutputEntry.pPattern);
-    }
-    else if (rOutputEntry.eType == FormatType::Data)
-    {
-        if (eResultDirection == FormatResultDirection::ROW)
-            oRow = rLineData.oLine;
-        else if (eResultDirection == FormatResultDirection::COLUMN)
-            oColumn = rLineData.oLine;
+            // In row orientation, the primary axis is row, then column, so we 
need to swap
+            if (eResultDirection == FormatResultDirection::ROW)
+                std::swap(nRow, nColumn);
+
+            // Set the pattern to the sheet
+            rDocument.ApplyPattern(nColumn, nRow, *rOutputEntry.onTab, 
*rOutputEntry.pPattern);
+        }
+        else if (rOutputEntry.eType == FormatType::Data)
+        {
+            if (eResultDirection == FormatResultDirection::ROW)
+                aRows.push_back(*rLineData.oLine);
+            else if (eResultDirection == FormatResultDirection::COLUMN)
+                aColumns.push_back(*rLineData.oLine);
+        }
     }
 }
 
@@ -386,15 +402,16 @@ void FormatOutput::apply(ScDocument& rDocument)
         if (!rOutputEntry.onTab || !rOutputEntry.pPattern)
             continue;
 
-        std::optional<SCCOLROW> oRow;
-        std::optional<SCCOLROW> oColumn;
+        std::vector<SCCOLROW> aRows;
+        std::vector<SCCOLROW> aColumns;
         {
             std::vector<std::reference_wrapper<const LineData>> rMatches;
             std::vector<std::reference_wrapper<const LineData>> rMaybeMatches;
 
-            checkForMatchingLines(maRowLines, rOutputEntry.aRowOutputFields, 
rMatches,
-                                  rMaybeMatches);
-            evaluateMatches(rDocument, rMatches, rMaybeMatches, oRow, oColumn, 
rOutputEntry,
+            checkForMatchingLines(maRowLines, rOutputEntry.aRowOutputFields, 
rOutputEntry.eType,
+                                  rMatches, rMaybeMatches);
+
+            evaluateMatches(rDocument, rMatches, rMaybeMatches, aRows, 
aColumns, rOutputEntry,
                             FormatResultDirection::ROW);
         }
 
@@ -402,15 +419,19 @@ void FormatOutput::apply(ScDocument& rDocument)
             std::vector<std::reference_wrapper<const LineData>> rMatches;
             std::vector<std::reference_wrapper<const LineData>> rMaybeMatches;
 
-            checkForMatchingLines(maColumnLines, 
rOutputEntry.aColumnOutputFields, rMatches,
-                                  rMaybeMatches);
-            evaluateMatches(rDocument, rMatches, rMaybeMatches, oRow, oColumn, 
rOutputEntry,
+            checkForMatchingLines(maColumnLines, 
rOutputEntry.aColumnOutputFields,
+                                  rOutputEntry.eType, rMatches, rMaybeMatches);
+
+            evaluateMatches(rDocument, rMatches, rMaybeMatches, aRows, 
aColumns, rOutputEntry,
                             FormatResultDirection::COLUMN);
         }
 
-        if (oColumn && oRow && rOutputEntry.eType == FormatType::Data)
+        if (!aColumns.empty() && !aRows.empty() && rOutputEntry.eType == 
FormatType::Data)
         {
-            rDocument.ApplyPattern(*oColumn, *oRow, *rOutputEntry.onTab, 
*rOutputEntry.pPattern);
+            for (SCCOLROW nRow : aRows)
+                for (SCCOLROW nColumn : aColumns)
+                    rDocument.ApplyPattern(nColumn, nRow, *rOutputEntry.onTab,
+                                           *rOutputEntry.pPattern);
         }
     }
 }
diff --git a/sc/source/filter/oox/PivotTableFormat.cxx 
b/sc/source/filter/oox/PivotTableFormat.cxx
index 59626f89a527..7d17be0ba0ec 100644
--- a/sc/source/filter/oox/PivotTableFormat.cxx
+++ b/sc/source/filter/oox/PivotTableFormat.cxx
@@ -105,7 +105,7 @@ void PivotTableFormat::finalizeImport()
     aFormat.pPattern = pPattern;
     for (auto& rReference : maReferences)
     {
-        if (rReference->mnField && !rReference->maFieldItemsIndices.empty())
+        if (rReference->mnField)
         {
             aFormat.aSelections.push_back(
                 sc::Selection{ .bSelected = rReference->mbSelected,
commit 37b1799fa163455c5120429ae1537ba5c6ec8d23
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Wed Apr 17 16:06:04 2024 +0900
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Mon Apr 22 08:21:55 2024 +0200

    pivot: add reference image to the test files
    
    Change-Id: I2532bae611bc815b270187dc6b9a07b47837962d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166383
    Tested-by: Tomaž Vajngerl <qui...@gmail.com>
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_10_FormatDefinitionNotMatchingPivotTable.xlsx
 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_10_FormatDefinitionNotMatchingPivotTable.xlsx
index c2fe60ccf69f..7413f28e33fe 100644
Binary files 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_10_FormatDefinitionNotMatchingPivotTable.xlsx
 and 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_10_FormatDefinitionNotMatchingPivotTable.xlsx
 differ
diff --git 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_2_DataFieldInRow_ColumnLabelColor.xlsx
 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_2_DataFieldInRow_ColumnLabelColor.xlsx
index 1c8245751bfc..ea1482bd617e 100644
Binary files 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_2_DataFieldInRow_ColumnLabelColor.xlsx
 and 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_2_DataFieldInRow_ColumnLabelColor.xlsx
 differ
diff --git 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_8_DataFieldInRow_DataColor.xlsx
 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_8_DataFieldInRow_DataColor.xlsx
index 48964032868b..7663554c501d 100644
Binary files 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_8_DataFieldInRow_DataColor.xlsx
 and 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_8_DataFieldInRow_DataColor.xlsx
 differ
diff --git 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_9_MultipleSelections.xlsx
 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_9_MultipleSelections.xlsx
index 6d317d2e45c0..6ef6cbaa4274 100644
Binary files 
a/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_9_MultipleSelections.xlsx
 and 
b/sc/qa/unit/data/xlsx/pivot-table/PivotTableCellFormatsTest_9_MultipleSelections.xlsx
 differ

Reply via email to