Verificare che il valore Cell soddisfi le regole di convalida dei dati

Verificare che il valore Cell soddisfi le regole di convalida dei dati

A volte è necessario verificare dinamicamente se un determinato valore soddisfa le regole di convalida dei dati applicate alla cella. A tale scopo, le API Aspose.Cells forniscono ilcell.getValidationValue metodo. Se il valore in una cella non soddisfa la regola di convalida dei dati applicata a quella cella, restituisceFalso , altroVero.

Il seguente file Excel di esempio Microsoft viene utilizzato con il codice di esempio riportato di seguito per testare il filecell.getValidationValue metodo. Come puoi vedere nell’istantanea che le celleC1 haconvalida dei dati decimali applicato e accetterà solo valoritra le 10 e le 20 . Ogni volta che il valore della cella è compreso tra 10 e 20,cell.getValidationValue verrà restituitoVero , altrimenti torneràFalso.

cose da fare:immagine_alt_testo

Il codice di esempio seguente illustra come ilcell.getValidationValue il metodo funziona. Innanzitutto, inserisce il valore 3 in C1. Poiché ciò non soddisfa la regola di convalida dei dati, il filecell.getValidationValue restituisce il metodoFalso . Quindi, inserisce il valore 15 in C1. Poiché questo valore soddisfa la regola di convalida dei dati, ilcell.getValidationValue restituisce il metodoVero . Allo stesso modo, ritornaFalso per valore 30.

Java codice per verificare se un valore Cell 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 dal codice di esempio

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