-
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.
- Loading branch information
Showing
30 changed files
with
982 additions
and
526 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
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,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace ClosedXML.Excel | ||
{ | ||
public interface IXLPivotSource | ||
{ | ||
IDictionary<String, IList<Object>> CachedFields { get; } | ||
XLItemsToRetain ItemsToRetainPerField { get; set; } | ||
IXLPivotSourceReference PivotSourceReference { get; } | ||
Boolean RefreshDataOnOpen { get; set; } | ||
|
||
Boolean SaveSourceData { get; set; } | ||
IList<String> SourceRangeFields { get; } | ||
|
||
IXLPivotSource Refresh(); | ||
|
||
IXLPivotSource SetItemsToRetainPerField(XLItemsToRetain value); | ||
|
||
IXLPivotSource SetRefreshDataOnOpen(); | ||
|
||
IXLPivotSource SetRefreshDataOnOpen(Boolean value); | ||
|
||
IXLPivotSource SetSaveSourceData(); | ||
|
||
IXLPivotSource 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,11 @@ | ||
using System; | ||
|
||
namespace ClosedXML.Excel | ||
{ | ||
public interface IXLPivotSourceReference : IEquatable<IXLPivotSourceReference> | ||
{ | ||
IXLRange SourceRange { get; set; } | ||
IXLTable SourceTable { get; set; } | ||
XLPivotTableSourceType SourceType { get; } | ||
} | ||
} |
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
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
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,107 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace ClosedXML.Excel | ||
{ | ||
internal class XLPivotSource : IXLPivotSource | ||
{ | ||
public XLPivotSource(IXLRange sourceRange) | ||
: this() | ||
{ | ||
this.PivotSourceReference = new XLPivotSourceReference { SourceRange = sourceRange }; | ||
} | ||
|
||
public XLPivotSource(IXLTable table) | ||
: this() | ||
{ | ||
this.PivotSourceReference = new XLPivotSourceReference { SourceTable = table }; | ||
} | ||
|
||
private XLPivotSource() | ||
{ | ||
this.Guid = Guid.NewGuid(); | ||
CachedFields = new Dictionary<String, IList<Object>>(StringComparer.OrdinalIgnoreCase); | ||
SetExcelDefaults(); | ||
} | ||
|
||
public IDictionary<String, IList<Object>> CachedFields { get; internal set; } | ||
public Guid Guid { get; private set; } | ||
public XLItemsToRetain ItemsToRetainPerField { get; set; } | ||
|
||
public IXLPivotSourceReference PivotSourceReference { get; set; } | ||
public Boolean RefreshDataOnOpen { get; set; } | ||
|
||
public Boolean SaveSourceData { get; set; } | ||
|
||
public IList<String> SourceRangeFields | ||
{ | ||
get | ||
{ | ||
return this.PivotSourceReference.SourceRange | ||
.FirstRow() | ||
.Cells() | ||
.Select(c => c.GetString()) | ||
.ToList() | ||
.AsReadOnly(); | ||
} | ||
} | ||
|
||
internal uint? CacheId { get; set; } | ||
internal String WorkbookCacheRelId { get; set; } | ||
|
||
public IXLPivotSource Refresh() | ||
{ | ||
CachedFields.Clear(); | ||
|
||
foreach (var column in PivotSourceReference.SourceRange.Columns()) | ||
{ | ||
var header = column.FirstCell().GetFormattedString(); | ||
var firstCellAddress = column.FirstCell().Address; | ||
var values = column.CellsUsed(c => !c.Address.Equals(firstCellAddress)) | ||
.Select(c => c.Value) | ||
.Distinct() | ||
.ToList(); | ||
|
||
CachedFields.Add(AdjustedFieldName(header), values); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public IXLPivotSource SetItemsToRetainPerField(XLItemsToRetain value) { ItemsToRetainPerField = value; return this; } | ||
|
||
public IXLPivotSource SetRefreshDataOnOpen() { RefreshDataOnOpen = true; return this; } | ||
|
||
public IXLPivotSource SetRefreshDataOnOpen(Boolean value) { RefreshDataOnOpen = value; return this; } | ||
|
||
public IXLPivotSource SetSaveSourceData() { SaveSourceData = true; return this; } | ||
|
||
public IXLPivotSource SetSaveSourceData(Boolean value) { SaveSourceData = value; return this; } | ||
|
||
internal XLPivotSource AddCachedField(String fieldName, List<Object> items) | ||
{ | ||
var cachedFields = CachedFields as Dictionary<String, IList<Object>>; | ||
cachedFields.Add(fieldName, items); | ||
return this; | ||
} | ||
|
||
private string AdjustedFieldName(string header) | ||
{ | ||
var modifiedHeader = header; | ||
int i = 1; | ||
while (CachedFields.ContainsKey(modifiedHeader)) | ||
{ | ||
i++; | ||
modifiedHeader = header + i.ToInvariantString(); | ||
} | ||
|
||
return modifiedHeader; | ||
} | ||
|
||
private void SetExcelDefaults() | ||
{ | ||
SaveSourceData = true; | ||
} | ||
} | ||
} |
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,55 @@ | ||
using ClosedXML.Excel.CalcEngine; | ||
using ClosedXML.Excel.Patterns; | ||
using System; | ||
|
||
namespace ClosedXML.Excel | ||
{ | ||
internal class XLPivotSourceReference : IXLPivotSourceReference | ||
{ | ||
private IXLRange sourceRange; | ||
|
||
public IXLRange SourceRange | ||
{ | ||
get { return sourceRange; } | ||
set | ||
{ | ||
if (value is IXLTable) | ||
SourceType = XLPivotTableSourceType.Table; | ||
else | ||
SourceType = XLPivotTableSourceType.Range; | ||
|
||
sourceRange = value; | ||
} | ||
} | ||
|
||
public IXLTable SourceTable | ||
{ | ||
get { return SourceRange as IXLTable; } | ||
set { SourceRange = value; } | ||
} | ||
|
||
public XLPivotTableSourceType SourceType { get; private set; } | ||
|
||
#region IEquatable interface | ||
|
||
public bool Equals(IXLPivotSourceReference other) | ||
{ | ||
if (this.SourceType != other.SourceType) return false; | ||
|
||
switch (this.SourceType) | ||
{ | ||
case XLPivotTableSourceType.Table: | ||
return ClosedXMLValueComparer.DefaultComparer.Compare(this.SourceTable.Name, other.SourceTable.Name) == 0; | ||
|
||
case XLPivotTableSourceType.Range: | ||
var rangeAddressComparer = new XLRangeAddressComparer(true); | ||
return rangeAddressComparer.Equals(this.SourceRange.RangeAddress, other.SourceRange.RangeAddress); | ||
|
||
default: | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
#endregion IEquatable interface | ||
} | ||
} |
Oops, something went wrong.