NanoXLSX.Core 3.2.1
Loading...
Searching...
No Matches
Validators.cs
1using System;
2using System.Text.RegularExpressions;
5
6
7namespace NanoXLSX.Utils
8{
12 public static class Validators
13 {
14
21 // <exception cref = "StyleException" > A StyleException is thrown if an invalid hex value is passed</exception>
22 public static void ValidateGenericColor(string hexCode, bool allowEmpty = false)
23 {
24 string argbMessage = ValidateColorInternal(hexCode, true, allowEmpty);
25 string rgbMessage = null;
26 if (argbMessage != null)
27 {
28 rgbMessage = ValidateColorInternal(hexCode, false, allowEmpty);
29 if (rgbMessage != null)
30 {
31 throw new StyleException(argbMessage);
32 }
33 }
34 }
35
43 public static void ValidateColor(string hexCode, bool useAlpha, bool allowEmpty = false)
44 {
45 string message = ValidateColorInternal(hexCode, useAlpha, allowEmpty);
46 if (message != null)
47 {
48 throw new StyleException(message);
49 }
50 }
51
59 public static void ValidateCellAddressExpression(string expression, Cell.AddressScope scope = Cell.AddressScope.Any)
60 {
61 bool isCellAddress = false;
62 bool isRange = false;
63 Exception lastException = null;
64 try
65 {
66 Cell.ResolveCellCoordinate(expression);
67 isCellAddress = true;
68 }
69 catch (Exception ex)
70 {
71 if (scope == Cell.AddressScope.SingleAddress)
72 {
73 throw new FormatException(ex.Message, ex); // No further checks necessary
74 }
75 lastException = ex;
76 }
77 try
78 {
79 Cell.ResolveCellRange(expression);
80 isRange = true;
81 }
82 catch (Exception ex)
83 {
84 if (scope == Cell.AddressScope.Range)
85 {
86 throw new FormatException(ex.Message, ex); // No further checks necessary
87 }
88 lastException = ex;
89 }
90 if (scope == Cell.AddressScope.Range && isCellAddress && isRange)
91 {
92 System.FormatException innerException = new System.FormatException("The expression (" + expression + ") is a single cell address, but a cell range was expected");
93 throw new FormatException(innerException.Message, innerException);
94 }
95 else if (scope == Cell.AddressScope.Any && !isCellAddress && !isRange)
96 {
97 throw new FormatException(lastException.Message, lastException); // Not a cell or range
98 }
99 else if (scope == Cell.AddressScope.Invalid && (isCellAddress || isRange))
100 {
101 throw new FormatException("The passed expression is valid cell address or range, but the validation was explicitly inverted");
102 }
103 }
104
110 public static void ValidateWorksheetName(string name)
111 {
112 if (string.IsNullOrEmpty(name))
113 {
114 throw new FormatException("the worksheet name must be between 1 and " + Worksheet.MaxWorksheetNameLength + " characters");
115 }
116 if (name.Length > Worksheet.MaxWorksheetNameLength)
117 {
118 throw new FormatException("the worksheet name must be between 1 and " + Worksheet.MaxWorksheetNameLength + " characters");
119 }
120 Regex regex = new Regex(@"[\‍[\‍]\*\?/\\‍]");
121 Match match = regex.Match(name);
122 if (match.Captures.Count > 0)
123 {
124 throw new FormatException(@"the worksheet name must not contain the characters [ ] * ? / \ ");
125 }
126 }
127
135 private static string ValidateColorInternal(string hexCode, bool useAlpha, bool allowEmpty)
136 {
137 if (string.IsNullOrEmpty(hexCode))
138 {
139 if (allowEmpty)
140 {
141 return null;
142 }
143 return "The color expression cannot be null or empty";
144 }
145
146 int length = useAlpha ? 8 : 6;
147 if (hexCode.Length != length)
148 {
149 return "The value '" + hexCode + "' is invalid. A valid value must contain " + length + " hex characters";
150 }
151 if (!Regex.IsMatch(hexCode, "[a-fA-F0-9]{6,8}"))
152 {
153 return "The expression '" + hexCode + "' is not a valid hex value";
154 }
155 return null;
156 }
157 }
158}
Class representing a cell of a worksheet.
Definition Cell.cs:25
static Address ResolveCellCoordinate(string address)
Gets the column and row number (zero based) of a cell by the address.
Definition Cell.cs:879
static Range ResolveCellRange(string range)
Resolves a cell range from the format like A1:B3 or AAD556:AAD1000.
Definition Cell.cs:970
AddressScope
Enum to define the scope of a passed address string (used in static context).
Definition Cell.cs:82
Class for exceptions regarding format error incidents.
Class for exceptions regarding Style incidents.
Class providing general validator methods.
Definition Validators.cs:13
static void ValidateWorksheetName(string name)
Validates the passed string, whether it is an expression that can be used as worksheet name.
static void ValidateCellAddressExpression(string expression, Cell.AddressScope scope=Cell.AddressScope.Any)
Validates the passed string, whether it is a valid single cell address or cell range....
Definition Validators.cs:59
static void ValidateGenericColor(string hexCode, bool allowEmpty=false)
Validates the passed string, whether it is a valid RGB or ARGB value that can be used for Fills,...
Definition Validators.cs:22
static void ValidateColor(string hexCode, bool useAlpha, bool allowEmpty=false)
Validates the passed string, whether it is a valid RGB or ARGB value that can be used for Fills,...
Definition Validators.cs:43
Class representing a worksheet of a workbook.
Definition Worksheet.cs:27
static readonly int MaxWorksheetNameLength
Maximum number of characters a worksheet name can have.
Definition Worksheet.cs:37