التحقق من البيانات

أنواع التحقق من البيانات والتنفيذ

التحقق من البيانات هو القدرة على وضع قواعد تتعلق بالبيانات المُدخلة على ورقة العمل. على سبيل المثال، استخدم التحقق لضمان أن العمود المسمى تاريخ يحتوي فقط على تواريخ، أو أن عمودًا آخر يحتوي فقط على أرقام. يمكنك حتى التأكد من أن العمود المسمى تاريخ يحتوي فقط على تواريخ ضمن نطاق معين. باستخدام التحقق من البيانات، يمكنك التحكم في ما يُدخل في الخلايا في ورقة العمل.

يدعم Microsoft Excel عددًا من أنواع التحقق المختلفة للبيانات. يُستخدم كل نوع للتحكم في نوع البيانات التي تُدخل إلى خلية أو نطاق الخلايا. أدناه، مقتطفات الكود توضح كيفية التحقق من:

  • أن الأرقام صحيحة، أي أن ليس لديها جزء عشري.
  • أن الأرقام العشرية تتبع الهيكل الصحيح. يحدد مثال الكود أن يكون نطاق الخلايا يجب أن يحتوي على اثنين من أماكن العشرية.
  • أن القيم مقيدة بقائمة من القيم. يحدد التحقق بالقائمة قائمة منفصلة من القيم التي يمكن تطبيقها على خلية أو نطاق الخلايا.
  • يتمثل التواريخ ضمن نطاق محدد.
  • أن الوقت يكون ضمن نطاق محدد.
  • أن النص يكون ضمن طول حرف معين.

التحقق من البيانات مع Microsoft Excel

لإنشاء التحققات باستخدام Microsoft Excel:

  1. في ورقة العمل، حدد الخلايا التي ترغب في تطبيق التحقق عليها.
  2. من قائمة البيانات، حدد التحقق. سيتم عرض حوار التحقق.
  3. انقر على علامة التبويب الإعدادات ثم أدخل الإعدادات.

التحقق من البيانات بمكتبة Aspose.Cells لبرنامج Python Excel

التحقق من البيانات هو ميزة قوية للتحقق من المعلومات المُدخلة في أوراق العمل. باستخدام التحقق من البيانات، يمكن للمطورين توفير للمستخدمين قائمة من الخيارات، وتقييد إدخالات البيانات إلى نوع معين أو حجم، الخ. في Aspose.Cells لبرنامج Python via .NET، لكل صنف Worksheet ميزة validations التي تمثل مجموعة من Validation الكائنات. لإعداد التحقق، قم بتعيين بعض خصائص صنف Validation كما يلي:

  • النوع – يمثل نوع التحقق، الذي يمكن تحديده باستخدام أحد القيم المحددة في التعداد ValidationType .
  • المشغل – يمثل المشغل الذي سيتم استخدامه في التحقق، والذي يمكن تحديده باستخدام أحد القيم المحددة في التعداد OperatorType .
  • الصيغة 1 – يمثل القيمة أو التعبير المرتبط بالجزء الأول من التحقق البيانات.
  • الصيغة2 - تمثل القيمة أو التعبير المرتبط بالجزء الثاني من التحقق من البيانات.

عند تكوين خصائص كائن Validation ، يمكن للمطورين استخدام هيكل CellArea لتخزين المعلومات حول نطاق الخلايا الذي سيتم التحقق من صحته باستخدام التحقق من البيانات الذي تم إنشاؤه.

أنواع التحقق من البيانات

يحتوي تعداد ValidationType على الأعضاء التالية:

اسم العضو الوصف
ANY_VALUE يشير إلى قيمة من أي نوع.
WHOLE_NUMBER يشير إلى نوع التحقق للأرقام الصحيحة.
عشري يشير إلى نوع التحقق للأرقام العشرية.
قائمة يشير إلى نوع التحقق لقائمة منسدلة.
DATE يشير إلى نوع التحقق للتواريخ.
الوقت يشير إلى نوع التحقق للوقت.
TEXT_LENGTH يشير إلى نوع التحقق لطول النص.
مخصص يشير إلى نوع التحقق المخصص.
تحقق البيانات من الأعداد الصحيحة

مع هذا النوع من التحقق، يمكن للمستخدمين إدخال أعداد صحيحة فقط داخل النطاق المحدد في الخلايا المحققة. تُظهر الأمثلة البرمجية التالية كيفية تنفيذ نوع التحقق WholeNumber. ينشئ المثال نفس التحقق من البيانات باستخدام Aspose.Cells for Python via .NET الذي أنشأناه باستخدام Microsoft Excel أعلاه.

from aspose.cells import CellArea, OperatorType, ValidationType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create a workbook object.
workbook = Workbook()
# Create a worksheet and get the first worksheet.
ExcelWorkSheet = workbook.worksheets[0]
# Accessing the Validations collection of the worksheet
validations = workbook.worksheets[0].validations
# Create Cell Area
ca = CellArea()
ca.start_row = 0
ca.end_row = 0
ca.start_column = 0
ca.end_column = 0
# Creating a Validation object
validation = validations[validations.add(ca)]
# Setting the validation type to whole number
validation.type = ValidationType.WHOLE_NUMBER
# Setting the operator for validation to Between
validation.operator = OperatorType.BETWEEN
# Setting the minimum value for the validation
validation.formula1 = "10"
# Setting the maximum value for the validation
validation.formula2 = "1000"
# Applying the validation to a range of cells from A1 to B2 using the
# CellArea structure
area = CellArea()
area.start_row = 0
area.end_row = 1
area.start_column = 0
area.end_column = 1
# Adding the cell area to Validation
validation.add_area(area)
# Save the workbook.
workbook.save("output.out.xls")
تحقق البيانات من القائمة

يسمح هذا النوع من التحقق للمستخدم بإدخال قيم من قائمة منسدلة. يوفر قائمة: سلسلة من الصفوف التي تحتوي على بيانات. في المثال، يتم إضافة ورقة عمل ثانية لاحتواء مصدر القائمة. يمكن للمستخدمين فقط تحديد القيم من القائمة. منطقة التحقق هي نطاق الخلية A1:A5 في الورقة العمل الأولى.

من المهم هنا تعيين Validation.in_cell_drop_down إلى true.

from aspose.cells import CellArea, OperatorType, ValidationAlertType, ValidationType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create a workbook object.
workbook = Workbook()
# Get the first worksheet.
worksheet1 = workbook.worksheets[0]
# Add a new worksheet and access it.
i = workbook.worksheets.add()
worksheet2 = workbook.worksheets[i]
# Create a range in the second worksheet.
range = worksheet2.cells.create_range("E1", "E4")
# Name the range.
range.name = "MyRange"
# Fill different cells with data in the range.
worksheet2.cells.get("E1").value = "Blue"
worksheet2.cells.get("E2").value = "Red"
worksheet2.cells.get("E3").value = "Green"
worksheet2.cells.get("E4").value = "Yellow"
# Get the validations collection.
validations = worksheet1.validations
# Create Cell Area
ca = CellArea()
ca.start_row = 0
ca.end_row = 0
ca.start_column = 0
ca.end_column = 0
# Create a new validation to the validations list.
validation = validations[validations.add(ca)]
# Set the validation type.
validation.type = ValidationType.LIST
# Set the operator.
validation.operator = OperatorType.NONE
# Set the in cell drop down.
validation.in_cell_drop_down = True
# Set the formula1.
validation.formula1 = "=MyRange"
# Enable it to show error.
validation.show_error = True
# Set the alert type severity level.
validation.alert_style = ValidationAlertType.STOP
# Set the error title.
validation.error_title = "Error"
# Set the error message.
validation.error_message = "Please select a color from the list"
# Specify the validation area.
area = CellArea()
area.start_row = 0
area.end_row = 4
area.start_column = 0
area.end_column = 0
# Add the validation area.
validation.add_area(area)
# Save the Excel file.
workbook.save("output.out.xls")
تحقق البيانات من التاريخ

مع هذا النوع من التحقق، يقوم المستخدمون بإدخال قيم تاريخية ضمن النطاق المحدد، أو تلبية معايير محددة، داخل الخلايا المحققة. في المثال، يتم تقييد المستخدم لإدخال تواريخ بين 1970 و1999. هنا، منطقة التحقق هي خلية B1.

from aspose.cells import CellArea, OperatorType, ValidationAlertType, ValidationType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create a workbook.
workbook = Workbook()
# Obtain the cells of the first worksheet.
cells = workbook.worksheets[0].cells
# Put a string value into the A1 cell.
cells.get("A1").put_value("Please enter Date b/w 1/1/1970 and 12/31/1999")
# Set row height and column width for the cells.
cells.set_row_height(0, float(31))
cells.set_column_width(0, float(35))
# Get the validations collection.
validations = workbook.worksheets[0].validations
# Create Cell Area
ca = CellArea()
ca.start_row = 0
ca.end_row = 0
ca.start_column = 0
ca.end_column = 0
# Add a new validation.
validation = validations[validations.add(ca)]
# Set the data validation type.
validation.type = ValidationType.DATE
# Set the operator for the data validation
validation.operator = OperatorType.BETWEEN
# Set the value or expression associated with the data validation.
validation.formula1 = "1/1/1970"
# The value or expression associated with the second part of the data validation.
validation.formula2 = "12/31/1999"
# Enable the error.
validation.show_error = True
# Set the validation alert style.
validation.alert_style = ValidationAlertType.STOP
# Set the title of the data-validation error dialog box
validation.error_title = "Date Error"
# Set the data validation error message.
validation.error_message = "Enter a Valid Date"
# Set and enable the data validation input message.
validation.input_message = "Date Validation Type"
validation.ignore_blank = True
validation.show_input = True
# Set a collection of CellArea which contains the data validation settings.
cellArea = CellArea()
cellArea.start_row = 0
cellArea.end_row = 0
cellArea.start_column = 1
cellArea.end_column = 1
# Add the validation area.
validation.add_area(cellArea)
# Save the Excel file.
workbook.save("output.out.xls")
تحقق المواقيت الزمنية للبيانات

مع هذا النوع من التحقق، يمكن للمستخدمين إدخال أوقات ضمن نطاق محدد، أو تلبية بعض المعايير، في الخلايا الموجودة. ففي المثال، يتم تقييد المستخدم بإدخال الأوقات بين الساعة 09:00 و11:30 صباحًا. هنا، مجال التحقق هو خلية B1.

from aspose.cells import CellArea, OperatorType, ValidationAlertType, ValidationType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create a workbook.
workbook = Workbook()
# Obtain the cells of the first worksheet.
cells = workbook.worksheets[0].cells
# Put a string value into A1 cell.
cells.get("A1").put_value("Please enter Time b/w 09:00 and 11:30 'o Clock")
# Set the row height and column width for the cells.
cells.set_row_height(0, float(31))
cells.set_column_width(0, float(35))
# Get the validations collection.
validations = workbook.worksheets[0].validations
# Create Cell Area
ca = CellArea()
ca.start_row = 0
ca.end_row = 0
ca.start_column = 0
ca.end_column = 0
# Add a new validation.
validation = validations[validations.add(ca)]
# Set the data validation type.
validation.type = ValidationType.TIME
# Set the operator for the data validation.
validation.operator = OperatorType.BETWEEN
# Set the value or expression associated with the data validation.
validation.formula1 = "09:00"
# The value or expression associated with the second part of the data validation.
validation.formula2 = "11:30"
# Enable the error.
validation.show_error = True
# Set the validation alert style.
validation.alert_style = ValidationAlertType.INFORMATION
# Set the title of the data-validation error dialog box.
validation.error_title = "Time Error"
# Set the data validation error message.
validation.error_message = "Enter a Valid Time"
# Set and enable the data validation input message.
validation.input_message = "Time Validation Type"
validation.ignore_blank = True
validation.show_input = True
# Set a collection of CellArea which contains the data validation settings.
cellArea = CellArea()
cellArea.start_row = 0
cellArea.end_row = 0
cellArea.start_column = 1
cellArea.end_column = 1
# Add the validation area.
validation.add_area(cellArea)
# Save the Excel file.
workbook.save("output.out.xls")
تحقق طول النصوص للبيانات

مع هذا النوع من التحقق، يمكن للمستخدمين إدخال قيم نصية من طول محدد في الخلايا الموجودة. في المثال، يتم تقييد المستخدم بإدخال قيم سلسلة نصية بأكثر من 5 أحرف. مجال التحقق هو الخلية B1.

from aspose.cells import CellArea, OperatorType, ValidationAlertType, ValidationType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create a new workbook.
workbook = Workbook()
# Obtain the cells of the first worksheet.
cells = workbook.worksheets[0].cells
# Put a string value into A1 cell.
cells.get("A1").put_value("Please enter a string not more than 5 chars")
# Set row height and column width for the cell.
cells.set_row_height(0, float(31))
cells.set_column_width(0, float(35))
# Get the validations collection.
validations = workbook.worksheets[0].validations
# Create Cell Area
ca = CellArea()
ca.start_row = 0
ca.end_row = 0
ca.start_column = 0
ca.end_column = 0
# Add a new validation.
validation = validations[validations.add(ca)]
# Set the data validation type.
validation.type = ValidationType.TEXT_LENGTH
# Set the operator for the data validation.
validation.operator = OperatorType.LESS_OR_EQUAL
# Set the value or expression associated with the data validation.
validation.formula1 = "5"
# Enable the error.
validation.show_error = True
# Set the validation alert style.
validation.alert_style = ValidationAlertType.WARNING
# Set the title of the data-validation error dialog box.
validation.error_title = "Text Length Error"
# Set the data validation error message.
validation.error_message = " Enter a Valid String"
# Set and enable the data validation input message.
validation.input_message = "TextLength Validation Type"
validation.ignore_blank = True
validation.show_input = True
# Set a collection of CellArea which contains the data validation settings.
cellArea = CellArea()
cellArea.start_row = 0
cellArea.end_row = 0
cellArea.start_column = 1
cellArea.end_column = 1
# Add the validation area.
validation.add_area(cellArea)
# Save the Excel file.
workbook.save("output.out.xls")

قواعد تحقق البيانات

عند تنفيذ التحقق من البيانات، يمكن فحص عملية التحقق من خلال تعيين قيم مختلفة في الخلايا. يمكن استخدام Cell.get_validation_value() لاسترجاع نتيجة التحقق. يوضح المثال التالي هذه الميزة مع قيم مختلفة. يمكن تنزيل الملف العيني من الرابط التالي للفحص:

sampleDataValidationRules.xlsx

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Instantiate the workbook from sample Excel file
workbook = Workbook("sample.xlsx")
# Access the first worksheet
worksheet = workbook.worksheets[0]
# Access Cell C1
# Cell C1 has the Decimal Validation applied on it.
# It can take only the values Between 10 and 20
cell = worksheet.cells.get("C1")
# Enter 3 inside this cell
# Since it is not between 10 and 20, it should fail the validation
cell.put_value(3)
# Check if number 3 satisfies the Data Validation rule applied on this cell
print("Is 3 a Valid Value for this Cell: " + str(cell.get_validation_value()))
# Enter 15 inside this cell
# Since it is between 10 and 20, it should succeed the validation
cell.put_value(15)
# Check if number 15 satisfies the Data Validation rule applied on this cell
print("Is 15 a Valid Value for this Cell: " + str(cell.get_validation_value()))
# Enter 30 inside this cell
# Since it is not between 10 and 20, it should fail the validation again
cell.put_value(30)
# Check if number 30 satisfies the Data Validation rule applied on this cell
print("Is 30 a Valid Value for this Cell: " + str(cell.get_validation_value()))

التحقق مما إذا كان التحقق في الخلية القائمة المنسدلة

كما رأينا، هناك العديد من أنواع التحقق التي يمكن تنفيذها داخل خلية. إذا أردت التحقق ما إذا كان التحقق منسدلة أم لا، يمكن استخدام Validation.in_cell_drop_down خاصية لاختبار هذا. يوضح الكود العيني التالي استخدام هذه الخاصية. يمكن تنزيل ملف عينة للفحص من الرابط التالي:

sampleValidation.xlsx

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
book = Workbook("sampleValidation.xlsx")
sheet = book.worksheets.get("Sheet1")
cells = sheet.cells
a2 = cells.get("A2")
va2 = a2.get_validation()
if va2.in_cell_drop_down:
print("A2 is a dropdown")
else:
print("A2 is NOT a dropdown")
b2 = cells.get("B2")
vb2 = b2.get_validation()
if vb2.in_cell_drop_down:
print("B2 is a dropdown")
else:
print("B2 is NOT a dropdown")
c2 = cells.get("C2")
vc2 = c2.get_validation()
if vc2.in_cell_drop_down:
print("C2 is a dropdown")
else:
print("C2 is NOT a dropdown")

إضافة منطقة الخلية إلى التحقق القائم

قد تكون هناك حالات حيث ترغب في إضافة CellArea إلى Validation القائمة بالفعل. عندما تضيف CellArea باستخدام Validation.add_area(cell_area)، يقوم Aspose.Cells بفحص جميع المناطق القائمة لمعرفة ما إذا كانت المنطقة الجديدة موجودة بالفعل. إذا كان للملف عدد كبير من التحققات، فإن هذا يؤثر على الأداء. للتغلب على هذا، يوفر الواجهة البرمجية الطريقة Validation.add_area(cell_area, check_intersection, check_edge) . يشير معلم checkIntersection إلى ما إذا كان يجب فحص اشتراك منطقة معينة مع مناطق التحقق القائمة. تعيينها على false سيعطل فحص المناطق الأخرى. معلم checkEdge يشير إلى ما إذا كان يجب فحص المناطق المطبقة. إذا أصبحت المنطقة الجديدة هي المنطقة العلوية اليسرى، يتم إعادة إعدادات الداخلية. إذا كنت متأكدًا من أن المنطقة الجديدة ليست المنطقة العلوية اليسرى، فيمكنك ضبط هذا المعلم على false.

الكود البرمجي التالي يوضح استخدام الطريقة Validation.add_area(cell_area, check_intersection, check_edge) لإضافة CellArea جديدة إلى Validation القائمة.

from aspose.cells import CellArea, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
workbook = Workbook("ValidationsSample.xlsx")
# Access first worksheet.
worksheet = workbook.worksheets[0]
# Accessing the Validations collection of the worksheet
validation = worksheet.validations[0]
# Create your cell area.
cellArea = CellArea.create_cell_area("D5", "E7")
# Adding the cell area to Validation
validation.add_area(cellArea, False, False)
# Save the output workbook.
workbook.save("ValidationsSample_out.xlsx")

الملفات الإكسل المصدر والناتج مرفقة للرجوع إليها.

ملف المصدر

ملف الإخراج

مواضيع متقدمة