Überprüfen Sie, ob der Zellenwert den Datenvalidierungsregeln entspricht

Überprüfen Sie, ob der Zellenwert den Datenvalidierungsregeln entspricht

Manchmal ist es erforderlich, dynamisch zu überprüfen, ob ein bestimmter Wert den für die Zelle geltenden Datenvalidierungsregeln entspricht. Hierfür bieten die Aspose.Cells-APIs die Methode cell.getValidationValue(). Wenn der Wert in einer Zelle den für diese Zelle geltenden Datenvalidierungsregeln nicht entspricht, gibt die Methode False zurück, andernfalls True.

Die folgende Beispielmicrosoft-excel-Datei wird mit dem unten stehenden Beispielcode verwendet, um die Methode cell.getValidationValue() zu testen. Wie in der Momentaufnahme zu sehen ist, hat die Zelle C1 eine dezimale Datenvalidierung und akzeptiert nur Werte zwischen 10 und 20. Wann immer der Wert der Zelle zwischen 10 und 20 liegt, gibt die Methode cell.getValidationValue() True zurück, ansonsten gibt sie False zurück.

todo:image_alt_text

Der folgende Beispielcode veranschaulicht, wie die Methode cell.getValidationValue() funktioniert. Zunächst gibt er den Wert 3 in C1 ein. Da dies den Datenvalidierungsregeln nicht entspricht, gibt die Methode cell.getValidationValue() False zurück. Dann gibt er den Wert 15 in C1 ein. Da dieser Wert den Datenvalidierungsregeln entspricht, gibt die Methode cell.getValidationValue() True zurück. Ebenso gibt sie für den Wert 30 ebenfalls False zurück.

Java-Code zum Überprüfen, ob der Zellenwert den Datenvalidierungsregeln entspricht

// 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());

Von dem Beispielcode generierte Konsolenausgabe

Hier ist die von der Beispielmethode generierte Konsolenausgabe, wenn der Beispielcode mit der obigen Beispieldatei ausgeführt wird.

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