Eliminar rangos con nombre usando JavaScript mediante C++
Introducción
Si hay demasiados nombres definidos o rangos con nombre en los archivos de Excel, debemos eliminar algunos para que no se vuelvan a hacer referencia.
Eliminar rango con nombre en MS Excel
Para eliminar un rango con nombre en Excel, siga estos pasos:
- Abra Microsoft Excel y abra el libro que contiene el rango con nombre.
- Vaya a la pestaña “Fórmulas” en la cinta de Excel.
- Haga clic en el botón “Administrador de nombres” en el grupo “Nombres definidos”. Esto abrirá la ventana de diálogo del Administrador de nombres.
- En la ventana de diálogo del Administrador de nombres, seleccione el rango con nombre que desea eliminar.
- Haga clic en el botón “Eliminar”. Confirme la eliminación cuando se lo soliciten.
- Haz clic en el botón “Cerrar” para cerrar el cuadro de diálogo del Administrador de nombres.
- Guarda el libro para guardar los cambios.
Eliminar rango con nombre usando Aspose.Cells for JavaScript mediante C++
Con Aspose.Cells for JavaScript vía C++, puedes eliminar rangos con nombre o nombres definidos por texto o índice en la lista.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells - Remove Named Ranges 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, 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();
// Instantiate a Workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get all the worksheets in the book.
const worksheets = workbook.worksheets;
// Deleted a named range by text.
worksheets.names.remove("NamedRange");
// Deleted a defined name by index. Ensure to check the count before removal.
if (worksheets.names.count > 0) {
worksheets.names.removeAt(0);
}
// Save the workbook to retain the changes.
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Book2.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Named ranges removed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Nota: si el nombre definido es referido por fórmulas, no se puede eliminar. Solo se puede eliminar la fórmula del nombre definido.
Eliminar algunos rangos con nombre
Cuando eliminamos un nombre definido, debemos verificar si está referido por todas las fórmulas en el archivo. Para mejorar el rendimiento al eliminar rangos con nombre, podemos eliminarlos algunos juntos.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Remove Named Ranges 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();
// Instantiate a new Workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get all the worksheets in the book.
const worksheets = workbook.worksheets;
// Delete some defined names.
worksheets.names.remove(["NamedRange1", "NamedRange2"]);
// Save the workbook to retain the changes.
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Book2.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Named ranges removed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Eliminar nombres definidos duplicados
Algunos archivos de Excel se corrompen porque algunos nombres definidos están duplicados. Por lo tanto, podemos eliminar estos nombres duplicados para reparar el archivo.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells - Remove Duplicate Defined Names</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, 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();
// Instantiate a new Workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get all the worksheets in the book.
const worksheets = workbook.worksheets;
// Deleted some defined names.
worksheets.names.removeDuplicateNames();
// Save the workbook to retain the changes.
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Book2.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Duplicate defined names removed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>