Find or Search Data

Finding Cells Containing Specified Data

Using Microsoft Excel

Microsoft Excel allows users to find cells in a worksheet that contain specified data. If you select Edit from the Find menu in Microsoft Excel, you will see a dialog where you can specify the search value.

Here, we are looking for the value “Oranges”. Aspose.Cells also allows developers to find cells in the worksheet containing specified values.

Using Aspose.Cells for Node.js via C++

Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a Workbook.getWorksheets() collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a getCells() collection that represents all cells in the worksheet. The Cells collection provides several methods for finding cells in a worksheet containing user-specified data. A few of these methods are discussed below in more detail.

Finding Cells Containing a Formula

Developers can find a specified formula in the worksheet by calling the Cells collection’s find method. Typically, the find method accepts three parameters:

  • Object: The object to search for. The type should be int, double, DateTime, string, bool.
  • Previous cell: Previous cell with the same object. This parameter can be set to null if searching from the start.
  • FindOptions: Options for finding the required object.

The examples below use worksheet data for practicing find methods:

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

Finding Data or Formulas using FindOptions

It is possible to find specified values using the Cells collection’s Cells.find(object, Cell) method with various FindOptions. Typically, the find method accepts the following parameters:

  • Search value, the data or value to be searched for.
  • Previous cell, the last cell that contained the same value. This parameter can be set to null when searching from the start.
  • Find options, the find options.
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 ");
}

Finding Cells Containing Specified String Value or Number

It is possible to find specified string values by calling the same find method found in the Cells collection with various FindOptions.

Specify the FindOptions.setLookInType and FindOptions.setLookAtType properties. The following example code illustrates how to use these properties to find cells with various number of strings at the beginning or at the center or at the end of the cell’s string.

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

Advance topics