Optimización del uso de memoria al trabajar con archivos grandes con conjuntos de datos extensos con JavaScript a través de C++
Cuando construyes un libro con grandes conjuntos de datos o lees un archivo de Microsoft Excel grande, la cantidad total de RAM que el proceso utilizará siempre es una preocupación. Existen medidas que se pueden adaptar para afrontar el desafío. Aspose.Cells for JavaScript a través de C++ ofrece algunas opciones relevantes y llamadas a la API para reducir, disminuir y optimizar el uso de memoria. Además, puede ayudar al proceso a trabajar de manera más eficiente y a funcionar más rápido.
Utilice la opción MemorySetting.MemoryPreference para optimizar el uso de memoria para los datos de las celdas y disminuir el costo total de memoria. Al construir un conjunto de datos grande para las celdas, se puede ahorrar una cierta cantidad de memoria en comparación con el uso de la configuración predeterminada (MemorySetting.Normal).
Optimización de memoria
Lectura de archivos Excel grandes
El siguiente ejemplo muestra cómo leer un archivo grande de Microsoft Excel en modo optimizado.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - LoadOptions MemorySetting</title>
</head>
<body>
<h1>LoadOptions MemorySetting 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, LoadOptions, MemorySetting, SaveFormat, Utils } = 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();
// Specify the LoadOptions
const opt = new LoadOptions();
// Set the memory preferences (converted from setMemorySetting)
opt.memorySetting = MemorySetting.MemoryPreference;
// Instantiate the Workbook - load the big Excel file with options
const workbook = new Workbook(new Uint8Array(arrayBuffer), opt);
// Save the workbook to XLSX and provide a download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook loaded with MemoryPreference. Click the download link to get the file.</p>';
});
</script>
</html>
Escribiendo Archivos de Excel Grandes
El siguiente ejemplo muestra cómo escribir un conjunto de datos grande en una hoja de trabajo en modo optimizado.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Memory Setting Example</title>
</head>
<body>
<h1>Aspose.Cells Memory Setting 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, Worksheet, Cell, Utils } = 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) {
// If no file is selected, create a new workbook (similar to new AsposeCells.Workbook() in Node)
// and proceed to set memory settings and populate sheets.
}
// Load workbook from file if provided, otherwise create empty workbook
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Set the memory preferences on the workbook settings
workbook.settings.memorySetting = AsposeCells.MemorySetting.MemoryPreference;
// Note: The memory settings also would not work for the default sheet i.e., "Sheet1" etc. automatically created by the Workbook
// To change the memory setting of existing sheets, change memory setting for them manually:
let cells = workbook.worksheets.get(0).cells;
cells.memorySetting = AsposeCells.MemorySetting.MemoryPreference;
// Input large dataset into the cells of the worksheet.
// Your code goes here.
// Example: populate a few cells to demonstrate
const firstCell = cells.get(0, 0);
firstCell.value = "Sample Data 1";
cells.get(1, 0).value = "Sample Data 2";
// Get cells of the newly created Worksheet "Sheet2" whose memory setting is same with the one defined in WorkbookSettings:
const sheet2 = workbook.worksheets.add("Sheet2");
const cells2 = sheet2.cells;
// .........
// Input large dataset into the cells of the worksheet.
// Your code goes here.
// Example: populate a few cells in Sheet2
cells2.get(0, 0).value = "Sheet2 Data 1";
cells2.get(1, 0).value = "Sheet2 Data 2";
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Result';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Precaución
La opción predeterminada, MemorySetting.Normal, se aplica para todas las versiones. Para algunas situaciones, como construir un libro con un conjunto de datos grande para celdas, la opción MemorySetting.MemoryPreference puede optimizar el uso de memoria y disminuir el costo de memoria para la aplicación. Sin embargo, esta opción puede degradar el rendimiento en algunos casos especiales, como se indica a continuación.
- Acceder a las celdas aleatoriamente y de forma repetida: La secuencia más eficiente para acceder a la colección de celdas es celda por celda en una fila, y luego fila por fila. Especialmente, si accede a filas/celdas mediante el enumerador obtenido de Cells, RowCollection, y Row, el rendimiento se maximizará con MemorySetting.MemoryPreference.
- Insertar y eliminar celdas y filas: Tenga en cuenta que si hay muchas operaciones de inserción/eliminación para celdas/filas, el deterioro del rendimiento será notable en modo MemoryPreference en comparación con el modo Normal.
- Operar en diferentes tipos de celda: Si la mayoría de las celdas contienen valores de cadena o fórmulas, el costo de memoria será igual al modo Normal, pero si hay muchas celdas vacías, o los valores de las celdas son numéricos, booleanos y otros, la opción MemorySetting.MemoryPreference dará mejor rendimiento.