获取应用于单元格的验证
Contents
[
Hide
]
您可以使用Aspose.Cells API获取应用于任何单元格的验证。Aspose.Cells为此目的提供了Cell.getValidation()方法。如果单元格上没有验证,它将返回null。同样,您可以通过提供其行和列索引使用Worksheet.getValidations().getValidationInCell(int row, int column)方法获取应用于单元格的验证。
以下快照显示了示例代码中使用的示例Microsoft Excel文件。单元格C1应用了十进制验证,并且只能接受10到20之间的值。
具有验证的单元格
以下示例代码获取应用于 C1 的验证并读取其各种属性。
This file contains 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(GetValidationAppliedonCell.class); | |
// Instantiate the workbook from sample Excel file | |
Workbook workbook = new Workbook(dataDir + "book1.xlsx"); | |
// Access its first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Cell C1 has the Decimal Validation applied on it. | |
// It can take only the values Between 10 and 20 | |
Cell cell = worksheet.getCells().get("C1"); | |
// Access the valditation applied on this cell | |
Validation validation = cell.getValidation(); | |
// Read various properties of the validation | |
System.out.println("Reading Properties of Validation"); | |
System.out.println("--------------------------------"); | |
System.out.println("Type: " + validation.getType()); | |
System.out.println("Operator: " + validation.getOperator()); | |
System.out.println("Formula1: " + validation.getFormula1()); | |
System.out.println("Formula2: " + validation.getFormula2()); | |
System.out.println("Ignore blank: " + validation.getIgnoreBlank()); |
以下是在上述快照中显示的示例文件中执行示例代码产生的控制台输出。
Reading Properties of Validation
\--------------------------------
Type: 2
Operator: 0
Formula1: =10
Formula2: =20
Ignore blank: true