Skip to content

Commit

Permalink
First attempt at refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
igitur authored and jahav committed Apr 10, 2023
1 parent 6e3078b commit 02fa34d
Show file tree
Hide file tree
Showing 29 changed files with 928 additions and 567 deletions.
7 changes: 3 additions & 4 deletions ClosedXML.Examples/PivotTables/PivotTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void Create(String filePath)
var table = ws.Cell(1, 1).InsertTable(pastries, "PastrySalesData", true);
ws.Columns().AdjustToContents();


IXLWorksheet ptSheet;
IXLPivotTable pt;

Expand Down Expand Up @@ -194,7 +193,7 @@ public void Create(String filePath)
.AddSelectedValue(new DateTime(2017, 05, 03));

#endregion Pivot Table with filter

#region Pivot table sorting

ptSheet = wb.Worksheets.Add("pvtSort");
Expand All @@ -208,7 +207,7 @@ public void Create(String filePath)

pt.SetRowHeaderCaption("Pastry name");

#endregion Different kind of pivot
#endregion Pivot table sorting

#region Pivot Table with integer rows

Expand All @@ -225,7 +224,7 @@ public void Create(String filePath)
pt.Values.Add("NumberOfOrders").SetSummaryFormula(XLPivotSummary.Sum);
pt.Values.Add("Quality").SetSummaryFormula(XLPivotSummary.Sum);

#endregion Pivot Table with filter
#endregion Pivot Table with integer rows

wb.SaveAs(filePath);
}
Expand Down
16 changes: 9 additions & 7 deletions ClosedXML.Tests/Excel/PivotTables/XLPivotTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void PivotTableOptionsSaveTest()
pt.ShowContextualTooltips = false;
pt.DisplayCaptionsAndDropdowns = false;
pt.RepeatRowLabels = true;
pt.SaveSourceData = false;
pt.Source.SaveSourceData = false;
pt.EnableShowDetails = false;
pt.ShowColumnHeaders = false;
pt.ShowRowHeaders = false;
Expand All @@ -104,7 +104,7 @@ public void PivotTableOptionsSaveTest()
pt.PrintTitles = true;

// TODO pt.RefreshDataOnOpen = false;
pt.ItemsToRetainPerField = XLItemsToRetain.Max;
pt.Source.ItemsToRetainPerField = XLItemsToRetain.Max;
pt.EnableCellEditing = true;
pt.ShowValuesRow = true;
pt.ShowRowStripes = true;
Expand Down Expand Up @@ -149,10 +149,10 @@ public void PivotTableOptionsSaveTest()
Assert.AreEqual(true, ptassert.PrintExpandCollapsedButtons, "PrintExpandCollapsedButtons save failure");
Assert.AreEqual(true, ptassert.RepeatRowLabels, "RepeatRowLabels save failure");
Assert.AreEqual(true, ptassert.PrintTitles, "PrintTitles save failure");
Assert.AreEqual(false, ptassert.SaveSourceData, "SaveSourceData save failure");
Assert.AreEqual(false, ptassert.Source.SaveSourceData, "SaveSourceData save failure");
Assert.AreEqual(false, ptassert.EnableShowDetails, "EnableShowDetails save failure");
// TODO Assert.AreEqual(false, ptassert.RefreshDataOnOpen, "RefreshDataOnOpen save failure");
Assert.AreEqual(XLItemsToRetain.Max, ptassert.ItemsToRetainPerField, "ItemsToRetainPerField save failure");
Assert.AreEqual(XLItemsToRetain.Max, ptassert.Source.ItemsToRetainPerField, "ItemsToRetainPerField save failure");
Assert.AreEqual(true, ptassert.EnableCellEditing, "EnableCellEditing save failure");
Assert.AreEqual(XLPivotTableTheme.PivotStyleDark13, ptassert.Theme, "Theme save failure");
Assert.AreEqual(true, ptassert.ShowValuesRow, "ShowValuesRow save failure");
Expand Down Expand Up @@ -508,7 +508,7 @@ public void PivotTableWithNoneTheme()
TestHelper.CreateAndCompare(() =>
{
var wb = new XLWorkbook(stream);
wb.SaveAs(ms);
wb.SaveAs(ms, validate: false);
return wb;
}, @"Other\PivotTableReferenceFiles\PivotTableWithNoneTheme\outputfile.xlsx");
}
Expand Down Expand Up @@ -737,9 +737,11 @@ public void TwoPivotWithOneSourceTest()
var wb = new XLWorkbook(stream);
var srcRange = wb.Range("Sheet1!$B$2:$H$207");
var pivotSource = wb.PivotSources.Add(srcRange);
foreach (var pt in wb.Worksheets.SelectMany(ws => ws.PivotTables))
{
pt.SourceRange = srcRange;
pt.Source = pivotSource;
}
return wb;
Expand All @@ -754,7 +756,7 @@ public void PivotSubtotalsLoadingTest()
{
var wb = new XLWorkbook(stream);
return wb;
}, @"Other\PivotTableReferenceFiles\PivotSubtotalsSource\input.xlsx");
}, @"Other\PivotTableReferenceFiles\PivotSubtotalsSource\output.xlsx");
}

[Test]
Expand Down
6 changes: 3 additions & 3 deletions ClosedXML.Tests/Utils/PivotTableComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public bool Equals(XLPivotTable x, XLPivotTable y)
&& x.PrintExpandCollapsedButtons.Equals(y.PrintExpandCollapsedButtons)
&& x.RepeatRowLabels.Equals(y.RepeatRowLabels)
&& x.PrintTitles.Equals(y.PrintTitles)
&& x.SaveSourceData.Equals(y.SaveSourceData)
&& x.Source.SaveSourceData.Equals(y.Source.SaveSourceData)
&& x.EnableShowDetails.Equals(y.EnableShowDetails)
&& x.RefreshDataOnOpen.Equals(y.RefreshDataOnOpen)
&& x.ItemsToRetainPerField.Equals(y.ItemsToRetainPerField)
&& x.Source.RefreshDataOnOpen.Equals(y.Source.RefreshDataOnOpen)
&& x.Source.ItemsToRetainPerField.Equals(y.Source.ItemsToRetainPerField)
&& x.EnableCellEditing.Equals(y.EnableCellEditing)
&& x.ShowRowHeaders.Equals(y.ShowRowHeaders)
&& x.ShowColumnHeaders.Equals(y.ShowColumnHeaders)
Expand Down
20 changes: 2 additions & 18 deletions ClosedXML/Excel/IXLWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,19 @@ public interface IXLWorkbook : IXLProtectable<IXLWorkbookProtection, XLWorkbookP
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 IsProtected { get; }

Boolean LockStructure { get; set; }

Boolean LockWindows { get; set; }

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

IXLPivotSources PivotSources { get; }

/// <summary>
/// Gets or sets the workbook's properties.
/// </summary>
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<XLCellValue>> 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 CopyTo(IXLCell targetCell);
Expand Down Expand Up @@ -124,13 +116,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 Array.Empty<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.PivotTable.ValuesSentinalLabel && !this._pivotTable.SourceRangeFieldsAvailable.Contains(sourceName))
if (sourceName != XLConstants.PivotTable.ValuesSentinalLabel && !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 @@ -130,6 +130,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.PivotTable.ValuesSentinalLabel && !this._pivotTable.SourceRangeFieldsAvailable.Contains(sourceName))
if (sourceName != XLConstants.PivotTable.ValuesSentinalLabel && !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

0 comments on commit 02fa34d

Please sign in to comment.