データフィルタリング

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

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

Microsoft Excelの自動フィルタ

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

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

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

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

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

オプション 説明
All Show all items in the list once.
Custom Customize filter criteria like contains/not contains
Filter by Color Filters based on filled color
Date Filters Filters rows based on different criteria on date
Number Filters 比較、平均、トップ10など、数値に関する異なるタイプのフィルタ。
Text Filters Different filters like begins with, ends with, contains etc,
Blanks/Non Blanks These filters can be implemented through Text Filter Blank

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

Aspose.Cells for Python Excel LibraryでAutofilterを使用します。

Aspose.Cells for Python via .NETには、Excelファイルを表すWorkbookクラスがあります。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetsコレクションが含まれています。

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

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

以下の例では、Microsoft Excelで作成したものと同じAutoFilterをAspose.Cells for Python via .NETを使用して作成しています。

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")
日付

AddDateFilter関数を使用して、2018年1月の日付を持つすべての行をフィルタリングするなど、異なる種類の日付フィルタを実装できます。以下のサンプルコードは、このフィルタを示しています。サンプルファイルは以下に示されています。

  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")
動的日付

時々動的なフィルタが必要な場合があります。例えば、年を問わず1月の日付を持つすべてのセル。この場合、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. Blank.xlsx
  2. FilteredBlank.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. Blank.xlsx
  2. FilteredNonBlank.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")
Contains カスタムフィルタ

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

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

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

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

高度なトピック