データの検索または検索
指定されたデータを含むセルの検索
Microsoft Excel の使用
Microsoft Excelユーザーは、指定したデータを含むセルを見つけることができます。Microsoft Excelの[検索]メニューから[編集]を選択すると、検索値を指定できるダイアログが表示されます。
ここでは、「オレンジ」の値を検索しています。Aspose.Cellsでは、指定された値を含むワークシート内のセルを検索できます。
Aspose.Cells for Node.js via C++を使用して
Aspose.Cellsは、Microsoft Excelファイルを表すクラスWorkbookを提供します。Workbookクラスには、Excelファイルの各ワークシートにアクセスできるWorkbook.getWorksheets()コレクションが含まれます。ワークシートはWorksheetクラスで表されます。Worksheetクラスは、ワークシート内のすべてのセルを表すgetCells()コレクションを提供します。Cellsコレクションは、ユーザー指定のデータを含むセルを検索するためのさまざまなメソッドを提供します。これらのメソッドのいくつかについては、以下で詳述します。
指定された数式を含むセルの検索
開発者は、Cellsコレクションのfindメソッドを呼び出すことによって、指定された数式をワークシート内で見つけることができます。通常、findメソッドは3つのパラメータを受け取ります。
- Object: 検索する対象。型はint、double、DateTime、string、boolのいずれか。
- 前のセル: 同じオブジェクトを持つ前のセル。このパラメータは、最初から検索する場合はnullに設定できます。
- FindOptions: 必要なオブジェクトを見つけるためのオプション。
以下の例では、検索メソッドの練習にワークシートデータを使用します:
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Opening the Excel file | |
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sampleFindingCellsContainingFormula.xlsx")); | |
// Accessing the first worksheet in the Excel file | |
const worksheet = workbook.getWorksheets().get(0); | |
// Instantiate FindOptions Object | |
const findOptions = new AsposeCells.FindOptions(); | |
findOptions.setLookInType(AsposeCells.LookInType.Formulas); | |
// Finding the cell containing the specified formula | |
const cell = worksheet.getCells().find("=SUM(A5:A10)", null, findOptions); | |
// Printing the name of the cell found after searching worksheet | |
console.log("Name of the cell containing formula: " + cell.getName()); |
FindOptionsを使用したデータまたは式の検索
さまざまなFindOptionsを用いて、CellsコレクションのCells.find(object, Cell)メソッドを呼び出すことで、指定された値を検索可能です。通常、findメソッドは次のパラメータを受け取ります。
- 検索値、検索するデータまたは値。
- 前のセル、同じ値を含んでいた最後のセル。最初から検索する場合は、このパラメーターをnullに設定できます。
- 検索オプション、検索オプション。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const sourceDir = path.join(__dirname, "data"); | |
// Instantiate the workbook object | |
const workbook = new AsposeCells.Workbook(sourceDir + "sampleFindingDataOrFormulasUsingFindOptions.xlsx"); | |
workbook.calculateFormula(); | |
// Get Cells collection | |
const cells = workbook.getWorksheets().get(0).getCells(); | |
// Instantiate FindOptions Object | |
const findOptions = new AsposeCells.FindOptions(); | |
// Create a Cells Area | |
const ca = new AsposeCells.CellArea(); | |
ca.startRow = 8; | |
ca.startColumn = 2; | |
ca.endRow = 17; | |
ca.endColumn = 13; | |
// Set cells area for find options | |
findOptions.setRange(ca); | |
// Set searching properties | |
findOptions.setSearchBackward(false); | |
findOptions.setSearchOrderByRows(true); | |
// Set the lookintype, you may specify, values, formulas, comments etc. | |
findOptions.setLookInType(AsposeCells.LookInType.Values); | |
// Set the lookattype, you may specify Match entire content, endswith, starwith etc. | |
findOptions.setLookAtType(AsposeCells.LookAtType.EntireContent); | |
// Find the cell with value | |
const cell = cells.find(341, null, findOptions); | |
if (cell !== null) { | |
console.log("Name of the cell containing the value: " + cell.getName()); | |
} else { | |
console.log("Record not found "); | |
} |
指定された文字列値を見つけることが可能です。異なる{2}を持つ{1}コレクション内に見つかった{0}メソッドを呼び出すことで。
同じfindメソッドをCellsコレクションで呼び出し、さまざまなFindOptionsを用いて文字列値を検索できる。
FindOptions.setLookInTypeとFindOptions.setLookAtTypeのプロパティを指定します。以下の例では、これらのプロパティを使用してセルの文字列の最初、中央、最後にあるさまざまな数の文字列を持つセルを見つける方法を示しています。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Instantiate the workbook object | |
const workbook = new AsposeCells.Workbook(path.join(dataDir, "book1.xls")); | |
// Get Cells collection | |
const cells = workbook.getWorksheets().get(0).getCells(); | |
const opts = new AsposeCells.FindOptions(); | |
opts.setLookInType(AsposeCells.LookInType.Values); | |
opts.setLookAtType(AsposeCells.LookAtType.EntireContent); | |
// Find the cell with the input integer or double | |
let cell1 = cells.find(205, null, opts); | |
if (cell1 !== null) { | |
console.log("Name of the cell containing the value: " + cell1.getName()); | |
} else { | |
console.log("Record not found "); | |
} | |
// Find the cell with the input string | |
let cell2 = cells.find("Items A", null, opts); | |
if (cell2 !== null) { | |
console.log("Name of the cell containing the value: " + cell2.getName()); | |
} else { | |
console.log("Record not found "); | |
} | |
// Find the cell containing the input string | |
opts.setLookAtType(AsposeCells.LookAtType.Contains); | |
let cell3 = cells.find("Data", null, opts); | |
if (cell3 !== null) { | |
console.log("Name of the cell containing the value: " + cell3.getName()); | |
} else { | |
console.log("Record not found "); | |
} |