在工作表中处理验证
验证模式
Aspose.Cells.GridDesktop 支持三种验证模式,如下:
- 必填验证模式
- 正则表达式验证模式
- 自定义验证模式
必填验证模式
在此验证模式下,用户受到限制,无法向指定单元格输入值。一旦在工作表单元格上应用了必填验证,用户就必须向该单元格输入值。
正则表达式验证模式
在此模式下,对工作表单元格施加了限制,要求用户以特定格式向单元格提交数据。数据格式的模式以正则表达式提供。
自定义验证模式
要使用自定义验证,开发人员必须实现 Aspose.Cells.GridDesktop.ICustomValidation 接口。该接口提供一个验证方法。此方法在数据有效时返回true,否则返回false。
在 Aspose.Cells.GridDesktop 中处理验证
添加验证
要向工作表单元格添加任何类型的验证,请按照下面的步骤操作:
- 向您的表单中添加Aspose.Cells.GridDesktop控件
- 访问任何所需的工作表
- 向工作表的验证集合中添加所需的验证,以指定要在哪个单元格上应用哪个验证
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing first worksheet of the Grid | |
Worksheet sheet = gridDesktop1.Worksheets[0]; | |
// Adding values to specific cells of the worksheet | |
sheet.Cells["a2"].Value = "Required"; | |
sheet.Cells["a4"].Value = "100"; | |
sheet.Cells["a6"].Value = "2006-07-21"; | |
sheet.Cells["a8"].Value = "101.2"; | |
// Adding Is Required Validation to a cell | |
sheet.Validations.Add("a2", true, ""); | |
// Adding simple Regular Expression Validation to a cell | |
sheet.Validations.Add("a4", true, @"\d+"); | |
// Adding complex Regular Expression Validation to a cell | |
sheet.Validations.Add("a6", true, @"\d{4}-\d{2}-\d{2}"); | |
// Adding Custom Validation to a cell | |
sheet.Validations.Add("a8", new CustomValidation()); |
实现ICustomValidation
在上面的代码片段中,我们已经在A8单元格中添加了自定义验证,但我们尚未实现该自定义验证。正如我们在本主题开始时所解释的,要应用自定义验证,我们必须实现ICustomValidation接口。那么,让我们尝试创建一个类来实现ICustomValidation接口。
在下面给出的代码片段中,我们实现了自定义验证以执行以下检查:
- 检查添加验证的单元格地址是否准确
- 检查单元格值的数据类型是否为双精度
- 检查单元格的值是否大于100
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Implementing ICustomValidation interface | |
public class CustomValidation : Aspose.Cells.GridDesktop.ICustomValidation | |
{ | |
// Implementing Validate method already defined in the interface | |
public bool Validate(Worksheet worksheet, int row, int col, object value) | |
{ | |
// Checking the cell's address | |
if (row == 7 && col == 0) | |
{ | |
//Checking the data type of cell's value | |
double d = 0; | |
try | |
{ | |
d = (double)value; | |
} | |
catch | |
{ | |
return false; | |
} | |
// Checking if the cell's value is greater than 100 | |
if (d > 100) | |
return true; | |
} | |
return false; | |
} | |
} |
访问验证
一旦将验证添加到特定工作表单元格,开发人员可能需要在运行时访问和修改特定验证的属性。因此,Aspose.Cells.GridDesktop已经简化了开发人员完成此任务的流程。
要访问特定的验证,请按照以下步骤操作:
- 访问所需的工作表
- 通过指定应用了验证的单元格名称在工作表中访问特定的验证
- 编辑验证属性(如果需要)
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing first worksheet of the Grid | |
Worksheet sheet = gridDesktop1.Worksheets[0]; | |
if (sheet.Validations.Count > 0) | |
{ | |
// Accessing the Validation object applied on "a8" cell | |
Aspose.Cells.GridDesktop.Data.GridValidation validation = sheet.Validations[7, 0]; | |
// Editing the attributes of Validation | |
validation.IsRequired = true; | |
validation.RegEx = ""; | |
validation.CustomValidation = null; | |
MessageBox.Show("Validation has been edited after accessing it."); | |
} | |
else | |
{ | |
MessageBox.Show("No validations found to access."); | |
} |
移除验证
要从工作表中移除特定的验证,请按照以下步骤操作:
- 访问所需的工作表
- 通过指定应用验证的单元格名称,从工作表中删除特定的验证
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing first worksheet of the Grid | |
Worksheet sheet = gridDesktop1.Worksheets[0]; | |
if (sheet.Validations.Count > 0) | |
{ | |
// Removing the Validation object applied on "a6" cell | |
sheet.Validations.RemoveAt(1); | |
MessageBox.Show("Validation has been removed."); | |
} | |
else | |
{ | |
MessageBox.Show("No validations found to remove."); | |
} |