データの検索または検索
指定されたデータを含むセルの検索
Microsoft Excel の使用
Microsoft Excelでは、ワークシート内の含まれる指定されたデータを持つセルを検索できます。Microsoft Excelの検索メニューから編集を選択すると、検索値を指定できるダイアログが表示されます。
ここでは、「オレンジ」の値を検索しています。Aspose.Cellsでは、指定された値を含むワークシート内のセルを検索できます。
Aspose.Cellsの使用
Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるworksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。Worksheetクラスにはワークシート内のすべてのセルを表すcellsコレクションが含まれており、cellsコレクションにはユーザー指定データを含むワークシート内のセルを検索するためのいくつかのメソッドが提供されています。これらのメソッドのいくつかについて詳しく説明します。
指定された数式を含むセルの検索
開発者は、cellsコレクションのfindメソッドを呼び出すことによって、ワークシート内で指定された数式を検索できます。通常、findメソッドは3つのパラメータを受け入れます。
- what: 検索するオブジェクト。タイプはint、double、DateTime、string、boolである必要があります。
- previous_cell: 同じオブジェクトを持つ前のセル。このパラメータは、開始位置から検索する場合はnullに設定できます。
- find_options: 必要なオブジェクトを検索するためのオプション。
以下の例では、検索メソッドの練習にワークシートデータを使用します:
from aspose.cells import FindOptions, LookInType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Opening the Excel file | |
workbook = Workbook(sourceDir + "sampleFindingCellsContainingFormula.xlsx") | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Instantiate FindOptions Object | |
findOptions = FindOptions() | |
findOptions.look_in_type = LookInType.FORMULAS | |
# Finding the cell containing the specified formula | |
cell = worksheet.cells.find("=SUM(A5:A10)", None, findOptions) | |
# Printing the name of the cell found after searching worksheet | |
print("Name of the cell containing formula: " + cell.name) |
FindOptionsを使用したデータまたは式の検索
異なるFindOptionsを使用して、cellsのfindメソッドを使用して指定した値を検索することが可能です。通常、findメソッドは次のパラメータを受け入れます:
- what:、検索対象のデータまたは値。
- previous_cell、同じ値を含む最後のセル。このパラメータは、開始位置から検索する場合はnullに設定できます。
- find_options、検索オプション。
from aspose.cells import CellArea, FindOptions, LookAtType, LookInType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Instantiate the workbook object | |
workbook = Workbook(sourceDir + "sampleFindingDataOrFormulasUsingFindOptions.xlsx") | |
workbook.calculate_formula() | |
# Get Cells collection | |
cells = workbook.worksheets[0].cells | |
# Instantiate FindOptions Object | |
findOptions = FindOptions() | |
# Create a Cells Area | |
ca = CellArea() | |
ca.start_row = 8 | |
ca.start_column = 2 | |
ca.end_row = 17 | |
ca.end_column = 13 | |
# Set cells area for find options | |
findOptions.set_range(ca) | |
# Set searching properties | |
findOptions.search_backward = False | |
findOptions.seach_order_by_rows = True | |
# Set the lookintype, you may specify, values, formulas, comments etc. | |
findOptions.look_in_type = LookInType.VALUES | |
# Set the lookattype, you may specify Match entire content, endswith, starwith etc. | |
findOptions.look_at_type = LookAtType.ENTIRE_CONTENT | |
# Find the cell with value | |
cell = cells.find(341, None, findOptions) | |
if cell != None: | |
print("Name of the cell containing the value: " + cell.name) | |
else: | |
print("Record not found ") |
指定された文字列値を見つけることが可能です。異なる{2}を持つ{1}コレクション内に見つかった{0}メソッドを呼び出すことで。
異なるFindOptionsを持つcellsコレクションの中で見つかったfindメソッドを呼び出すことで、指定された文字列値を見つけることが可能です。
FindOptions.look_in_typeとFindOptions.look_at_typeプロパティを指定します。次の例コードは、これらのプロパティを使用してセル内の文字列の先頭、または中央、または末尾にさまざまな数の文字列を探す方法を示しています。
from aspose.cells import FindOptions, LookAtType, LookInType, 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(".") | |
# Instantiate the workbook object | |
workbook = Workbook(dataDir + "book1.xls") | |
# Get Cells collection | |
cells = workbook.worksheets[0].cells | |
opts = FindOptions() | |
opts.look_in_type = LookInType.VALUES | |
opts.look_at_type = LookAtType.ENTIRE_CONTENT | |
# Find the cell with the input integer or double | |
cell1 = cells.find(205, None, opts) | |
if cell1 != None: | |
print("Name of the cell containing the value: " + cell1.name) | |
else: | |
print("Record not found ") | |
# Find the cell with the input string | |
cell2 = cells.find("Items A", None, opts) | |
if cell2 != None: | |
print("Name of the cell containing the value: " + cell2.name) | |
else: | |
print("Record not found ") | |
# Find the cell containing with the input string | |
opts.look_at_type = LookAtType.CONTAINS | |
cell3 = cells.find("Data", None, opts) | |
if cell3 != None: | |
print("Name of the cell containing the value: " + cell3.name) | |
else: | |
print("Record not found ") |