diff --git a/ClosedXML/Excel/PivotTables/XLPivotTable.cs b/ClosedXML/Excel/PivotTables/XLPivotTable.cs index 0e20ea8..80124a0 100644 --- a/ClosedXML/Excel/PivotTables/XLPivotTable.cs +++ b/ClosedXML/Excel/PivotTables/XLPivotTable.cs @@ -44,6 +44,44 @@ 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; } @@ -53,22 +91,8 @@ var oldname = _name ?? string.Empty; - if (String.IsNullOrWhiteSpace(value)) - throw new ArgumentException($"The table name '{value}' is invalid"); - - // Table names are case insensitive - if (!oldname.Equals(value, StringComparison.OrdinalIgnoreCase) - && Worksheet.Tables.Any(t => t.Name.Equals(value, StringComparison.OrdinalIgnoreCase))) - throw new ArgumentException($"This worksheet already contains a table named '{value}'"); - - if (value[0] != '_' && !char.IsLetter(value[0])) - throw new ArgumentException($"The table name '{value}' does not begin with a letter or an underscore"); - - if (value.Length > 255) - throw new ArgumentException("The table name is more than 255 characters"); - - if (new[] { 'C', 'R' }.Any(c => value.ToUpper().Equals(c.ToString()))) - throw new ArgumentException($"The table name '{value}' is invalid"); + if (!ValidateName(value, oldname, 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 b1068d7..73df4a2 100644 --- a/ClosedXML/Excel/Tables/XLTable.cs +++ b/ClosedXML/Excel/Tables/XLTable.cs @@ -201,6 +201,44 @@ 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; } @@ -211,22 +249,8 @@ // Validation rules for table names var oldname = _name ?? string.Empty; - if (String.IsNullOrWhiteSpace(value)) - throw new ArgumentException($"The table name '{value}' is invalid"); - - // Table names are case insensitive - if (!oldname.Equals(value, StringComparison.OrdinalIgnoreCase) - && Worksheet.Tables.Any(t => t.Name.Equals(value, StringComparison.OrdinalIgnoreCase))) - throw new ArgumentException($"This worksheet already contains a table named '{value}'"); - - if (value[0] != '_' && !char.IsLetter(value[0])) - throw new ArgumentException($"The table name '{value}' does not begin with a letter or an underscore"); - - if (value.Length > 255) - throw new ArgumentException("The table name is more than 255 characters"); - - if (new[] { 'C', 'R' }.Any(c => value.ToUpper().Equals(c.ToString()))) - throw new ArgumentException($"The table name '{value}' is invalid"); + if (!ValidateName(value, oldname, out String message)) + throw new ArgumentException(message, nameof(value)); _name = value;