diff --git a/ClosedXML/Excel/PivotTables/IXLPivotTable.cs b/ClosedXML/Excel/PivotTables/IXLPivotTable.cs index 91bcafd..03f76d1 100644 --- a/ClosedXML/Excel/PivotTables/IXLPivotTable.cs +++ b/ClosedXML/Excel/PivotTables/IXLPivotTable.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace ClosedXML.Excel { @@ -117,20 +118,22 @@ IXLCell TargetCell { get; set; } IXLRange SourceRange { get; set; } + IEnumerable SourceRangeFieldsAvailable { get; } + Boolean MergeAndCenterWithLabels { get; set; } // MergeItem Int32 RowLabelIndent { get; set; } // Indent - XLFilterAreaOrder FilterAreaOrder { get; set; } // PageOverThenDown + XLFilterAreaOrder FilterAreaOrder { get; set; } // PageOverThenDown Int32 FilterFieldsPageWrap { get; set; } // PageWrap String ErrorValueReplacement { get; set; } // ErrorCaption String EmptyCellReplacement { get; set; } // MissingCaption - Boolean AutofitColumns { get; set; } //UseAutoFormatting - Boolean PreserveCellFormatting { get; set; } // PreserveFormatting - - Boolean ShowGrandTotalsRows { get; set; } // RowGrandTotals - Boolean ShowGrandTotalsColumns { get; set; } // ColumnGrandTotals + Boolean AutofitColumns { get; set; } //UseAutoFormatting + Boolean PreserveCellFormatting { get; set; } // PreserveFormatting + + Boolean ShowGrandTotalsRows { get; set; } // RowGrandTotals + Boolean ShowGrandTotalsColumns { get; set; } // ColumnGrandTotals Boolean FilteredItemsInSubtotals { get; set; } // Subtotal filtered page items - Boolean AllowMultipleFilters { get; set; } // MultipleFieldFilters - Boolean UseCustomListsForSorting { get; set; } // CustomListSort + Boolean AllowMultipleFilters { get; set; } // MultipleFieldFilters + Boolean UseCustomListsForSorting { get; set; } // CustomListSort Boolean ShowExpandCollapseButtons { get; set; } Boolean ShowContextualTooltips { get; set; } diff --git a/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValues.cs b/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValues.cs index 4e09f16..e5e124d 100644 --- a/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValues.cs +++ b/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValues.cs @@ -32,6 +32,9 @@ } public IXLPivotValue Add(String sourceName, String customName) { + if (!this._pivotTable.SourceRangeFieldsAvailable.Contains(sourceName, StringComparer.OrdinalIgnoreCase)) + 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 }; _pivotValues.Add(customName, pivotValue); diff --git a/ClosedXML/Excel/PivotTables/XLPivotFields.cs b/ClosedXML/Excel/PivotTables/XLPivotFields.cs index 19accb7..42abd9d 100644 --- a/ClosedXML/Excel/PivotTables/XLPivotFields.cs +++ b/ClosedXML/Excel/PivotTables/XLPivotFields.cs @@ -4,9 +4,16 @@ namespace ClosedXML.Excel { - public class XLPivotFields : IXLPivotFields + internal class XLPivotFields : IXLPivotFields { + private readonly Dictionary _pivotFields = new Dictionary(); + private readonly IXLPivotTable _pivotTable; + + internal XLPivotFields(IXLPivotTable pivotTable) + { + this._pivotTable = pivotTable; + } public IXLPivotField Add(String sourceName) { @@ -15,6 +22,9 @@ public IXLPivotField Add(String sourceName, String customName) { + if (!this._pivotTable.SourceRangeFieldsAvailable.Contains(sourceName, StringComparer.OrdinalIgnoreCase)) + throw new ArgumentOutOfRangeException(nameof(sourceName), String.Format("The column '{0}' does not appear in the source range.", sourceName)); + var pivotField = new XLPivotField(sourceName) { CustomName = customName }; _pivotFields.Add(sourceName, pivotField); return pivotField; @@ -49,7 +59,8 @@ { var selectedItem = _pivotFields.Select((item, index) => new { Item = item, Position = index }).FirstOrDefault(i => i.Item.Key == pf.SourceName); if (selectedItem == null) - throw new IndexOutOfRangeException("Invalid field name."); + throw new ArgumentNullException(nameof(pf), "Invalid field name."); + return selectedItem.Position; } diff --git a/ClosedXML/Excel/PivotTables/XLPivotTable.cs b/ClosedXML/Excel/PivotTables/XLPivotTable.cs index acb4a84..e1232cb 100644 --- a/ClosedXML/Excel/PivotTables/XLPivotTable.cs +++ b/ClosedXML/Excel/PivotTables/XLPivotTable.cs @@ -10,10 +10,10 @@ public XLPivotTable() { - Fields = new XLPivotFields(); - ReportFilters = new XLPivotFields(); - ColumnLabels=new XLPivotFields(); - RowLabels = new XLPivotFields(); + Fields = new XLPivotFields(this); + ReportFilters = new XLPivotFields(this); + ColumnLabels=new XLPivotFields(this); + RowLabels = new XLPivotFields(this); Values = new XLPivotValues(this); Theme = XLPivotTableTheme.PivotStyleLight16; @@ -22,6 +22,10 @@ public IXLCell TargetCell { get; set; } public IXLRange SourceRange { get; set; } + public IEnumerable SourceRangeFieldsAvailable + { + get { return this.SourceRange.FirstRow().Cells().Select(c => c.GetString()); } + } public IXLPivotFields Fields { get; private set; } public IXLPivotFields ReportFilters { get; private set; }