Hitta eller söka data
Att hitta celler som innehåller specificerad data
Använda Microsoft Excel
Microsoft Excel tillåter användare att hitta celler i ett kalkblad som innehåller specificerad data. Om du väljer Redigera från Sök-menyn i Microsoft Excel, ser du en dialogruta där du kan ange sökvärdet.
Här letar vi efter värdet “Apelsiner”. Aspose.Cells tillåter också utvecklare att hitta celler i kalkylarket som innehåller specificerade värden.
Användning av Aspose.Cells for Node.js via C++
Aspose.Cells tillhandahåller en klass, Workbook, som representerar en Microsoft Excel-fil. Klassen Workbook innehåller en Workbook.getWorksheets()-samling som ger åtkomst till varje kalkblad i Excel-filen. Ett kalkblad representeras av klassen Worksheet. Klassen Worksheet tillhandahåller en getCells()-samling som representerar alla celler i kalkbladet. Samlingen Cells ger flera metoder för att hitta celler som innehåller användarspecifik data. Några av dessa metoder diskuteras nedan i mer detalj.
Att hitta celler som innehåller en formel
Utvecklare kan hitta en angiven formel i kalkbladet genom att anropa metoden Cells i find-samlingen. Vanligtvis tar metoden find tre parametrar:
- Objekt: Objektet att söka efter. Typen ska vara int, double, DateTime, string, bool.
- Föregående cell: Föregående cell med samma objekt. Denna parameter kan sättas till null om sökningen börjar från början.
- FindOptions: Inställningar för att hitta det erforderliga objektet.
Nedan används exempel på kalkylarksdata för att öva på hittametoder.
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()); |
Hitta data eller formler med FindOptions
Det är möjligt att hitta specificerade värden med hjälp av Cells-samlingen och Cells.find(object, Cell)-metoden med olika FindOptions. Vanligtvis accepterar find-metoden följande parametrar:
- Sökvärde, data eller värde som ska sökas efter.
- Föregående cell, den sista cellen som innehöll samma värde. Denna parameter kan ställas in till null när du söker från början.
- Hitta alternativ, hitta alternativ.
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 "); | |
} |
Hitta celler som innehåller specifierade strängvärden eller nummer
Det är möjligt att hitta angivna strängvärden genom att anropa samma find-metod som finns i Cells-samlingen med olika FindOptions.
Ange egenskaperna FindOptions.setLookInType och FindOptions.setLookAtType. Följande kodexempel visar hur man använder dessa egenskaper för att hitta celler med olika antal strängar i början, mitten eller slutet av cellens text.
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 "); | |
} |