Cell 値がデータ検証ルールを満たしていることを確認する
Cell 値がデータ検証ルールを満たしていることを確認する
場合によっては、特定の値がセルに適用されるデータ検証ルールを満たしているかどうかを動的に検証する必要があります。この目的のために、Aspose.Cells API は[cell.getValidationValue](https://reference.aspose.com/cells/java/com.aspose.cells/cell#getValidationValue() ) 方法。セルの値が、そのセルに適用されたデータ検証規則を満たさない場合は、戻り値が返されます。間違い、 それ以外真実.
次のサンプル Microsoft Excel ファイルは、以下のサンプル コードで使用され、[cell.getValidationValue](https://reference.aspose.com/cells/java/com.aspose.cells/cell#getValidationValue() ) 方法。スナップショットでわかるように、セルはC1もっている小数データの検証適用され、値のみを受け入れます10から20の間.セルの値が 10 から 20 の間にあるときはいつでも、cell.getValidationValue メソッドが返されます真実、それ以外の場合は戻ります間違い.
次のサンプル コードは、[cell.getValidationValue](https://reference.aspose.com/cells/java/com.aspose.cells/cell#getValidationValue() )メソッドが機能します。まず、値 3 を C1 に入力します。これはデータ検証規則を満たさないため、cell.getValidationValue メソッドが返す間違い.次に、値 15 を C1 に入力します。この値はデータ検証ルールを満たしているため、cell.getValidationValue メソッドが返す真実 .同様に、それは戻ります間違い値 30 の場合。
Cell 値がデータ検証ルールを満たしているかどうかを検証するための Java コード
// 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