データフィルタリング

データの自動フィルタリング

自動フィルタリングは、リスト内で表示したいアイテムのみを素早く選択できる最も簡単な方法です。自動フィルタ機能により、リスト内のアイテムを特定の基準に従ってフィルタリングできます。テキスト、数値、または日付に基づいてフィルタリングできます。

Microsoft Excelの自動フィルタ

Microsoft Excelで自動フィルタ機能を有効にするには:

  1. ワークシート内の見出し行をクリックします。
  2. データ メニューから、フィルタ を選択し、その後** 自動フィルタ** を選択します。

ワークシートに自動フィルタを適用すると、フィルタスイッチ(黒い矢印)が列見出しの右側に表示されます。

  1. フィルタ矢印をクリックして、フィルタオプションのリストを表示します。

自動フィルタのオプションには次のものがあります:

オプション 説明
All リストのすべてのアイテムを一度に表示します。
Custom 含む/含まないなどのフィルター条件をカスタマイズします
Filter by Color 塗りつぶし色に基づくフィルター
Date Filters 日付に基づくさまざまな条件で行をフィルター
Number Filters 比較、平均、トップ10など、数値に関する異なるタイプのフィルタ。
Text Filters 始まり、終わり、含むなどのさまざまなフィルター
Blanks/Non Blanks これらのフィルターはテキストフィルター空白を通じて実装できます

Microsoft Excelのユーザーは、これらのオプションを使用してワークシートデータを手動でフィルタリングします。

Aspose.Cells for Node.js via C++を用いた自動フィルタ|

Aspose.CellsはExcelファイルを表すWorkbookクラスを提供します。WorkbookクラスにはワークシートにアクセスできるWorksheetsコレクションが含まれています

ワークシートは、ワークシートクラスによって表されます。ワークシートクラスには、ワークシートを管理するための広範なプロパティとメソッドが含まれています。Autofilterを作成するには、ワークシートクラスのAutoFilterプロパティを使用します。AutoFilterプロパティはAutoFilterクラスのオブジェクトであり、ヘッダ行を構成するセル範囲を指定するRangeプロパティが提供されています。オートフィルタは、ヘッダ行を構成するセル範囲に適用されます。

各ワークシートで、1つのフィルタ範囲のみを指定できます。これはMicrosoft Excelによって制限されています。カスタムデータフィルタリングには、AutoFilter.Customメソッドを使用します。

以下の例では、Microsoft Excelで作成したのと同じAutoFilterをAspose.Cells for Node.js via C++を使用して作成しました。|

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では、セルの塗りつぶし色プロパティに基づいてデータをフィルタリングするためのAddFillColorFilter関数が提供されています。以下の例では、シートの最初の列に異なる塗りつぶし色を持つテンプレートファイルを使用して色のフィルタリング機能をテストしています。サンプルファイルは以下からダウンロードできます

  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年1月のすべての日付を持つ行をフィルタリングするなど、さまざまなタイプの日時フィルターを実装できます。以下のサンプルコードは、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"));
動的日付

時々動的なフィルタが必要な場合があります。例えば、年を問わず1月の日付を持つすべてのセル。この場合、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を使用して数値の範囲内のセルを選択するなど、カスタムフィルタを適用できます。以下の例では、Custom()関数を使用して数値をフィルタする方法を示しています。サンプルファイルは以下にあります

  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. Blank.xlsx
  2. FilteredBlank.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. Blank.xlsx
  2. FilteredNonBlank.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");
Contains カスタムフィルタ

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"));
NotContains カスタムフィルタ

Excelは特定の文字列を含まない行をフィルタリングするカスタムフィルタを提供しています。この機能は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"));
BeginsWith カスタムフィルタ

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
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");
EndsWith カスタムフィルタ

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");

高度なトピック