在工作表中使用验证
Contents
[
Hide
]
Aspose.Cells.GridDesktop 还支持向工作表的单元格添加验证(或验证规则)。通过对单元格应用验证规则,开发人员可以限制用户以特定格式向 Grid 中输入数据。 Aspose.Cells.GridDesktop 支持不同的验证模式。在本主题中,我们不仅会讨论这些验证模式,还会解释这些验证的操作。
验证模式
Aspose.Cells.GridDesktop支持三种验证模式如下:
- 是否需要验证模式
- 正则表达式验证模式
- 自定义验证模式
是否需要验证模式
在此验证模式下,用户只能在指定的单元格中输入值。一次是否需要验证应用于工作表单元格时,用户必须在该单元格中输入值。
正则表达式验证模式
在这种模式下,对工作表单元格施加限制,以便用户以特定格式将数据提交到单元格中。数据格式的模式以一种形式提供正则表达式.
自定义验证模式
使用自定义验证 开发者必须实现Aspose.Cells.GridDesktop.ICustomValidation接口。该接口提供了一个证实方法。如果数据有效,则此方法返回 true,否则返回 false。
在 Aspose.Cells.GridDesktop 中使用验证
添加验证
要向工作表单元格添加任何类型的验证,请按照以下步骤操作:
- 将 Aspose.Cells.GridDesktop 控件添加到您的形式
- 访问任何想要的工作表
- 将所需的验证添加到验证的集合工作表指定哪个验证将应用于哪个单元格。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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单元格,但我们还没有实现自定义验证。正如我们在本主题开头所解释的那样,要应用自定义验证,我们必须实现自定义验证界面。那么,让我们尝试创建一个类来实现自定义验证界面。
在下面给出的代码片段中,我们实现了自定义验证来执行以下检查:
- 检查添加验证的单元格地址是否准确
- 检查单元格值的数据类型是否为双精度
- 检查单元格的值是否大于100
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 使开发人员可以轻松完成此任务。
要访问特定验证,请按照以下步骤操作:
- 访问一个想要的工作表
- 访问特定的验证在工作表中指定应用验证的单元格名称
- 编辑验证属性,如果需要
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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."); | |
} |
验证集合有两个索引器。一个索引器(在下面的示例中使用)允许访问一个验证对象通过将单元格名称作为其索引,而另一个索引器使用两个参数(即行号和列号)来执行相同的任务。
删除验证
要从工作表中删除特定验证,请按照以下步骤操作:
- 访问一个想要的工作表
- 删除特定的验证来自工作表通过指定应用验证的单元格名称
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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."); | |
} |