تصفية البيانات

عنصرية البيانات

التصفية التلقائية هي أسرع طريقة لتحديد العناصر الوحيدة التي ترغب في عرضها في قائمة. تتيح ميزة التصفية التلقائية للمستخدمين تصفية العناصر في القائمة وفقًا لمعايير محددة. تصفية بناءً على النصوص أو الأرقام أو التواريخ.

التصفية التلقائية في Microsoft Excel

لتفعيل ميزة التصفية التلقائية في Microsoft Excel:

  1. انقر على صف العنوان في ورقة العمل.
  2. من قائمة البيانات، حدد تصفية ثم تصفية تلقائية.

عند تطبيق التصفية التلقائية على ورقة عمل، يظهر التبديل (السهام السوداء) إلى يمين عناوين العمود.

  1. انقر على سهم التصفية لرؤية قائمة الخيارات التصفية.

بعض خيارات التصفية التلقائية هي:

الخيارات الوصف
All
Custom
Filter by Color
Date Filters
Number Filters
Text Filters
Blanks/Non Blanks

يقوم المستخدمون بتصفية بيانات ورقة العمل يدويًا في Microsoft Excel باستخدام هذه الخيارات.

التصفية التلقائية باستخدام مكتبة Aspose.Cells for Python Excel

توفر Aspose.Cells for Python via .NET فئة تمثل ملف Excel وتحتوي على مجموعة من ورق العمل التي تسمح بالوصول إلى كل ورق عمل في ملف Excel.

ورقة العمل ممثلة بفئة ورقة العمل، وتقدم فئة ورقة العمل مجموعة واسعة من الخصائص والأساليب لإدارة أوراق العمل. لإنشاء تصفية تلقائية، استخدم خاصية AutoFilter من فئة ورقة العمل. وتعتبر خاصية AutoFilter كائنًا لفئة AutoFilter توفر خاصية Range لتحديد نطاق الخلايا التي تتكون من صف العنوان. يتم تطبيق التصفية التلقائية على نطاق الخلايا الذي يشكل صف العناوين.

في كل ورقة عمل، يمكنك تحديد نطاق تصفية واحد فقط وهذا محدود بواسطة Microsoft Excel. لتصفية البيانات المخصصة، استخدم الأسلوب AutoFilter.Custom.

في المثال أدناه، قمنا بإنشاء نفس التصفية التلقائية باستخدام Aspose.Cells for Python via .NET كما قمنا بإنشاءها باستخدام Microsoft Excel في الجزء السابق.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(dataDir + "book1.xls")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Creating AutoFilter by giving the cells range of the heading row
worksheet.auto_filter.range = "A1:B1"
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")

أنواع مختلفة من التصفية

يوفر Aspose.Cells for Python via .NET خيارات متعددة لتطبيق أنواع مختلفة من التصفية مثل تصفية اللون، وتصفية التاريخ، وتصفية الرقم، وتصفية النص، وتصفية الخانات الفارغة، وتصفية الخانات غير الفارغة.

لون التعبئة

يوفر Aspose.Cells for Python via .NET وظيفة AddFillColorFilter لتصفية البيانات بناءً على خاصية لون التعبئة للخلايا. في المثال أدناه، تم استخدام ملف قالب يحتوي على ألوان تعبئة مختلفة في العمود الأول من الورقة لاختبار وظيفة تصفية الألوان. يمكن تنزيل الملفات العينية من الروابط التالية.

  1. ColouredCells.xlsx
  2. FilteredColouredCells.xlsx
from aspose.cells import BackgroundType, Workbook
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(sourceDir + "ColouredCells.xlsx")
# Instantiating a CellsColor object for foreground color
clrForeground = workbook.create_cells_color()
clrForeground.color = Color.red
# Instantiating a CellsColor object for background color
clrBackground = workbook.create_cells_color()
clrBackground.color = Color.white
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Call AddFillColorFilter function to apply the filter
worksheet.auto_filter.add_fill_color_filter(0, BackgroundType.SOLID, clrForeground, clrBackground)
# Call refresh function to update the worksheet
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save(outputDir + "FilteredColouredCells.xlsx")
تاريخ

يمكن تنفيذ أنواع مختلفة من عمليات تصفية التاريخ مثل تصفية جميع الصفوف التي تحتوي على تواريخ في يناير 2018. يوضح الكود العيني التالي هذا التصفية باستخدام الدالة AddDateFilter. يتم إعطاء ملفات عينية أدناه.

  1. Date.xlsx
  2. FilteredDate.xlsx
from aspose.cells import DateTimeGroupingType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(sourceDir + "Date.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Call AddDateFilter function to apply the filter
worksheet.auto_filter.add_date_filter(0, DateTimeGroupingType.MONTH, 2018, 1, 0, 0, 0, 0)
# Call refresh function to update the worksheet
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save(outputDir + "FilteredDate.xlsx")
تاريخ ديناميكي

في بعض الأحيان، تكون التصفيات الديناميكية مطلوبة بناءً على التاريخ مثل جميع الخلايا التي تحتوي على تواريخ في يناير بغض النظر عن السنة. في هذه الحالة، يتم استخدام دالة DynamicFilter كما هو موضح في الكود العيني التالي. يتم إعطاء ملفات عينية أدناه.

  1. Date.xlsx
  2. FilteredDynamicDate.xlsx
from aspose.cells import DynamicFilterType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(sourceDir + "Date.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Call DynamicFilter function to apply the filter
worksheet.auto_filter.dynamic_filter(0, DynamicFilterType.JANUARY)
# Call refresh function to update the worksheet
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save(outputDir + "FilteredDynamicDate.xlsx")
رقم

تطبيق التصفية المخصصة يمكن استخدامها باستخدام Aspose.Cells for Python via .NET مثل اختيار الخلايا التي تحتوي على أرقام بين النطاق المعطى. يوضح المثال التالي استخدام الدالة Custom() لتصفية الأرقام. يتم تقديم ملفات عينة أدناه.

  1. Number.xlsx
  2. FilteredNumber.xlsx
from aspose.cells import FilterOperatorType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(sourceDir + "Number.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Call Custom function to apply the filter
worksheet.auto_filter.custom(0, FilterOperatorType.GREATER_OR_EQUAL, 5, True, FilterOperatorType.LESS_OR_EQUAL, 10)
# Call refresh function to update the worksheet
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save(outputDir + "FilteredNumber.xlsx")
نص

إذا كانت العمود يحتوي على نص ويتعين تحديد الخلايا التي تحتوي على نص معين، فيمكن استخدام دالة Filter(). في المثال التالي، يحتوي ملف القالب على قائمة بالدول ويجب تحديد الصف الذي يحتوي على اسم دولة معين. يوضح الكود التالي تصفية النصوص. يتم تقديم ملفات عينة أدناه.

  1. Text.xlsx
  2. FilteredText.xlsx
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(sourceDir + "Text.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Call Filter function to apply the filter
worksheet.auto_filter.filter(0, "Angola")
# Call refresh function to update the worksheet
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save(outputDir + "FilteredText.xlsx")
فراغات

إذا كان العمود يحتوي على نص بحيث تكون بعض الخلايا فارغة ويتطلب التصفية لتحديد الصفوف فقط حيث يتواجد الخلية الفارغة، فيمكن استخدام الدالة MatchBlanks() كما هو موضح أدناه. تتم تقديم ملفات عينة أدناه.

  1. ملف فارغ.xlsx
  2. ملف فارغ مصفى.xlsx
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(sourceDir + "Blank.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Call MatchBlanks function to apply the filter
worksheet.auto_filter.match_blanks(0)
# Call refresh function to update the worksheet
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save(outputDir + "FilteredBlank.xlsx")
غير فارغة

عندما يتعين تصفية الخلايا التي تحتوي على أي نص، استخدم دالة MatchNonBlanks كمصفية كما هو موضح أدناه. تتم تقديم ملفات عينة أدناه.

  1. ملف فارغ.xlsx
  2. ملف تصفية غير فارغ.xlsx
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(sourceDir + "Blank.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Call MatchNonBlanks function to apply the filter
worksheet.auto_filter.match_non_blanks(0)
# Call refresh function to update the worksheet
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save(outputDir + "FilteredNonBlank.xlsx")
تصفية مخصصة مع الاحتواء

يوفر Excel تصفية مخصصة مثل تصفية الصفوف التي تحتوي على سلسلة نصية معينة. يتوفر هذا الميزة في Aspose.Cells for Python via .NET ويتم توضيحه أدناه بتصفية الأسماء في الملف العينة. تتم تقديم ملفات عينة أدناه.

  1. sourseSampleCountryNames.xlsx
  2. outSourseSampleCountryNames.xlsx.
from aspose.cells import FilterOperatorType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Instantiating a Workbook object containing sample data
workbook = Workbook("sourseSampleCountryNames.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Creating AutoFilter by giving the cells range
worksheet.auto_filter.range = "A1:A18"
# Initialize filter for rows containing string "Ba"
worksheet.auto_filter.custom(0, FilterOperatorType.CONTAINS, "Ba")
# Refresh the filter to show/hide filtered rows
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save("outSourseSampleCountryNames.xlsx")
تصفية مخصصة مع عدم الاحتواء

إكسل يوفر عوامل تصفية مخصصة مثل تصفية الصفوف التي لا تحتوي على سلسلة معينة. هذه الميزة متاحة في Aspose.Cells for Python via .NET وموضحة أدناه من خلال تصفية الأسماء في الملف النموذجي المعطى أدناه.

  1. sourseSampleCountryNames.xlsx.
from aspose.cells import FilterOperatorType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Instantiating a Workbook object containing sample data
workbook = Workbook("sourseSampleCountryNames.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Creating AutoFilter by giving the cells range
worksheet.auto_filter.range = "A1:A18"
# Initialize filter for rows containing string "Ba"
worksheet.auto_filter.custom(0, FilterOperatorType.NOT_CONTAINS, "Be")
# Refresh the filter to show/hide filtered rows
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save("outSourseSampleCountryNames.xlsx")
تصفية مخصصة تبدأ ب

إكسل يوفر عوامل تصفية مخصصة مثل تصفية الصفوف التي تبدأ بسلسلة معينة. هذه الميزة متاحة في Aspose.Cells for Python via .NET وموضحة أدناه من خلال تصفية الأسماء في الملف النموذجي المعطى أدناه.

  1. sourseSampleCountryNames.xlsx.
from aspose.cells import FilterOperatorType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Instantiating a Workbook object containing sample data
workbook = Workbook(sourceDir + "sourseSampleCountryNames.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Creating AutoFilter by giving the cells range
worksheet.auto_filter.range = "A1:A18"
# Initialize filter for rows starting with string "Ba"
worksheet.auto_filter.custom(0, FilterOperatorType.BEGINS_WITH, "Ba")
# Refresh the filter to show/hide filtered rows
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save(outputDir + "outSourseSampleCountryNames.xlsx")
تصفية مخصصة تنتهي ب

إكسل يوفر عوامل تصفية مخصصة مثل تصفية الصفوف التي تنتهي بسلسلة معينة. هذه الميزة متاحة في Aspose.Cells for Python via .NET وموضحة أدناه من خلال تصفية الأسماء في الملف النموذجي المعطى أدناه.

  1. sourseSampleCountryNames.xlsx.
from aspose.cells import FilterOperatorType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Instantiating a Workbook object containing sample data
workbook = Workbook(sourceDir + "sourseSampleCountryNames.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Creating AutoFilter by giving the cells range
worksheet.auto_filter.range = "A1:A18"
# Initialize filter for rows end with string "ia"
worksheet.auto_filter.custom(0, FilterOperatorType.BEGINS_WITH, "ia")
# Refresh the filter to show/hide filtered rows
worksheet.auto_filter.refresh()
# Saving the modified Excel file
workbook.save(outputDir + "outSourseSampleCountryNames.xlsx")

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