Skip to content

Commit

Permalink
First attempt at refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
igitur committed Jun 26, 2019
1 parent e06be3e commit c28800c
Show file tree
Hide file tree
Showing 30 changed files with 982 additions and 526 deletions.
17 changes: 2 additions & 15 deletions ClosedXML/Excel/IXLWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,20 @@ public interface IXLWorkbook : IDisposable
Double ColumnWidth { get; set; }

IXLCustomProperties CustomProperties { get; }

Boolean DefaultRightToLeft { get; }

Boolean DefaultShowFormulas { get; }

Boolean DefaultShowGridLines { get; }

Boolean DefaultShowOutlineSymbols { get; }

Boolean DefaultShowRowColHeaders { get; }

Boolean DefaultShowRuler { get; }

Boolean DefaultShowWhiteSpace { get; }

Boolean DefaultShowZeros { get; }

IXLFileSharing FileSharing { get; }

Boolean ForceFullCalculation { get; set; }

Boolean FullCalculationOnLoad { get; set; }

Boolean FullPrecision { get; set; }

Boolean IsPasswordProtected { get; }

Boolean LockStructure { get; set; }

Boolean LockWindows { get; set; }

/// <summary>
Expand All @@ -73,6 +58,8 @@ public interface IXLWorkbook : IDisposable
/// </summary>
IXLPageSetup PageOptions { get; set; }

IXLPivotSources PivotSources { get; }

/// <summary>
/// Gets or sets the workbook's properties.
/// </summary>
Expand Down
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; }
}
}
13 changes: 1 addition & 12 deletions ClosedXML/Excel/PivotTables/IXLPivotTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public interface IXLPivotTable
{
XLPivotTableTheme Theme { get; set; }

IXLPivotFields Fields { get; }
IXLPivotFields ReportFilters { get; }
IXLPivotFields ColumnLabels { get; }
IXLPivotFields RowLabels { get; }
Expand All @@ -22,11 +21,7 @@ public interface IXLPivotTable

IXLCell TargetCell { get; set; }

IXLRange SourceRange { get; set; }
IXLTable SourceTable { get; set; }
XLPivotTableSourceType SourceType { get; }

IEnumerable<String> SourceRangeFieldsAvailable { get; }
IXLPivotSource Source { get; set; }

Boolean MergeAndCenterWithLabels { get; set; } // MergeItem
Int32 RowLabelIndent { get; set; } // Indent
Expand Down Expand Up @@ -58,10 +53,7 @@ public interface IXLPivotTable
Boolean RepeatRowLabels { get; set; }
Boolean PrintTitles { get; set; }

Boolean SaveSourceData { get; set; }
Boolean EnableShowDetails { get; set; }
Boolean RefreshDataOnOpen { get; set; }
XLItemsToRetain ItemsToRetainPerField { get; set; }
Boolean EnableCellEditing { get; set; }

IXLPivotTable SetName(String value);
Expand Down Expand Up @@ -122,13 +114,10 @@ public interface IXLPivotTable

IXLPivotTable SetPrintTitles(); IXLPivotTable SetPrintTitles(Boolean value);

IXLPivotTable SetSaveSourceData(); IXLPivotTable SetSaveSourceData(Boolean value);

IXLPivotTable SetEnableShowDetails(); IXLPivotTable SetEnableShowDetails(Boolean value);

IXLPivotTable SetRefreshDataOnOpen(); IXLPivotTable SetRefreshDataOnOpen(Boolean value);

IXLPivotTable SetItemsToRetainPerField(XLItemsToRetain value);

IXLPivotTable SetEnableCellEditing(); IXLPivotTable SetEnableCellEditing(Boolean value);

Expand Down
2 changes: 2 additions & 0 deletions ClosedXML/Excel/PivotTables/IXLPivotTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace ClosedXML.Excel
{
public interface IXLPivotTables : IEnumerable<IXLPivotTable>
{
IXLPivotTable Add(String name, IXLCell targetCell, IXLPivotSource pivotSource);

IXLPivotTable Add(String name, IXLCell targetCell, IXLRange range);

IXLPivotTable Add(String name, IXLCell targetCell, IXLTable table);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ internal abstract class AbstractPivotFieldReference
/// <P>Helper function used during saving to calculate the indices of the filtered values</P>
/// </summary>
/// <returns>Indices of the filtered values</returns>
internal abstract IEnumerable<Int32> Match(XLWorkbook.PivotTableInfo pti, IXLPivotTable pt);
internal abstract IEnumerable<Int32> Match(XLWorkbook.PivotSourceInfo psi, IXLPivotTable pt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ internal override UInt32Value GetFieldOffset()
return UInt32Value.FromUInt32((uint)PivotField.Offset);
}

internal override IEnumerable<Int32> Match(XLWorkbook.PivotTableInfo pti, IXLPivotTable pt)
internal override IEnumerable<Int32> Match(XLWorkbook.PivotSourceInfo psi, IXLPivotTable pt)
{
var values = pti.Fields[PivotField.SourceName].DistinctValues.ToList();
var values = psi.Fields[PivotField.SourceName].DistinctValues.ToList();

if (predicate == null)
return new Int32[] { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal override UInt32Value GetFieldOffset()
return UInt32Value.FromUInt32(unchecked((uint)-2));
}

internal override IEnumerable<Int32> Match(XLWorkbook.PivotTableInfo pti, IXLPivotTable pt)
internal override IEnumerable<Int32> Match(XLWorkbook.PivotSourceInfo psi, IXLPivotTable pt)
{
return new Int32[]
{
Expand Down
2 changes: 1 addition & 1 deletion ClosedXML/Excel/PivotTables/PivotValues/XLPivotValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public IXLPivotValue Add(String sourceName)

public IXLPivotValue Add(String sourceName, String customName)
{
if (sourceName != XLConstants.PivotTableValuesSentinalLabel && !this._pivotTable.SourceRangeFieldsAvailable.Contains(sourceName))
if (sourceName != XLConstants.PivotTableValuesSentinalLabel && !this._pivotTable.Source.SourceRangeFields.Contains(sourceName))
throw new ArgumentOutOfRangeException(nameof(sourceName), String.Format("The column '{0}' does not appear in the source range.", sourceName));

var pivotValue = new XLPivotValue(sourceName) { CustomName = customName };
Expand Down
2 changes: 1 addition & 1 deletion ClosedXML/Excel/PivotTables/XLPivotField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ private void SetExcelDefaults()

public Boolean IsInFilterList => _pivotTable.ReportFilters.Contains(this.SourceName);

public Int32 Offset => _pivotTable.SourceRangeFieldsAvailable.ToList().IndexOf(this.SourceName);
public Int32 Offset => _pivotTable.Source.SourceRangeFields.ToList().IndexOf(this.SourceName);
}
}
2 changes: 1 addition & 1 deletion ClosedXML/Excel/PivotTables/XLPivotFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public IXLPivotField Add(String sourceName)

public IXLPivotField Add(String sourceName, String customName)
{
if (sourceName != XLConstants.PivotTableValuesSentinalLabel && !this._pivotTable.SourceRangeFieldsAvailable.Contains(sourceName))
if (sourceName != XLConstants.PivotTableValuesSentinalLabel && !this._pivotTable.Source.SourceRangeFields.Contains(sourceName))
throw new ArgumentOutOfRangeException(nameof(sourceName), String.Format("The column '{0}' does not appear in the source range.", sourceName));

var pivotField = new XLPivotField(_pivotTable, sourceName) { CustomName = customName };
Expand Down
107 changes: 107 additions & 0 deletions ClosedXML/Excel/PivotTables/XLPivotSource.cs
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;
}
}
}
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
}
}

0 comments on commit c28800c

Please sign in to comment.