如何以及在哪里使用 JavaScript 通过 C++ 枚举器
Contents
[
Hide
]
枚举器是一个提供遍历容器或集合能力的对象。枚举器可以用来读取集合中的数据,但不能用来修改底层集合,而数组是一种定义了enumerator方法的接口,该方法返回一个IEnumerator接口,从而实现只读访问集合。
Aspose.Cells API提供了一堆枚举器,但本文主要讨论如下三种类型。
- 单元格枚举器
- 行枚举器
- 列枚举器
如何使用枚举器
单元格枚举器
有各种方式可以访问单元格枚举器,并且可以根据应用程序的要求使用任何这些方法。以下是返回单元格枚举器的方法。
上述所有方法都返回允许遍历初始化的单元格集合的枚举器。
在遍历单元格时,集合不应被修改(进行会导致实例化新单元格或删除现有单元格的操作)。否则,枚举器可能无法正确遍历所有单元格(某些元素可能会被重复遍历或被跳过)。
以下代码示例演示了如何实现Cells集合的IEnumerator接口。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example - Read Cell Values</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the uploaded Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access the first worksheet
const worksheet = workbook.worksheets.get(0);
const resultLines = [];
// Iterate over all cells in the worksheet
const cellsEnumerator = worksheet.cells.getEnumerator();
for (const cell of cellsEnumerator) {
console.log(`${cell.name} ${cell.value}`);
resultLines.push(`${cell.name} ${cell.value}`);
}
// Iterate over the first row's cells
const firstRowEnumerator = worksheet.cells.rows.get(0).getEnumerator();
for (const cell of firstRowEnumerator) {
console.log(`${cell.name} ${cell.value}`);
resultLines.push(`${cell.name} ${cell.value}`);
}
// Iterate over a specific range A1:B10
const rangeEnumerator = worksheet.cells.createRange("A1:B10").getEnumerator();
for (const cell of rangeEnumerator) {
console.log(`${cell.name} ${cell.value}`);
resultLines.push(`${cell.name} ${cell.value}`);
}
document.getElementById('result').innerHTML = `<pre>${resultLines.join('\n')}</pre>`;
});
</script>
</html>
行枚举器
在使用RowCollection.enumerator方法时可以访问行枚举器。以下示例代码演示了为RowCollection实现IEnumerator接口的方法。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example - List Row Indexes</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate Workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get RowCollection and iterate using index
const rows = workbook.worksheets.get(0).cells.rows;
const rowCount = rows.count;
// Traverse rows in the collection and display indexes
let output = '<p>Row indexes:</p><ul>';
for (let i = 0; i < rowCount; i++) {
const row = rows.get(i);
output += `<li>${row.index}</li>`;
console.log(row.index);
}
output += '</ul>';
document.getElementById('result').innerHTML = output;
});
</script>
</html>
列枚举器
在使用ColumnCollection.enumerator方法时可以访问列枚举器。以下示例代码演示了为ColumnCollection实现IEnumerator接口的方法。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Read Columns Indexes Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get columns collection from first worksheet
const columns = workbook.worksheets.get(0).cells.columns;
// Traverse columns using index
const count = columns.count;
let html = '<p>Columns indexes:</p><ul>';
for (let i = 0; i < count; i++) {
const col = columns.get(i);
html += `<li>${col.index}</li>`;
console.log(col.index);
}
html += '</ul>';
document.getElementById('result').innerHTML = html;
});
</script>
</html>
枚举器的使用场景
为了讨论使用枚举器的优势,让我们以一个实时案例来说明。
情景
应用程序需要遍历给定 Worksheet 中的所有单元格以读取它们的值。实现此目标的方法可能有多种,以下列出几种示例。
使用显示范围
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Read Max Display Range Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = '';
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the Excel file from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get Cells collection of first worksheet
const cells = workbook.worksheets.get(0).cells;
// Get the MaxDisplayRange
const displayRange = cells.maxDisplayRange;
// Loop over all cells in the MaxDisplayRange
let outputLines = [];
for (let row = displayRange.firstRow; row < displayRange.rowCount; row++) {
for (let col = displayRange.firstColumn; col < displayRange.columnCount; col++) {
// Read the Cell value (stringValue property)
const cell = displayRange.get(row, col);
outputLines.push(cell.stringValue);
console.log(cell.stringValue);
}
}
resultDiv.innerHTML = '<pre>' + outputLines.join('\n') + '</pre>';
});
</script>
</html>
使用MaxDataRow和MaxDataColumn
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
<meta charset="utf-8" />
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
#result { margin-top: 15px; max-height: 400px; overflow: auto; border: 1px solid #ddd; padding: 10px; }
.cell-value { padding: 2px 0; border-bottom: 1px dashed #eee; }
.error { color: red; }
</style>
</head>
<body>
<h1>Read Cells Example</h1>
<input type="file" id="fileInput" accept=".xlsx,.xls,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
function escapeHtml(text) {
if (text === null || text === undefined) return '';
return String(text)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = '';
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p class="error">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the Excel file from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get Cells collection of first worksheet
const cells2 = workbook.worksheets.get(0).cells;
const maxDataRow = cells2.maxDataRow;
const maxDataColumn = cells2.maxDataColumn;
const outputLines = [];
// Loop over all cells
for (let row = 0; row <= maxDataRow; row++) {
for (let col = 0; col <= maxDataColumn; col++) {
// Read the Cell value
const currentCell = cells2.checkCell(row, col);
if (currentCell) {
const cellText = currentCell.stringValue;
outputLines.push('<div class="cell-value">' + escapeHtml(cellText) + '</div>');
console.log(cellText);
}
}
}
if (outputLines.length === 0) {
resultDiv.innerHTML = '<p>No cell values found.</p>';
} else {
resultDiv.innerHTML = outputLines.join('');
}
});
</script>
</html>
正如您所注意到的,上述两种方法都使用了几乎相似的逻辑,即在集合中循环遍历所有单元格以读取单元格的值。对于许多原因,这可能会有问题,如下所讨论的。
- maxRow、maxDataRow、maxColumn、maxDataColumn 和 maxDisplayRange 等API需要额外时间来收集相应的统计信息。如果数据矩阵(行×列)很大,使用这些API可能会影响性能。
- 在大多数情况下,给定范围中并非所有单元格都被实例化。在这种情况下,检查矩阵中的每个单元格比仅检查初始化的单元格效率低。
- 在循环中访问单元格作为Cells row, column将导致范围内的所有单元格对象被实例化,这最终可能导致OutOfMemoryException。
结论
基于上述事实,以下是应该使用枚举器的可能情况。
- 需要只读访问单元格集合,即只需检查单元格。
- 需要遍历大量的单元格。
- 只需遍历已初始化的单元格/行/列。