セルの値がデータ検証ルールを満たしているかを確認
Contents
[
Hide
]
Microsoft Excelを使用すると、ワークシートのセルにデータ検証ルールを追加できます。たとえば、10から20の数値を制限するために10進数の検証を適用できます。指定された範囲外の数値を入力すると、Microsoft Excelはエラーメッセージを表示し、正しい範囲から数値を再入力するようユーザーに促します。ユーザーが数値をコピーして貼り付けると、たとえば3といった数値を入力すると、Excelは検証チェックを実行せず、エラーメッセージを表示しません。
データ検証ルールを満たしているか確認
時々、セルに適用されたデータ検証ルールを動的に確認することが必要です。この目的のために、Aspose.Cells APIはcell.getValidationValue()メソッドを提供します。セルの値がそのセルに適用されたデータ検証ルールを満たさない場合、Falseを返し、それ以外の場合はTrueを返します。
以下のサンプルMicrosoft Excelファイルは、下記のサンプルコードでcell.getValidationValue()メソッドをテストするために使用されます。スナップショットでわかるように、セル** C1 **に** 10から20の間 **の値しか受け付けない**10進数のデータ検証**が適用されています。セルの値が10から20の間にある場合、cell.getValidationValue()メソッドは**True**を返し、それ以外の場合は**False**を返します。
以下のサンプルコードは、cell.getValidationValue()メソッドの動作を示しています。まず、C1に値3を入力します。これはデータ検証ルールを満たさないため、cell.getValidationValue()メソッドはFalseを返します。その後、C1に値15を入力します。この値はデータ検証ルールを満たすため、cell.getValidationValue()メソッドはTrueを返します。同様に、値30に対してはFalseを返します。
**Javaコード:セルの値がデータ検証ルールを満たしているか確認
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(VerifyCellValueSatisfiesDataValidationRules.class); | |
// Instantiate the workbook from sample Excel file | |
Workbook workbook = new Workbook(dataDir + "Sample1.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
/* | |
* Access Cell C1. 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"); | |
// Enter 3 inside this cell. Since it is not between 10 and 20, it should fail the validation | |
cell.putValue(3); | |
// Check if number 3 satisfies the Data Validation rule applied on this cell | |
System.out.println("Is 3 a Valid Value for this Cell: " + cell.getValidationValue()); | |
// Enter 15 inside this cell. Since it is between 10 and 20, it should succeed the validation | |
cell.putValue(15); | |
// Check if number 15 satisfies the Data Validation rule applied on this cell | |
System.out.println("Is 15 a Valid Value for this Cell: " + cell.getValidationValue()); | |
// Enter 30 inside this cell. Since it is not between 10 and 20, it should fail the validation again | |
cell.putValue(30); | |
// Check if number 30 satisfies the Data Validation rule applied on this cell | |
System.out.println("Is 30 a Valid Value for this Cell: " + cell.getValidationValue()); | |
サンプルコードによって生成されたコンソール出力
以下は、上記で示されているサンプルExcelファイルを使用してサンプルコードを実行した際に生成されたコンソール出力です。
Is 3 a Valid Value for this Cell: False
Is 15 a Valid Value for this Cell: True
Is 30 a Valid Value for this Cell: False