Verificare che il valore della cella soddisfi le regole di convalida dei dati
Verifica che il valore della cella soddisfi le regole di convalida dei dati
A volte è necessario verificare dinamicamente se un dato valore soddisfa le regole di convalida dei dati applicate alla cella. A questo scopo, le API di Aspose.Cells forniscono il metodo cell.getValidationValue(). Se il valore in una cella non soddisfa la regola di convalida dei dati applicata a quella cella, restituisce Falso, altrimenti Vero.
Il file di esempio di seguito di Microsoft Excel viene usato con il codice di esempio sottostante per testare il metodo cell.getValidationValue(). Come puoi vedere nello snapshot, le celle C1 hanno la convalida dei dati decimali applicata e accetteranno solo valori tra 10 e 20. Ogni volta che il valore della cella è compreso tra 10 e 20, il metodo cell.getValidationValue() restituirà Vero, altrimenti restituirà Falso.
Il seguente esempio di codice illustra come funziona il metodo cell.getValidationValue(). Per prima cosa, immette il valore 3 in C1. Poiché ciò non soddisfa la regola di convalida dei dati, il metodo cell.getValidationValue() restituisce Falso. Poi, immette il valore 15 in C1. Poiché questo valore soddisfa la regola di convalida dei dati, il metodo cell.getValidationValue() restituisce Vero. Allo stesso modo, restituisce Falso per il valore 30.
Codice Java per verificare se un valore della cella soddisfa le regole di convalida dei dati
// 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()); | |
Output della console generato dall’esempio di codice
Ecco l’output della console generato quando il codice di esempio viene eseguito con il file Excel di esempio mostrato sopra.
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