التحقق من أن قيمة الخلية تفي بقواعد التحقق من البيانات

التحقق من أن قيمة الخلية تفي بقواعد التحقق من البيانات

في بعض الأحيان، يكون من الضروري التحقق ديناميكيًا مما إذا كانت القيمة المعطاة تفي بقواعد التحقق من البيانات المطبقة على الخلية. لهذا الغرض، توفر واجهة برمجة التطبيقات Aspose.Cells الطريقة cell.getValidationValue(). إذا لم تفي قيمة في الخلية بقاعدة التحقق من البيانات المطبقة على تلك الخلية، فإنه يعيد خطأ، وإلا صحيح.

يتم استخدام الملف النموذجي التالي لمايكروسوفت إكسل مع الشيفرة البرمجية النموذجية أدناه لاختبار الطريقة cell.getValidationValue(). كما يمكنك رؤية في اللقطة الفوتوغرافية أن الخلية C1 لديها تحقق بيانات عشرية مطبقة وسوف تقبل فقط القيم بين 10 و 20. كلما كانت قيمة الخلية بين 10 و 20، ستعيد الطريقة cell.getValidationValue() صحيح، وإلا، ستعيد خطأ.

todo:image_alt_text

يوضح الكود المثال التالي كيفية عمل طريقة cell.getValidationValue(). أولاً، يُدخل القيمة 3 إلى C1. لأن هذا لا يُرضي قاعدة التحقق من البيانات، فإن طريقة cell.getValidationValue() تعيد False. ثم، يُدخل القيمة 15 إلى C1. لأن هذه القيمة تُرضي قاعدة التحقق من البيانات، فإن طريقة cell.getValidationValue() تعيد True. بالمثل، تُعيد False للقيمة 30.

كود جافا للتحقق مما إذا كانت قيمة الخلية تُرضي قواعد التحقق من البيانات

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

الناتج على واجهة الأوامر الناتجة عن الكود المثال

هنا الناتج على واجهة الأوامر عند تنفيذ الكود المثال مع ملف Excel المعروض أعلاه.

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