EPPlusSoftware / EPPlus

EPPlus-Excel spreadsheets for .NET
https://epplussoftware.com
Other
1.79k stars 273 forks source link

ShowDetails(false) does not work anymore since version 7.2.0 #1603

Open yangmijun opened 6 days ago

yangmijun commented 6 days ago

EPPlus usage

Noncommercial use

Environment

Windows

Epplus version

7.2.0 - 7.3.2

Spreadsheet application

Excel

Description

I created a pivot table with 2 row fields, and set "field.Items.ShowDetails(false)" to each of them. When I use v7.1.3, the pivot table is OK(the row fields are all folded), but after I update version to 7.2.0(or any other upper version to 7.3.2), "ShowDetails(false)" do not work any more, the row fields are still expended

JanKallman commented 5 days ago

Can you provide some code and a workbook if needed to reproduce this issue?

yangmijun commented 5 days ago

`using OfficeOpenXml;

namespace ConsoleAppEpplus { internal class Program { static void Main(string[] args) { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (var package = new ExcelPackage()) { var dataSheet = package.Workbook.Worksheets.Add("Data"); var pivotSheet = package.Workbook.Worksheets.Add("Pivot");

            //put data in the data sheet
            dataSheet.Cells["A1"].Value = "Name";
            dataSheet.Cells["B1"].Value = "Age";
            dataSheet.Cells["C1"].Value = "Gender";

            dataSheet.Cells["A2"].Value = "John";
            dataSheet.Cells["B2"].Value = 25;
            dataSheet.Cells["C2"].Value = "Male";
            dataSheet.Cells["A3"].Value = "Jane";
            dataSheet.Cells["B3"].Value = 30;
            dataSheet.Cells["C3"].Value = "Female";
            dataSheet.Cells["A4"].Value = "Bob";
            dataSheet.Cells["B4"].Value = 40;
            dataSheet.Cells["C4"].Value = "Male";
            dataSheet.Cells["A5"].Value = "Mary";
            dataSheet.Cells["B5"].Value = 28;
            dataSheet.Cells["C5"].Value = "Female";
            dataSheet.Cells["A6"].Value = "John";
            dataSheet.Cells["B6"].Value = 68;
            dataSheet.Cells["C6"].Value = "Male";

            //create pivot table
            var pivotDataRange = dataSheet.Cells[1, 1, 6, 3];
            var pivotTable = pivotSheet.PivotTables.Add(pivotSheet.Cells["C3"], pivotDataRange, "TestPivotTable");

            var field1 = pivotTable.Fields["Name"];
            var f1 = pivotTable.RowFields.Add(field1);
            f1.Items.ShowDetails(false);

            var field2 = pivotTable.Fields["Age"];
            var f2 = pivotTable.RowFields.Add(field2);
            f2.Items.ShowDetails(false);

            //save the file
            package.SaveAs(new FileInfo("HelloWorld.xlsx"));
        }
    }
}

} `

yangmijun commented 5 days ago

This is a simple sample code, run it in a Console Program(.net 6.0), first install v7.1.3, see pivot in the output "HelloWorld.xlsx",then update Epplus to 7.3.2, run it again, you will find pivot in "HelloWorld.xlsx" is different.

JanKallman commented 5 days ago

Thanks, I will have a look and get back to you.

JanKallman commented 5 days ago

The problem is that EPPlus no longer refreshes the items before setting the showing/hiding the items as the Items collection now contains the default item. I will add a fix for the next version. For now you can refresh the items before calling the ShowDetails method. In the supplied sample:

                var field1 = pivotTable.Fields["Name"];
                var f1 = pivotTable.RowFields.Add(field1);
                f1.Items.Refresh(); //Refresh the items manually
                f1.Items.ShowDetails(false);

                var field2 = pivotTable.Fields["Age"];
                var f2 = pivotTable.RowFields.Add(field2);
                f1.Items.Refresh();  //Refresh the items manually
                f2.Items.ShowDetails(false);
yangmijun commented 5 days ago

Thank you for your help. @JanKallman