Stellen Sie sicher, dass der Wert Cell die Datenvalidierungsregeln erfüllt
Stellen Sie sicher, dass der Wert Cell die Datenvalidierungsregeln erfüllt
Manchmal ist es erforderlich, dynamisch zu überprüfen, ob ein bestimmter Wert die auf die Zelle angewendeten Datenvalidierungsregeln erfüllt. Hierfür stellen die Aspose.Cells APIs diecell.getValidationValue Methode. Wenn der Wert in einer Zelle die auf diese Zelle angewendete Datenvalidierungsregel nicht erfüllt, wird er zurückgegebenFALSCH , andersWahr.
Die folgende Excel-Beispieldatei Microsoft wird mit dem folgenden Beispielcode verwendet, um diecell.getValidationValue Methode. Wie Sie im Schnappschuss sehen können, sind die ZellenC1 hatdezimale Datenvalidierung angewendet und akzeptiert nur Wertezwischen 10 und 20 . Immer wenn der Wert der Zelle zwischen 10 und 20 liegt,cell.getValidationValue Methode zurückWahr , andernfalls kehrt es zurückFALSCH.
Der folgende Beispielcode veranschaulicht, wie diecell.getValidationValue Methode funktioniert. Zuerst trägt er den Wert 3 in C1 ein. Da dies die Datenvalidierungsregel nicht erfüllt, wird diecell.getValidationValue Methode gibt zurückFALSCH . Dann trägt er den Wert 15 in C1 ein. Da dieser Wert die Datenvalidierungsregel erfüllt, wird diecell.getValidationValue Methode gibt zurückWahr . Ebenso kehrt es zurückFALSCH für Wert 30.
Java-Code, um zu überprüfen, ob ein Cell-Wert die Datenvalidierungsregeln erfüllt
// 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()); | |
Vom Beispielcode generierte Konsolenausgabe
Hier ist die Konsolenausgabe, die generiert wird, wenn der Beispielcode mit der oben gezeigten Beispiel-Excel-Datei 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