1 | /*******************************************************************************
|
---|
2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
3 | *
|
---|
4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2011 Jan Källman
|
---|
8 | *
|
---|
9 | * This library is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU Lesser General Public
|
---|
11 | * License as published by the Free Software Foundation; either
|
---|
12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | * This library is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | * See the GNU Lesser General Public License for more details.
|
---|
18 | *
|
---|
19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
21 | *
|
---|
22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
24 | *
|
---|
25 | * Code change notes:
|
---|
26 | *
|
---|
27 | * Author Change Date
|
---|
28 | * ******************************************************************************
|
---|
29 | * Mats Alm Added 2011-01-01
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-27
|
---|
31 | * Raziq York Added support for Any type 2014-08-08
|
---|
32 | *******************************************************************************/
|
---|
33 | using System;
|
---|
34 | using System.Collections.Generic;
|
---|
35 | using System.Linq;
|
---|
36 | using System.Text;
|
---|
37 |
|
---|
38 | namespace OfficeOpenXml.DataValidation
|
---|
39 | {
|
---|
40 | /// <summary>
|
---|
41 | /// Enum for available data validation types
|
---|
42 | /// </summary>
|
---|
43 | public enum eDataValidationType
|
---|
44 | {
|
---|
45 | /// <summary>
|
---|
46 | /// Any value
|
---|
47 | /// </summary>
|
---|
48 | Any,
|
---|
49 | /// <summary>
|
---|
50 | /// Integer value
|
---|
51 | /// </summary>
|
---|
52 | Whole,
|
---|
53 | /// <summary>
|
---|
54 | /// Decimal values
|
---|
55 | /// </summary>
|
---|
56 | Decimal,
|
---|
57 | /// <summary>
|
---|
58 | /// List of values
|
---|
59 | /// </summary>
|
---|
60 | List,
|
---|
61 | /// <summary>
|
---|
62 | /// Text length validation
|
---|
63 | /// </summary>
|
---|
64 | TextLength,
|
---|
65 | /// <summary>
|
---|
66 | /// DateTime validation
|
---|
67 | /// </summary>
|
---|
68 | DateTime,
|
---|
69 | /// <summary>
|
---|
70 | /// Time validation
|
---|
71 | /// </summary>
|
---|
72 | Time,
|
---|
73 | /// <summary>
|
---|
74 | /// Custom validation
|
---|
75 | /// </summary>
|
---|
76 | Custom
|
---|
77 | }
|
---|
78 |
|
---|
79 | internal static class DataValidationSchemaNames
|
---|
80 | {
|
---|
81 | public const string Any = "";
|
---|
82 | public const string Whole = "whole";
|
---|
83 | public const string Decimal = "decimal";
|
---|
84 | public const string List = "list";
|
---|
85 | public const string TextLength = "textLength";
|
---|
86 | public const string Date = "date";
|
---|
87 | public const string Time = "time";
|
---|
88 | public const string Custom = "custom";
|
---|
89 | }
|
---|
90 |
|
---|
91 | /// <summary>
|
---|
92 | /// Types of datavalidation
|
---|
93 | /// </summary>
|
---|
94 | public class ExcelDataValidationType
|
---|
95 | {
|
---|
96 | private ExcelDataValidationType(eDataValidationType validationType, bool allowOperator, string schemaName)
|
---|
97 | {
|
---|
98 | Type = validationType;
|
---|
99 | AllowOperator = allowOperator;
|
---|
100 | SchemaName = schemaName;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /// <summary>
|
---|
104 | /// Validation type
|
---|
105 | /// </summary>
|
---|
106 | public eDataValidationType Type
|
---|
107 | {
|
---|
108 | get;
|
---|
109 | private set;
|
---|
110 | }
|
---|
111 |
|
---|
112 | internal string SchemaName
|
---|
113 | {
|
---|
114 | get;
|
---|
115 | private set;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /// <summary>
|
---|
119 | /// This type allows operator to be set
|
---|
120 | /// </summary>
|
---|
121 | internal bool AllowOperator
|
---|
122 | {
|
---|
123 |
|
---|
124 | get;
|
---|
125 | private set;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /// <summary>
|
---|
129 | /// Returns a validation type by <see cref="eDataValidationType"/>
|
---|
130 | /// </summary>
|
---|
131 | /// <param name="type"></param>
|
---|
132 | /// <returns></returns>
|
---|
133 | internal static ExcelDataValidationType GetByValidationType(eDataValidationType type)
|
---|
134 | {
|
---|
135 | switch (type)
|
---|
136 | {
|
---|
137 | case eDataValidationType.Any:
|
---|
138 | return ExcelDataValidationType.Any;
|
---|
139 | case eDataValidationType.Whole:
|
---|
140 | return ExcelDataValidationType.Whole;
|
---|
141 | case eDataValidationType.List:
|
---|
142 | return ExcelDataValidationType.List;
|
---|
143 | case eDataValidationType.Decimal:
|
---|
144 | return ExcelDataValidationType.Decimal;
|
---|
145 | case eDataValidationType.TextLength:
|
---|
146 | return ExcelDataValidationType.TextLength;
|
---|
147 | case eDataValidationType.DateTime:
|
---|
148 | return ExcelDataValidationType.DateTime;
|
---|
149 | case eDataValidationType.Time:
|
---|
150 | return ExcelDataValidationType.Time;
|
---|
151 | case eDataValidationType.Custom:
|
---|
152 | return ExcelDataValidationType.Custom;
|
---|
153 | default:
|
---|
154 | throw new InvalidOperationException("Non supported Validationtype : " + type.ToString());
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | internal static ExcelDataValidationType GetBySchemaName(string schemaName)
|
---|
159 | {
|
---|
160 | switch (schemaName)
|
---|
161 | {
|
---|
162 | case DataValidationSchemaNames.Any:
|
---|
163 | return ExcelDataValidationType.Any;
|
---|
164 | case DataValidationSchemaNames.Whole:
|
---|
165 | return ExcelDataValidationType.Whole;
|
---|
166 | case DataValidationSchemaNames.Decimal:
|
---|
167 | return ExcelDataValidationType.Decimal;
|
---|
168 | case DataValidationSchemaNames.List:
|
---|
169 | return ExcelDataValidationType.List;
|
---|
170 | case DataValidationSchemaNames.TextLength:
|
---|
171 | return ExcelDataValidationType.TextLength;
|
---|
172 | case DataValidationSchemaNames.Date:
|
---|
173 | return ExcelDataValidationType.DateTime;
|
---|
174 | case DataValidationSchemaNames.Time:
|
---|
175 | return ExcelDataValidationType.Time;
|
---|
176 | case DataValidationSchemaNames.Custom:
|
---|
177 | return ExcelDataValidationType.Custom;
|
---|
178 | default:
|
---|
179 | throw new ArgumentException("Invalid schemaname: " + schemaName);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /// <summary>
|
---|
184 | /// Overridden Equals, compares on internal validation type
|
---|
185 | /// </summary>
|
---|
186 | /// <param name="obj"></param>
|
---|
187 | /// <returns></returns>
|
---|
188 | public override bool Equals(object obj)
|
---|
189 | {
|
---|
190 | if (!(obj is ExcelDataValidationType))
|
---|
191 | {
|
---|
192 | return false;
|
---|
193 | }
|
---|
194 | return ((ExcelDataValidationType)obj).Type == Type;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /// <summary>
|
---|
198 | /// Overrides GetHashCode()
|
---|
199 | /// </summary>
|
---|
200 | /// <returns></returns>
|
---|
201 | public override int GetHashCode()
|
---|
202 | {
|
---|
203 | return base.GetHashCode();
|
---|
204 | }
|
---|
205 |
|
---|
206 | /// <summary>
|
---|
207 | /// Integer values
|
---|
208 | /// </summary>
|
---|
209 | private static ExcelDataValidationType _any;
|
---|
210 | public static ExcelDataValidationType Any
|
---|
211 | {
|
---|
212 | get
|
---|
213 | {
|
---|
214 | if (_any == null)
|
---|
215 | {
|
---|
216 | _any = new ExcelDataValidationType(eDataValidationType.Any, false, DataValidationSchemaNames.Any);
|
---|
217 | }
|
---|
218 | return _any;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | /// <summary>
|
---|
223 | /// Integer values
|
---|
224 | /// </summary>
|
---|
225 | private static ExcelDataValidationType _whole;
|
---|
226 | public static ExcelDataValidationType Whole
|
---|
227 | {
|
---|
228 | get
|
---|
229 | {
|
---|
230 | if (_whole == null)
|
---|
231 | {
|
---|
232 | _whole = new ExcelDataValidationType(eDataValidationType.Whole, true, DataValidationSchemaNames.Whole);
|
---|
233 | }
|
---|
234 | return _whole;
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /// <summary>
|
---|
239 | /// List of allowed values
|
---|
240 | /// </summary>
|
---|
241 | private static ExcelDataValidationType _list;
|
---|
242 | public static ExcelDataValidationType List
|
---|
243 | {
|
---|
244 | get
|
---|
245 | {
|
---|
246 | if (_list == null)
|
---|
247 | {
|
---|
248 | _list = new ExcelDataValidationType(eDataValidationType.List, false, DataValidationSchemaNames.List);
|
---|
249 | }
|
---|
250 | return _list;
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | private static ExcelDataValidationType _decimal;
|
---|
255 | public static ExcelDataValidationType Decimal
|
---|
256 | {
|
---|
257 | get
|
---|
258 | {
|
---|
259 | if (_decimal == null)
|
---|
260 | {
|
---|
261 | _decimal = new ExcelDataValidationType(eDataValidationType.Decimal, true, DataValidationSchemaNames.Decimal);
|
---|
262 | }
|
---|
263 | return _decimal;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | private static ExcelDataValidationType _textLength;
|
---|
268 | public static ExcelDataValidationType TextLength
|
---|
269 | {
|
---|
270 | get
|
---|
271 | {
|
---|
272 | if (_textLength == null)
|
---|
273 | {
|
---|
274 | _textLength = new ExcelDataValidationType(eDataValidationType.TextLength, true, DataValidationSchemaNames.TextLength);
|
---|
275 | }
|
---|
276 | return _textLength;
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | private static ExcelDataValidationType _dateTime;
|
---|
281 | public static ExcelDataValidationType DateTime
|
---|
282 | {
|
---|
283 | get
|
---|
284 | {
|
---|
285 | if (_dateTime == null)
|
---|
286 | {
|
---|
287 | _dateTime = new ExcelDataValidationType(eDataValidationType.DateTime, true, DataValidationSchemaNames.Date);
|
---|
288 | }
|
---|
289 | return _dateTime;
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | private static ExcelDataValidationType _time;
|
---|
294 | public static ExcelDataValidationType Time
|
---|
295 | {
|
---|
296 | get
|
---|
297 | {
|
---|
298 | if (_time == null)
|
---|
299 | {
|
---|
300 | _time = new ExcelDataValidationType(eDataValidationType.Time, true, DataValidationSchemaNames.Time);
|
---|
301 | }
|
---|
302 | return _time;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | private static ExcelDataValidationType _custom;
|
---|
307 | public static ExcelDataValidationType Custom
|
---|
308 | {
|
---|
309 | get
|
---|
310 | {
|
---|
311 | if (_custom == null)
|
---|
312 | {
|
---|
313 | _custom = new ExcelDataValidationType(eDataValidationType.Custom, true, DataValidationSchemaNames.Custom);
|
---|
314 | }
|
---|
315 | return _custom;
|
---|
316 | }
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|