-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review and rework pivot cache API, refactor and document pivot caches.
- Loading branch information
Showing
54 changed files
with
1,211 additions
and
708 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using ClosedXML.Excel; | ||
using NUnit.Framework; | ||
|
||
namespace ClosedXML.Tests.Excel.PivotTables | ||
{ | ||
[TestFixture] | ||
public class XLPivotCacheTests | ||
{ | ||
[Test] | ||
public void FieldNames_KeepNamesEvenWhenSourceChange() | ||
{ | ||
using var wb = new XLWorkbook(); | ||
var ws = wb.AddWorksheet(); | ||
var range = ws.FirstCell().InsertData(new[] { "Name", "Pie" }); | ||
|
||
var pivotCache = wb.PivotCaches.Add(range); | ||
ws.Cell("A1").Value = "Pastry"; | ||
|
||
Assert.AreEqual(new[] { "Name" }, pivotCache.FieldNames); | ||
} | ||
|
||
[Test] | ||
public void Refresh_UpdatesFieldNames() | ||
{ | ||
using var wb = new XLWorkbook(); | ||
var ws = wb.AddWorksheet(); | ||
var range = ws.FirstCell().InsertData(new[] { "Name", "Pie" }); | ||
|
||
var pivotCache = wb.PivotCaches.Add(range); | ||
ws.Cell("A1").Value = "Pastry"; | ||
pivotCache.Refresh(); | ||
|
||
Assert.AreEqual(new[] { "Pastry" }, pivotCache.FieldNames); | ||
} | ||
|
||
[Test] | ||
public void Refresh_RetainsSetOptions() | ||
{ | ||
using var wb = new XLWorkbook(); | ||
var ws = wb.AddWorksheet(); | ||
var range = ws.FirstCell().InsertData(new[] { "Name", "Pie" }); | ||
|
||
var pivotCache = wb.PivotCaches.Add(range); | ||
|
||
pivotCache.ItemsToRetainPerField = XLItemsToRetain.None; | ||
pivotCache.SaveSourceData = false; | ||
pivotCache.RefreshDataOnOpen = true; | ||
|
||
pivotCache.Refresh(); | ||
|
||
Assert.AreEqual(XLItemsToRetain.None, pivotCache.ItemsToRetainPerField); | ||
Assert.AreEqual(false, pivotCache.SaveSourceData); | ||
Assert.AreEqual(true, pivotCache.RefreshDataOnOpen); | ||
} | ||
|
||
[Test] | ||
public void Refresh_RenamedFieldIsRemovedFromPivotTable() | ||
{ | ||
// Pivot table has only field for Pastry, the dough is no longer in the pivot table after refresh | ||
TestHelper.CreateAndCompare(wb => | ||
{ | ||
var ws = wb.AddWorksheet(); | ||
var range = ws.FirstCell().InsertData(new object[] | ||
{ | ||
("Pastry", "Dough"), | ||
("Waffles", "Puff") | ||
}); | ||
var table = range.CreateTable(); | ||
var pivotTable = ws.PivotTables.Add("pvt", ws.Cell("D1"), table); | ||
pivotTable.RowLabels.Add("Pastry"); | ||
pivotTable.RowLabels.Add("Dough"); | ||
pivotTable.Values.Add("Pastry").SetSummaryFormula(XLPivotSummary.Count); | ||
ws.Cell("B1").Value = "Mixture"; | ||
pivotTable.PivotCache.Refresh(); | ||
}, @"Other\PivotTableReferenceFiles\RenamedFieldIsRemovedFromPivotTable-output.xlsx"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-10.4 KB
(76%)
ClosedXML.Tests/Resource/Examples/PivotTables/PivotTables.xlsx
Binary file not shown.
Binary file modified
BIN
-5.03 KB
(80%)
...ts/Resource/Other/PivotTableReferenceFiles/BlankPivotTableField/BlankPivotTableField.xlsx
Binary file not shown.
Binary file modified
BIN
+33 Bytes
(100%)
ClosedXML.Tests/Resource/Other/PivotTableReferenceFiles/LongText/outputfile.xlsx
Binary file not shown.
Binary file modified
BIN
+3.18 KB
(120%)
ClosedXML.Tests/Resource/Other/PivotTableReferenceFiles/PivotSubtotalsSource/input.xlsx
Binary file not shown.
Binary file added
BIN
+15 KB
ClosedXML.Tests/Resource/Other/PivotTableReferenceFiles/PivotSubtotalsSource/output.xlsx
Binary file not shown.
Binary file modified
BIN
+197 Bytes
(100%)
...XML.Tests/Resource/Other/PivotTableReferenceFiles/PivotTableWithNoneTheme/outputfile.xlsx
Binary file not shown.
Binary file added
BIN
+9.03 KB
...s/Resource/Other/PivotTableReferenceFiles/RenamedFieldIsRemovedFromPivotTable-output.xlsx
Binary file not shown.
Binary file modified
BIN
+204 Bytes
(100%)
...L.Tests/Resource/Other/PivotTableReferenceFiles/SourceSheetWithWhitespace/outputfile.xlsx
Binary file not shown.
Binary file modified
BIN
+1.46 KB
(110%)
....Tests/Resource/Other/PivotTableReferenceFiles/TwoPivotTablesWithSingleSource/output.xlsx
Binary file not shown.
Binary file modified
BIN
-1 Byte
(100%)
....Tests/Resource/Other/PivotTableReferenceFiles/VariousDataTypesInTableColumns/output.xlsx
Binary file not shown.
Binary file modified
BIN
-2.6 KB
(89%)
ClosedXML.Tests/Resource/Other/PivotTableReferenceFiles/VersioningAttributes/outputfile.xlsx
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace ClosedXML.Excel | ||
{ | ||
/// <summary> | ||
/// A cache of pivot data - essentially a collection of fields and their values that can be | ||
/// displayed by a <see cref="IXLPivotTable"/>. Data for the cache are retrieved from | ||
/// an area (a table or a range). The pivot cache data are <strong>cached</strong>, i.e. | ||
/// the data in the source are not immediately updated once the data in a worksheet change. | ||
/// </summary> | ||
public interface IXLPivotCache | ||
{ | ||
/// <summary> | ||
/// Get names of all fields in the source, in left to right order. Every field name is unique. | ||
/// </summary> | ||
/// <remarks> | ||
/// The field names are case insensitive. The field names of the cached | ||
/// source might differ from actual names of the columns | ||
/// in the data cells. | ||
/// </remarks> | ||
IReadOnlyList<String> FieldNames { get; } | ||
|
||
/// <summary> | ||
/// Gets the number of unused items in shared items to allow before discarding unused items. | ||
/// </summary> | ||
/// <remarks> | ||
/// Shared items are distinct values of a source field values. Updating them can be expensive | ||
/// and this controls, when should the cache be updated. Application-dependent attribute. | ||
/// </remarks> | ||
/// <value>Default value is <see cref="XLItemsToRetain.Automatic"/>.</value> | ||
XLItemsToRetain ItemsToRetainPerField { get; set; } | ||
|
||
/// <summary> | ||
/// Will Excel refresh the cache when it opens the workbook. | ||
/// </summary> | ||
/// <value>Default value is <c>false</c>.</value> | ||
Boolean RefreshDataOnOpen { get; set; } | ||
|
||
/// <summary> | ||
/// Should the cached values of the pivot source be saved into the workbook file? | ||
/// If source data are not saved, they will have to be refreshed from the source | ||
/// reference which might cause a change in the table values. | ||
/// </summary> | ||
/// <value>Default value is <c>true</c>.</value> | ||
Boolean SaveSourceData { get; set; } | ||
|
||
/// <summary> | ||
/// Refresh data in the pivot source from the source reference data. | ||
/// </summary> | ||
IXLPivotCache Refresh(); | ||
|
||
/// <inheritdoc cref="ItemsToRetainPerField"/> | ||
IXLPivotCache SetItemsToRetainPerField(XLItemsToRetain value); | ||
|
||
/// <inheritdoc cref="RefreshDataOnOpen"/> | ||
/// <remarks>Sets the value to <c>true</c>.</remarks> | ||
IXLPivotCache SetRefreshDataOnOpen(); | ||
|
||
/// <inheritdoc cref="RefreshDataOnOpen"/> | ||
IXLPivotCache SetRefreshDataOnOpen(Boolean value); | ||
|
||
/// <inheritdoc cref="SaveSourceData"/> | ||
/// <remarks>Sets the value to <c>true</c>.</remarks> | ||
IXLPivotCache SetSaveSourceData(); | ||
|
||
/// <inheritdoc cref="SaveSourceData"/> | ||
IXLPivotCache SetSaveSourceData(Boolean value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace ClosedXML.Excel | ||
{ | ||
/// <summary> | ||
/// A collection of <see cref="IXLPivotCache">pivot caches</see>. Pivot cache | ||
/// can be added from a <see cref="IXLRange"/> or a <see cref="IXLTable"/>. | ||
/// </summary> | ||
public interface IXLPivotCaches : IEnumerable<IXLPivotCache> | ||
{ | ||
/// <summary> | ||
/// Add a new pivot cache. | ||
/// </summary> | ||
/// <param name="range">Range for which to create the pivot cache.</param> | ||
/// <returns>The pivot cache for the range.</returns> | ||
IXLPivotCache Add(IXLRange range); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.