Skip to content

Commit

Permalink
More WIP...
Browse files Browse the repository at this point in the history
  • Loading branch information
igitur committed Jun 26, 2019
1 parent 024dadc commit d6fb4ab
Show file tree
Hide file tree
Showing 18 changed files with 395 additions and 224 deletions.
16 changes: 15 additions & 1 deletion ClosedXML/Excel/Patterns/ClosedXMLValueComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,21 @@ public int GetHashCode(object obj)
if (obj is null)
return 0;

return obj.GetHashCode();
switch (obj)
{
case DateTime dt:
return dt.ToOADate().GetHashCode();
case Double dbl:
return dbl.GetHashCode();
case TimeSpan ts:
return ts.TotalDays.GetHashCode();
case Boolean b:
return (b ? 1d : 0d).GetHashCode();
case String s:
return StringComparer.OrdinalIgnoreCase.GetHashCode(s);
default:
throw new NotImplementedException();
}
}

private int CompareWithCoersion(IComparable c1, IComparable c2)
Expand Down
28 changes: 28 additions & 0 deletions ClosedXML/Excel/PivotTables/IXLPivotSource.cs
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);
}
}
11 changes: 11 additions & 0 deletions ClosedXML/Excel/PivotTables/IXLPivotSourceReference.cs
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; }
}
}
70 changes: 15 additions & 55 deletions ClosedXML/Excel/PivotTables/XLPivotSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,21 @@

namespace ClosedXML.Excel
{
public interface IXLPivotSource
{
IDictionary<String, IList<Object>> CachedFields { get; }
XLItemsToRetain ItemsToRetainPerField { get; set; }
Boolean RefreshDataOnOpen { get; set; }

Boolean SaveSourceData { get; set; }

IXLRange SourceRange { get; set; }

IList<String> SourceRangeFields { get; }

IXLTable SourceTable { get; set; }

XLPivotTableSourceType SourceType { get; }

IXLPivotSource Refresh();

IXLPivotSource SetItemsToRetainPerField(XLItemsToRetain value);

IXLPivotSource SetRefreshDataOnOpen();

IXLPivotSource SetRefreshDataOnOpen(Boolean value);

IXLPivotSource SetSaveSourceData();

IXLPivotSource SetSaveSourceData(Boolean value);
}

internal class XLPivotSource : IXLPivotSource
{
private IXLRange sourceRange;
public XLPivotSource(IXLRange sourceRange)
: this()
{
this.PivotSourceReference = new XLPivotSourceReference { SourceRange = sourceRange };
}

public XLPivotSource(IXLTable table)
: this()
{
this.PivotSourceReference = new XLPivotSourceReference { SourceTable = table };
}

public XLPivotSource()
private XLPivotSource()
{
this.Guid = Guid.NewGuid();
CachedFields = new Dictionary<String, IList<Object>>(StringComparer.OrdinalIgnoreCase);
Expand All @@ -48,29 +29,16 @@ public XLPivotSource()
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 IXLRange SourceRange
{
get { return sourceRange; }
set
{
if (value is IXLTable)
SourceType = XLPivotTableSourceType.Table;
else
SourceType = XLPivotTableSourceType.Range;

sourceRange = value;
}
}

public IList<String> SourceRangeFields
{
get
{
return this.SourceRange
return this.PivotSourceReference.SourceRange
.FirstRow()
.Cells()
.Select(c => c.GetString())
Expand All @@ -79,22 +47,14 @@ public IList<String> SourceRangeFields
}
}

public IXLTable SourceTable
{
get { return SourceRange as IXLTable; }
set { SourceRange = value; }
}

public XLPivotTableSourceType SourceType { get; private set; }

internal uint? CacheId { get; set; }
internal String WorkbookCacheRelId { get; set; }

public IXLPivotSource Refresh()
{
CachedFields.Clear();

foreach (var column in SourceRange.Columns())
foreach (var column in PivotSourceReference.SourceRange.Columns())
{
var header = column.FirstCell().GetFormattedString();
var firstCellAddress = column.FirstCell().Address;
Expand Down
55 changes: 55 additions & 0 deletions ClosedXML/Excel/PivotTables/XLPivotSourceReference.cs
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
}
}
48 changes: 38 additions & 10 deletions ClosedXML/Excel/PivotTables/XLPivotSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace ClosedXML.Excel
{
public interface IXLPivotSources : IEnumerable<IXLPivotSource>
{
IXLPivotSource Add(IXLPivotSourceReference pivotSourceReference);

IXLPivotSource Add(IXLRange range);

IXLPivotSource Add(IXLTable table);
Expand All @@ -26,37 +28,63 @@ internal class XLPivotSources : IXLPivotSources
private readonly IDictionary<String, IXLPivotSource> _tablePivotSources = new Dictionary<String, IXLPivotSource>(StringComparer.OrdinalIgnoreCase);

public IXLPivotSource Add(IXLRange range)
{
return Add(range, refresh: true);
}

public IXLPivotSource Add(IXLRange range, bool refresh)
{
if (_rangePivotSources.TryGetValue(range, out IXLPivotSource pivotSource))
return pivotSource;
else
{
var newPivotSource = new XLPivotSource()
{
SourceRange = range
};
newPivotSource.Refresh();
var newPivotSource = new XLPivotSource(range);
if (refresh)
newPivotSource.Refresh();
_rangePivotSources.Add(range, newPivotSource);
return newPivotSource;
}
}

public IXLPivotSource Add(IXLTable table)
{
return Add(table, refresh: true);
}

public IXLPivotSource Add(IXLTable table, bool refresh)
{
if (_tablePivotSources.TryGetValue(table.Name, out IXLPivotSource pivotSource))
return pivotSource;
else
{
var newPivotSource = new XLPivotSource()
{
SourceTable = table
};
newPivotSource.Refresh();
var newPivotSource = new XLPivotSource(table);
if (refresh)
newPivotSource.Refresh();
_tablePivotSources.Add(table.Name, newPivotSource);
return newPivotSource;
}
}

public IXLPivotSource Add(IXLPivotSourceReference pivotSourceReference)
{
return Add(pivotSourceReference, refresh: true);
}

public IXLPivotSource Add(IXLPivotSourceReference pivotSourceReference, bool refresh)
{
switch (pivotSourceReference.SourceType)
{
case XLPivotTableSourceType.Table:
return Add(pivotSourceReference.SourceTable, refresh);

case XLPivotTableSourceType.Range:
return Add(pivotSourceReference.SourceRange, refresh);

default:
throw new NotImplementedException();
}
}

public bool Contains(IXLRange range)
{
return _rangePivotSources.ContainsKey(range);
Expand Down
10 changes: 2 additions & 8 deletions ClosedXML/Excel/PivotTables/XLPivotTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,15 @@ public IXLPivotTable Add(string name, IXLCell targetCell, IXLPivotSource pivotSo
public IXLPivotTable Add(string name, IXLCell targetCell, IXLRange range)
{
if (!this.Worksheet.Workbook.PivotSources.TryGet(range, out IXLPivotSource pivotSource))
{
pivotSource = new XLPivotSource() { SourceRange = range };
pivotSource.Refresh();
}
pivotSource = this.Worksheet.Workbook.PivotSources.Add(range);

return Add(name, targetCell, pivotSource);
}

public IXLPivotTable Add(string name, IXLCell targetCell, IXLTable table)
{
if (!this.Worksheet.Workbook.PivotSources.TryGet(table, out IXLPivotSource pivotSource))
{
pivotSource = new XLPivotSource() { SourceTable = table };
pivotSource.Refresh();
}
pivotSource = this.Worksheet.Workbook.PivotSources.Add(table);

return Add(name, targetCell, pivotSource);
}
Expand Down

0 comments on commit d6fb4ab

Please sign in to comment.