Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load from the file subtotals for PivotFields #601

Merged
merged 6 commits into from Dec 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions ClosedXML/Excel/XLWorkbook_Load.cs
Expand Up @@ -582,6 +582,28 @@ private void LoadSpreadsheetDocument(SpreadsheetDocument dSpreadsheet)

if (pivotField != null)
{
if (pf.AverageSubTotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Average);
if (pf.CountASubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Count);
if (pf.CountSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.CountNumbers);
if (pf.MaxSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Maximum);
if (pf.MinSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Minimum);
if (pf.ApplyStandardDeviationPInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.PopulationStandardDeviation);
if (pf.ApplyVariancePInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.PopulationVariance);
if (pf.ApplyProductInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Product);
if (pf.ApplyStandardDeviationInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.StandardDeviation);
if (pf.SumSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Sum);
if (pf.ApplyVarianceInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Variance);
var items = pf.Items.OfType<Item>().Where(i => i.Index != null && i.Index.HasValue);
if (!items.Any(i => i.HideDetails == null || BooleanValue.ToBoolean(i.HideDetails)))
pivotField.SetCollapsed();
Expand Down Expand Up @@ -620,6 +642,28 @@ private void LoadSpreadsheetDocument(SpreadsheetDocument dSpreadsheet)

if (pivotField != null)
{
if (pf.AverageSubTotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Average);
if (pf.CountASubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Count);
if (pf.CountSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.CountNumbers);
if (pf.MaxSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Maximum);
if (pf.MinSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Minimum);
if (pf.ApplyStandardDeviationPInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.PopulationStandardDeviation);
if (pf.ApplyVariancePInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.PopulationVariance);
if (pf.ApplyProductInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Product);
if (pf.ApplyStandardDeviationInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.StandardDeviation);
if (pf.SumSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Sum);
if (pf.ApplyVarianceInSubtotal != null)
pivotField.AddSubtotal(XLSubtotalFunction.Variance);
var items = pf.Items.OfType<Item>().Where(i => i.Index != null && i.Index.HasValue);
if (!items.Any(i => i.HideDetails == null || BooleanValue.ToBoolean(i.HideDetails)))
pivotField.SetCollapsed();
Expand Down
17 changes: 17 additions & 0 deletions ClosedXML_Tests/Excel/Loading/LoadingTests.cs
Expand Up @@ -136,6 +136,23 @@ public void CanLoadOrderedPivotTable()
}
}

[Test]
public void CanLoadPivotTableSubtotals()
{
using (var stream = TestHelper.GetStreamFromResource(TestHelper.GetResourcePath(@"Misc\LoadPivotTables.xlsx")))
using (var wb = new XLWorkbook(stream))
{
var ws = wb.Worksheet("PivotTableSubtotals");
var pt = ws.PivotTable("PivotTableSubtotals");

var subtotals = pt.RowLabels.Get("Group").Subtotals.ToArray();
Assert.AreEqual(3, subtotals.Length);
Assert.AreEqual(XLSubtotalFunction.Average, subtotals[0]);
Assert.AreEqual(XLSubtotalFunction.Count, subtotals[1]);
Assert.AreEqual(XLSubtotalFunction.Sum, subtotals[2]);
}
}

/// <summary>
/// For non-English locales, the default style ("Normal" in English) can be
/// another piece of text (e.g. ??????? in Russian).
Expand Down
Binary file modified ClosedXML_Tests/Resource/Misc/LoadPivotTables.xlsx
Binary file not shown.