Benannte Bereiche mit JavaScript via C++ löschen
Einführung
Wenn es zu viele definierte Namen oder benannte Bereiche in den Excel-Dateien gibt, müssen wir einige davon löschen, da sie nicht mehr referenziert werden.
Benannten Bereich in MS Excel entfernen
Um einen benannten Bereich aus Excel zu entfernen, können Sie folgende Schritte befolgen:
- Öffnen Sie Microsoft Excel und die Arbeitsmappe, die den benannten Bereich enthält.
- Gehen Sie zum Register “Formeln” in der Excel-Menüleiste.
- Klicken Sie auf die Schaltfläche “Namensmanager” in der Gruppe “Definierte Namen”. Dadurch wird das Dialogfeld Namensmanager geöffnet.
- Wählen Sie im Dialogfeld Namensmanager den benannten Bereich aus, den Sie entfernen möchten.
- Klicken Sie auf die Schaltfläche “Löschen”. Bestätigen Sie die Löschung beim Auffordern.
- Klicken Sie auf die Schaltfläche “Schließen”, um das Dialogfeld Namensmanager zu schließen.
- Speichern Sie die Arbeitsmappe, um die Änderungen beizubehalten.
Benannten Bereich mit Aspose.Cells for JavaScript via C++ löschen
Mit Aspose.Cells for JavaScript via C++ können Sie benannte Bereiche oder definierte Namen anhand von Text oder Index in der Liste entfernen.
<!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>
Hinweis: Wenn der definierte Name sich auf Formeln bezieht, kann er nicht entfernt werden. Wir können nur die Formel des definierten Namens entfernen.
Einige benannte Bereiche entfernen
Beim Entfernen eines definierten Namens müssen wir überprüfen, ob er von allen Formeln in der Datei referenziert wird. Um die Leistung beim Entfernen benannter Bereiche zu verbessern, können wir einige gleichzeitig entfernen.
<!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>
Doppelte definierte Namen entfernen
Einige Excel-Dateien sind beschädigt, weil einige definierte Namen doppelt vorhanden sind. Daher können wir diese doppelten Namen entfernen, um die Datei zu reparieren.
<!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>