Daten finden oder suchen

Suchen von Zellen, die bestimmte Daten enthalten

Verwendung von Microsoft Excel

Microsoft Excel ermöglicht es Benutzern, Zellen in einem Arbeitsblatt zu finden, die bestimmte Daten enthalten. Wenn Sie Bearbeiten im Suchen-Menü in Microsoft Excel auswählen, sehen Sie einen Dialog, in dem Sie den Suchwert festlegen können.

Hier suchen wir nach dem Wert “Orangen”. Aspose.Cells ermöglicht es Entwicklern auch, in einem Arbeitsblatt Zellen mit bestimmten Werten zu finden.

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

Aspose.Cells stellt eine Klasse, Workbook, bereit, die eine Microsoft Excel-Datei darstellt. Die Klasse Workbook enthält eine Workbook.getWorksheets()-Sammlung, die Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch die Klasse Worksheet repräsentiert. Die Klasse Worksheet bietet eine getCells()-Sammlung, die alle Zellen im Arbeitsblatt repräsentiert. Die Cells-Sammlung stellt verschiedene Methoden zum Finden von Zellen in einem Arbeitsblatt bereit, die vom Benutzer angegebene Daten enthalten. Einige dieser Methoden werden im Folgenden detaillierter erläutert.

Suchen von Zellen, die eine Formel enthalten

Entwickler können eine bestimmte Formel im Arbeitsblatt finden, indem sie die Cells-Sammlungs-Methode find aufrufen. Typischerweise akzeptiert die find-Methode drei Parameter:

  • Objekt: Das zu suchende Objekt. Der Typ sollte int, double, DateTime, string, bool sein.
  • Vorherige Zelle: Vorherige Zelle mit demselben Objekt. Dieser Parameter kann auf null gesetzt werden, wenn von Anfang an gesucht wird.
  • FindOptions: Optionen zum Finden des benötigten Objekts.

Die folgenden Beispiele verwenden Arbeitsblattdaten, um die Find-Methoden zu üben:

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

Suchen von Daten oder Formeln mithilfe von FindOptions

Es ist möglich, bestimmte Werte mit der Cells-Sammlungs-Cells.find(object, Cell)-Methode und verschiedenen FindOptions zu finden. Typischerweise akzeptiert die find-Methode die folgenden Parameter:

  • Suchwert, die Daten oder der Wert, nach dem gesucht werden soll.
  • Vorherige Zelle, die letzte Zelle, die den gleichen Wert enthielt. Dieser Parameter kann auf null gesetzt werden, wenn von Anfang an gesucht wird.
  • Suchoptionen, die Suchoptionen.
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 ");
}

Zellen finden, die den angegebenen Zeichenfolgenwert oder die angegebene Zahl enthalten

Es ist möglich, bestimmte Zeichenkettenwerte durch Aufrufen derselben find-Methode zu finden, die in der Cells-Sammlung mit verschiedenen FindOptions vorhanden ist.

Geben Sie die Eigenschaften FindOptions.setLookInType und FindOptions.setLookAtType an. Das folgende Beispiel zeigt, wie diese Eigenschaften verwendet werden, um Zellen mit unterschiedlicher Anzahl von Zeichenketten am Anfang, in der Mitte oder am Ende der Zelle zu finden.

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

Erweiterte Themen