This repository was archived by the owner on Mar 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataValidationActions.cs
262 lines (209 loc) · 12.3 KB
/
DataValidationActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using DevExpress.Spreadsheet;
using System.Drawing;
using System.Linq;
namespace SpreadsheetControl_API_Part03
{
public static class DataValidationActions {
static void AddDataValidation(IWorkbook workbook) {
#region #AddDataValidation
// Load a document from a file.
workbook.LoadDocument("Documents\\DataValidation.xlsx");
// Access a worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Specify the "C1" cell value.
worksheet["C1"].SetValue(DateTime.Now);
worksheet["C1"].NumberFormat = "mmm/d/yyyy h:mm";
// Restrict data entry to a whole number from 10 to 20.
worksheet.DataValidations.Add(worksheet["B1"],
DataValidationType.WholeNumber, DataValidationOperator.Between, 10, 20);
// Restrict data entry to a number within limits.
DataValidation validation = worksheet.DataValidations.Add(worksheet["F4:F11"], DataValidationType.Decimal, DataValidationOperator.Between, 10, 40);
// Restrict data entry based on a formula.
worksheet.DataValidations.Add(worksheet["B4:B11"], DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)");
// Restrict data entry to 3 symbols.
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
// Restrict data entry to values in a drop-down list specified in code.
// Note that the list in code uses comma to separate entries,
// but the list in UI displays a culture-specific list separator.
worksheet.DataValidations.Add(worksheet["A4:A11"], DataValidationType.List, "PASS, FAIL");
// Restrict data entry to values in a drop-down list obtained from a worksheet.
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));
// Restrict data entry to a time before the specified time.
worksheet.DataValidations.Add(worksheet["C1"], DataValidationType.Time, DataValidationOperator.LessThanOrEqual, DateTime.Now);
// Highlight data validation ranges.
worksheet["H4:H9"].FillColor = Color.LightGray;
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3, 0xFFDFC4, 0xFFDAE9};
for (int i = 0; i < worksheet.DataValidations.Count; i++){
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
}
#endregion #AddDataValidation
}
static void ChangeCriteria(IWorkbook workbook) {
#region #ChangeCriteria
// Load a document from a file.
workbook.LoadDocument("Documents\\DataValidation.xlsx");
// Access a worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Restrict data entry to a number within limits.
DataValidation validation = worksheet.DataValidations.Add
(worksheet["F4:F11"], DataValidationType.Decimal, DataValidationOperator.Between, 10, 40);
// Change the validation operator and criteria.
// The "F4:F11" range's cell values are valid
// if they are greater than or equal 20.
validation.Operator = DataValidationOperator.GreaterThanOrEqual;
validation.Criteria = 20;
validation.Criteria2 = ValueObject.Empty;
// Highlight data validation ranges.
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
for (int i = 0; i < worksheet.DataValidations.Count; i++)
{
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
}
#endregion #ChangeCriteria
}
static void UseUnionRange(IWorkbook workbook) {
#region #UseUnionRange
// Load a document from a file.
workbook.LoadDocument("Documents\\DataValidation.xlsx");
// Access a worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Create a union range.
CellRange range = worksheet.Range.Union(worksheet["F4:F5"], worksheet["F6:F11"]);
// Restrict data entry to a number within limits.
worksheet.DataValidations.Add(range, DataValidationType.Decimal, DataValidationOperator.Between, 10, 40);
// Highlight data validation ranges.
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
for (int i = 0; i < worksheet.DataValidations.Count; i++)
{
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
}
#endregion #UseUnionRange
}
static void ShowInputMessage(IWorkbook workbook) {
#region #ShowInputMessage
// Load a document from a file.
workbook.LoadDocument("Documents\\DataValidation.xlsx");
// Access a worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Restrict data entry to a 5-digit number.
DataValidation validation = worksheet.DataValidations.Add(worksheet["B4:B11"], DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)");
// Show input message.
validation.InputTitle = "Employee Id";
validation.InputMessage = "Please enter 5-digit number";
validation.ShowInputMessage = true;
// Highlight data validation ranges.
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
for (int i = 0; i < worksheet.DataValidations.Count; i++)
{
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
}
#endregion #ShowInputMessage
}
static void ShowErrorMessage(IWorkbook workbook) {
#region #ShowErrorMessage
// Load a document from a file.
workbook.LoadDocument("Documents\\DataValidation.xlsx");
// Access a worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Restrict data entry to a 5-digit number.
DataValidation validation = worksheet.DataValidations.Add(worksheet["B4:B11"], DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)");
// Show error message.
validation.ErrorTitle = "Wrong Employee Id";
validation.ErrorMessage = "The value you entered is not valid. Use 5-digit number for the employee ID.";
validation.ErrorStyle = DataValidationErrorStyle.Information;
validation.ShowErrorMessage = true;
// Highlight data validation ranges.
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
for (int i = 0; i < worksheet.DataValidations.Count; i++)
{
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
}
#endregion #ShowErrorMessage
}
static void GetDataValidation(IWorkbook workbook)
{
#region #GetDataValidation
// Load a document from a file.
workbook.LoadDocument("Documents\\DataValidation.xlsx");
// Access a worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Add data validations.
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));
// Get data validation entry associated with the "E4" cell.
worksheet.DataValidations.GetDataValidation(worksheet.Cells["E4"]).Criteria = ValueObject.FromRange(worksheet["H4:H5"]);
// Get data validation entries for the "D4:E11" range.
var myValidation = worksheet.DataValidations.GetDataValidations(worksheet["D4:E11"])
.Where(d => d.ValidationType == DataValidationType.TextLength).SingleOrDefault();
if (myValidation != null) myValidation.Criteria = 4;
// Get data validation entries that meet a specific criteria.
foreach (var d in worksheet.DataValidations.GetDataValidations(DataValidationType.TextLength, DataValidationOperator.Equal, 4, ValueObject.Empty))
{
// Change the validation operator.
// The "D4:D11" range's cell values are valid
// if they contain text with more than 4 characters.
d.Operator = DataValidationOperator.GreaterThan;
}
// Highlight data validation ranges.
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
for (int i = 0; i < worksheet.DataValidations.Count; i++)
{
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
}
#endregion #GetDataValidation
}
static void ValidateCellValue(IWorkbook workbook)
{
#region #ValidateCellValue
// Load a document from a file.
workbook.LoadDocument("Documents\\DataValidation.xlsx");
// Access a worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Add data validations.
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
// Check whether the cell value meets a validation criteria.
bool isValid = worksheet.DataValidations.Validate(worksheet.Cells["D4"], worksheet.Cells["J4"].Value);
if (isValid) { worksheet["D4"].CopyFrom(worksheet["J4"]); }
#endregion #ValidateCellValue
}
static void RemoveDataValidation(IWorkbook workbook) {
#region #RemoveDataValidation
// Load a document from a file.
workbook.LoadDocument("Documents\\DataValidation.xlsx");
// Access a worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Add data validations.
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));
// Remove a data validation by its index.
worksheet.DataValidations.RemoveAt(1);
// Highlight data validation ranges.
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
for (int i = 0; i < worksheet.DataValidations.Count; i++)
{
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
}
#endregion #RemoveDataValidation
}
static void RemoveAllDataValidations(IWorkbook workbook) {
#region #RemoveAllDataValidations
// Load a document from a file.
workbook.LoadDocument("Documents\\DataValidation.xlsx");
// Access a worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Add data validations.
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));
// Remove all data validations.
worksheet.DataValidations.Clear();
// Highlight data validation ranges.
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
for (int i = 0; i < worksheet.DataValidations.Count; i++)
{
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
}
#endregion #RemoveAllDataValidations
}
}
}