Trova o Cerca Dati

Ricerca delle celle contenenti dati specificati

Utilizzando Microsoft Excel

Microsoft Excel consente agli utenti di trovare celle in un foglio di lavoro che contengono dati specificati. Se selezioni Modifica dal menu Trova in Microsoft Excel, vedrai una finestra di dialogo in cui puoi specificare il valore di ricerca.

Qui stiamo cercando il valore “Arance”. Aspose.Cells consente anche agli sviluppatori di trovare celle nel foglio di lavoro contenenti valori specificati.

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

Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta Workbook.getWorksheets() che permette di accedere a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta getCells() che rappresenta tutte le celle del foglio di lavoro. La raccolta Cells offre vari metodi per trovare celle in un foglio di lavoro contenenti dati inseriti dall’utente. Alcuni di questi metodi sono discussi più dettagliatamente di seguito.

Ricerca delle celle contenenti una formula

Gli sviluppatori possono trovare una formula specifica nel foglio di lavoro chiamando il metodo find nella raccolta Cells. In genere, il metodo find accetta tre parametri:

  • Oggetto: L’oggetto da cercare. Il tipo dovrebbe essere int, double, DateTime, string, bool.
  • Cella precedente: Cella precedente con lo stesso oggetto. Questo parametro può essere impostato su null se si cerca dall’inizio.
  • OpzioniDiRicerca: Opzioni per trovare l’oggetto richiesto.

Gli esempi seguenti utilizzano i dati del foglio di lavoro per praticare i metodi di ricerca:

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

Ricerca di dati o formule mediante FindOptions

È possibile trovare valori specificati utilizzando il metodo Cells.find(object, Cell) della collezione Cells con vari FindOptions. Tipicamente, il metodo find accetta i seguenti parametri:

  • Valore di ricerca, i dati o il valore da cercare.
  • Celle precedenti, l’ultima cella che conteneva lo stesso valore. Questo parametro può essere impostato su null durante la ricerca dall’inizio.
  • Opzioni di ricerca, le opzioni di ricerca.
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 ");
}

Ricerca delle celle contenenti un valore di stringa specificato o numero

È possibile trovare valori stringa specifici chiamando lo stesso metodo find presente nella collezione Cells con vari FindOptions.

Specifica le proprietà FindOptions.setLookInType e FindOptions.setLookAtType. Il seguente esempio di codice illustra come utilizzare queste proprietà per trovare celle con un diverso numero di stringhe all’inizio, al centro o alla fine della stringa della cella.

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

Argomenti avanzati