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

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

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

التصفية التلقائية في 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 Node.js via C++

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

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

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

في المثال المقدم أدناه، أنشأنا نفس التصفية التلقائية باستخدام Aspose.Cells for Node.js via C++ كما أنشأنا باستخدام Microsoft Excel في القسم أعلاه.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Creating AutoFilter by giving the cells range of the heading row
worksheet.getAutoFilter().setRange("A1:B1");
// Saving the modified Excel file
workbook.save(dataDir + "output.out.xls");

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

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

لون التعبئة

توفر Aspose.Cells إضافة الوظيفة إضافة مرشح لون التعبئة لتصفية البيانات استنادًا إلى خاصية لون التعبئة للخلايا. في المثال الوارد أدناه، يتم استخدام ملف قالب يحتوي على ألوان تعبئة مختلفة في العمود الأول من الورقة لاختبار وظيفة تصفية الألوان. يمكن تنزيل الملفات العينية من الروابط التالية.

  1. ColouredCells.xlsx
  2. FilteredColouredCells.xlsx
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Source directory
const sourceDir = dataDir;
// Output directory
const outputDir = dataDir;
// Instantiating a Workbook object
// Opening the Excel file through the file stream
let workbook = new AsposeCells.Workbook(path.join(sourceDir, "ColouredCells.xlsx"));
// Instantiating a CellsColor object for foreground color
let clrForeground = workbook.createCellsColor();
clrForeground.color = AsposeCells.Color.fromArgb(255, 0, 0); // Red color
// Instantiating a CellsColor object for background color
let clrBackground = workbook.createCellsColor();
clrBackground.color = AsposeCells.Color.fromArgb(255, 255, 255); // White color
// Accessing the first worksheet in the Excel file
let worksheet = workbook.getWorksheets().get(0);
// Call AddFillColorFilter function to apply the filter
worksheet.getAutoFilter().addFillColorFilter(0, AsposeCells.BackgroundType.Solid, clrForeground, clrBackground);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(path.join(outputDir, "FilteredColouredCells.xlsx"));
تاريخ

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

  1. Date.xlsx
  2. FilteredDate.xlsx
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const sourceDir = path.join(__dirname, "data");
const outputDir = path.join(__dirname, "output");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "Date.xlsx"));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Call AddDateFilter function to apply the filter
worksheet.getAutoFilter().addDateFilter(0, AsposeCells.DateTimeGroupingType.Month, 2018, 1, 0, 0, 0, 0);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(path.join(outputDir, "FilteredDate.xlsx"));
تاريخ ديناميكي

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

  1. Date.xlsx
  2. FilteredDynamicDate.xlsx
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const sourceDir = path.join(__dirname, "data/");
const outputDir = path.join(__dirname, "output/");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(`${sourceDir}Date.xlsx`);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Call DynamicFilter function to apply the filter
worksheet.getAutoFilter().dynamic_Filter(0, AsposeCells.DynamicFilterType.January);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(`${outputDir}FilteredDynamicDate.xlsx`);
رقم

يمكن تطبيق مرشحات مخصصة باستخدام Aspose.Cells مثل اختيار الخلايا التي تحتوي على أرقام بين نطاق معين. يظهر المثال التالي استخدام الوظيفة المخصصة لتصفية الأرقام. يتم تقديم ملفات العينات أدناه.

  1. Number.xlsx
  2. FilteredNumber.xlsx
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const sourceDir = path.join(__dirname, "data");
const outputDir = path.join(__dirname, "data");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "Number.xlsx"));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Call Custom function to apply the filter
worksheet.getAutoFilter().custom(0, AsposeCells.FilterOperatorType.GreaterOrEqual, 5, true, AsposeCells.FilterOperatorType.LessOrEqual, 10);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(path.join(outputDir, "FilteredNumber.xlsx"));
نص

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

  1. Text.xlsx
  2. FilteredText.xlsx
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const sourceDir = path.join(__dirname, "data");
const outputDir = path.join(__dirname, "output");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "Text.xlsx"));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Call Filter function to apply the filter
worksheet.getAutoFilter().filter(0, "Angola");
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(path.join(outputDir, "FilteredText.xlsx"));
فراغات

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

  1. ملف فارغ.xlsx
  2. ملف فارغ مصفى.xlsx
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Source directory
const sourceDir = path.join(dataDir, "source/");
// Output directory
const outputDir = path.join(dataDir, "output/");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(sourceDir + "Blank.xlsx");
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Call MatchBlanks function to apply the filter
worksheet.getAutoFilter().matchBlanks(0);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(outputDir + "FilteredBlank.xlsx");
غير فارغة

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

  1. ملف فارغ.xlsx
  2. ملف تصفية غير فارغ.xlsx
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Source directory
const sourceDir = dataDir + "/"; // Assuming sourceDir is stored here
// Output directory
const outputDir = dataDir + "/"; // Assuming outputDir is stored here
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(sourceDir + "Blank.xlsx");
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Call MatchNonBlanks function to apply the filter
worksheet.getAutoFilter().matchNonBlanks(0);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(outputDir + "FilteredNonBlank.xlsx");
تصفية مخصصة مع الاحتواء

توفر Excel عوامل تصفية مخصصة مثل تصفية الصفوف التي تحتوي على سلسلة نصية معينة. تتوفر هذه الميزة في Aspose.Cells وتُظهر أدناه عن طريق تصفية الأسماء في الملف النموذجي. الملفات النموذجية معروضة أدناه.

  1. sourseSampleCountryNames.xlsx
  2. outSourseSampleCountryNames.xlsx.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object containing sample data
let workbook = new AsposeCells.Workbook(path.join(dataDir, "sourseSampleCountryNames.xlsx"));
// Accessing the first worksheet in the Excel file
let worksheet = workbook.getWorksheets().get(0);
// Creating AutoFilter by giving the cells range
worksheet.getAutoFilter().setRange("A1:A18");
// Initialize filter for rows containing string "Ba"
worksheet.getAutoFilter().custom(0, AsposeCells.FilterOperatorType.Contains, "Ba");
// Refresh the filter to show/hide filtered rows
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(path.join(dataDir, "outSourseSampleCountryNames.xlsx"));
تصفية مخصصة مع عدم الاحتواء

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

  1. sourseSampleCountryNames.xlsx.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object containing sample data
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sourseSampleCountryNames.xlsx"));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Creating AutoFilter by giving the cells range
worksheet.getAutoFilter().setRange("A1:A18");
// Initialize filter for rows containing string "Ba"
worksheet.getAutoFilter().custom(0, AsposeCells.FilterOperatorType.NotContains, "Be");
// Refresh the filter to show/hide filtered rows
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(path.join(dataDir, "outSourseSampleCountryNames.xlsx"));
تصفية مخصصة تبدأ ب

يقدم إكسل فلاتر مخصصة مثل تصفية الصفوف التي تبدأ بسلسلة محددة. تتوفر هذه الميزة في Aspose.Cells ويتم توضيحها أدناه من خلال تصفية الأسماء في الملف النموذجي المقدم أدناه.

  1. sourseSampleCountryNames.xlsx.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const sourceDir = path.join(__dirname, "data/");
const outputDir = path.join(__dirname, "output/");
// Instantiating a Workbook object containing sample data
let workbook = new AsposeCells.Workbook(sourceDir + "sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
let worksheet = workbook.getWorksheets().get(0);
// Creating AutoFilter by giving the cells range
worksheet.getAutoFilter().setRange("A1:A18");
// Initialize filter for rows starting with string "Ba"
worksheet.getAutoFilter().custom(0, AsposeCells.FilterOperatorType.BeginsWith, "Ba");
// Refresh the filter to show/hide filtered rows
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(outputDir + "outSourseSampleCountryNames.xlsx");
تصفية مخصصة تنتهي ب

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

  1. sourseSampleCountryNames.xlsx.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const sourceDir = path.join(__dirname, "data/");
const outputDir = path.join(__dirname, "output/");
// Instantiating a Workbook object containing sample data
const workbook = new AsposeCells.Workbook(sourceDir + "sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Creating AutoFilter by giving the cells range
worksheet.getAutoFilter().setRange("A1:A18");
// Initialize filter for rows end with string "ia"
worksheet.getAutoFilter().custom(0, AsposeCells.FilterOperatorType.BeginsWith, "ia");
// Refresh the filter to show/hide filtered rows
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(outputDir + "outSourseSampleCountryNames.xlsx");

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