セルの値がデータ検証ルールを満たしているかを確認
Microsoft Excelは、セルにデータ検証ルールを追加できるようにしています。例えば、少数点の検証は10から20の間の数値のみを入力可能にします。異なる数字を入力した場合、Excelはエラーメッセージを表示し、正しい範囲の数字を入力するよう促します。数字をコピー&ペーストした場合、検証は行われずエラーメッセージも表示されません。
時には、プログラムでセルに適用されたデータ検証ルールが値を満たしているかどうかを検証する必要があります。たとえば、上記の場合、エントリが失敗する必要があります。
紹介
Aspose.Cells for Node.js via C++は、Cell.getValidationValue()メソッドを提供し、プログラムmaticallyセルの値を検証します。セルの値が適用されたデータ検証ルールを満たさない場合はfalseを返し、満たす場合はtrueを返します。
以下のサンプルコードは、Cell.getValidationValue()メソッドの動作例を示しています。まず、C1に値3を入力します。この値はデータ検証ルールを満たさないため、メソッドはfalseを返します。その後、値15をC1に入力すると、この値はルールを満たすため、メソッドはtrueを返します。同様に、値30ではfalseとなります。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiate the workbook from sample Excel file | |
const workbook = new AsposeCells.Workbook(dataDir + "sample.xlsx"); | |
// Access the first worksheet | |
const 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 | |
const 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 | |
console.log("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 | |
console.log("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 | |
console.log("Is 30 a Valid Value for this Cell: " + cell.getValidationValue()); |
出力
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