diff --git a/ClosedXML/Excel/PivotTables/XLPivotTable.cs b/ClosedXML/Excel/PivotTables/XLPivotTable.cs
index 80124a0..5985ed4 100644
--- a/ClosedXML/Excel/PivotTables/XLPivotTable.cs
+++ b/ClosedXML/Excel/PivotTables/XLPivotTable.cs
@@ -44,44 +44,6 @@
public IXLPivotTable SetTheme(XLPivotTableTheme value) { Theme = value; return this; }
- private Boolean ValidateName(String newName, String oldName, out String message)
- {
- message = "";
- if (String.IsNullOrWhiteSpace(newName))
- {
- message = $"The table name '{newName}' is invalid";
- return false;
- }
-
- // Table names are case insensitive
- if (!oldName.Equals(newName, StringComparison.OrdinalIgnoreCase)
- && Worksheet.Tables.Any(t => t.Name.Equals(newName, StringComparison.OrdinalIgnoreCase)))
- {
- message = $"This worksheet already contains a table named '{newName}'";
- return false;
- }
-
- if (newName[0] != '_' && !char.IsLetter(newName[0]))
- {
- message = $"The table name '{newName}' does not begin with a letter or an underscore";
- return false;
- }
-
- if (newName.Length > 255)
- {
- message = "The table name is more than 255 characters";
- return false;
- }
-
- if (new[] { 'C', 'R' }.Any(c => newName.ToUpper().Equals(c.ToString())))
- {
- message = $"The table name '{newName}' is invalid";
- return false;
- }
-
- return true;
- }
-
public String Name
{
get { return _name; }
@@ -91,7 +53,7 @@
var oldname = _name ?? string.Empty;
- if (!ValidateName(value, oldname, out String message))
+ if (!XLHelper.ValidateName("pivot table", value, oldname, Worksheet.PivotTables.Select(pvt => pvt.Name), out String message))
throw new ArgumentException(message, nameof(value));
_name = value;
diff --git a/ClosedXML/Excel/Tables/XLTable.cs b/ClosedXML/Excel/Tables/XLTable.cs
index 73df4a2..e37e473 100644
--- a/ClosedXML/Excel/Tables/XLTable.cs
+++ b/ClosedXML/Excel/Tables/XLTable.cs
@@ -201,44 +201,6 @@
public XLTableTheme Theme { get; set; }
- private Boolean ValidateName(String newName, String oldName, out String message)
- {
- message = "";
- if (String.IsNullOrWhiteSpace(newName))
- {
- message = $"The table name '{newName}' is invalid";
- return false;
- }
-
- // Table names are case insensitive
- if (!oldName.Equals(newName, StringComparison.OrdinalIgnoreCase)
- && Worksheet.Tables.Any(t => t.Name.Equals(newName, StringComparison.OrdinalIgnoreCase)))
- {
- message = $"This worksheet already contains a table named '{newName}'";
- return false;
- }
-
- if (newName[0] != '_' && !char.IsLetter(newName[0]))
- {
- message = $"The table name '{newName}' does not begin with a letter or an underscore";
- return false;
- }
-
- if (newName.Length > 255)
- {
- message = "The table name is more than 255 characters";
- return false;
- }
-
- if (new[] { 'C', 'R' }.Any(c => newName.ToUpper().Equals(c.ToString())))
- {
- message = $"The table name '{newName}' is invalid";
- return false;
- }
-
- return true;
- }
-
public String Name
{
get { return _name; }
@@ -249,7 +211,7 @@
// Validation rules for table names
var oldname = _name ?? string.Empty;
- if (!ValidateName(value, oldname, out String message))
+ if (!XLHelper.ValidateName("table", value, oldname, Worksheet.Tables.Select(t => t.Name), out String message))
throw new ArgumentException(message, nameof(value));
_name = value;
diff --git a/ClosedXML/XLHelper.cs b/ClosedXML/XLHelper.cs
index 43aa729..0b8b156 100644
--- a/ClosedXML/XLHelper.cs
+++ b/ClosedXML/XLHelper.cs
@@ -1,6 +1,6 @@
using System;
-using System.Drawing;
using System.Collections.Generic;
+using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
@@ -19,7 +19,7 @@
public const String MaxColumnLetter = "XFD";
public const Double Epsilon = 1e-10;
- private const Int32 TwoT26 = 26*26;
+ private const Int32 TwoT26 = 26 * 26;
internal static readonly Graphics Graphic = Graphics.FromImage(new Bitmap(200, 200));
internal static readonly Double DpiX = Graphic.DpiX;
@@ -101,7 +101,6 @@
throw new ArgumentOutOfRangeException(columnLetter + " is not recognized as a column letter");
}
-
///
/// Gets the column letter of a given column number.
///
@@ -320,5 +319,43 @@
{
return -657435 <= d && d < 2958466;
}
+
+ public static Boolean ValidateName(String objectType, String newName, String oldName, IEnumerable existingNames, out String message)
+ {
+ message = "";
+ if (String.IsNullOrWhiteSpace(newName))
+ {
+ message = $"The {objectType} name '{newName}' is invalid";
+ return false;
+ }
+
+ // Table names are case insensitive
+ if (!oldName.Equals(newName, StringComparison.OrdinalIgnoreCase)
+ && existingNames.Contains(newName, StringComparer.OrdinalIgnoreCase))
+ {
+ message = $"There is already a {objectType} named '{newName}'";
+ return false;
+ }
+
+ if (newName[0] != '_' && !char.IsLetter(newName[0]))
+ {
+ message = $"The {objectType} name '{newName}' does not begin with a letter or an underscore";
+ return false;
+ }
+
+ if (newName.Length > 255)
+ {
+ message = $"The {objectType} name is more than 255 characters";
+ return false;
+ }
+
+ if (new[] { 'C', 'R' }.Any(c => newName.ToUpper().Equals(c.ToString())))
+ {
+ message = $"The {objectType} name '{newName}' is invalid";
+ return false;
+ }
+
+ return true;
+ }
}
}