Erstellen und Kopieren von benannten Bereichen mit JavaScript via C++
Einführung
Normalerweise werden Spalten- und Zeilenlabels verwendet, um einzelne Zellen zu referenzieren. Es ist möglich, beschreibende Namen zu erstellen, um Zellen, Zellbereiche, Formeln oder Konstanten zu repräsentieren. Das Wort Name kann sich auf eine Zeichenkette beziehen, die eine Zelle, einen Zellbereich, eine Formel oder einen Konstantenwert darstellt. Das Zuweisen eines Namens zu einem Bereich bedeutet, dass dieser Zellbereich anhand seines Namens referenziert werden kann. Verwenden Sie leicht verständliche Namen, wie Produkte, um schwer verständliche Bereiche wie Verkäufe!C20:C30 zu repräsentieren. Labels können in Formeln verwendet werden, die sich auf Daten im selben Arbeitsblatt beziehen; wenn Sie einen Bereich auf einem anderen Arbeitsblatt darstellen möchten, können Sie einen Namen verwenden. Benannte Bereiche gehören zu den leistungsstärksten Funktionen von Microsoft Excel, insbesondere wenn sie als Quellbereich für Listensteuerungen, Pivot-Tabellen, Diagramme usw. verwendet werden.
Arbeiten mit benannten Bereich unter Verwendung von Microsoft Excel
Benannte Bereiche erstellen
Die folgenden Schritte beschreiben, wie man eine Zelle oder einen Zellbereich mit MS Excel benennt. Diese Methode gilt für Microsoft Office Excel 2003, Microsoft Excel 97, 2000 und 2002.
- Wählen Sie die Zelle oder den Zellbereich aus, den Sie benennen möchten.
- Klicken Sie auf das Namensfeld am linken Ende der Formelleiste.
- Geben Sie den Namen für die Zellen ein.
- Drücken Sie ENTER.
Arbeiten mit benannten Bereich unter Verwendung von Aspose.Cells
Hier verwenden wir die Aspose.Cells API, um die Aufgabe zu erledigen.
Aspose.Cells bietet eine Klasse Workbook, die eine Microsoft Excel-Datei repräsentiert. Die Klasse Workbook enthält eine worksheets-Sammlung, die den Zugriff auf jede Arbeitsmappe in einer Excel-Datei ermöglicht. Eine Arbeitsmappe wird durch die Klasse Worksheet repräsentiert. Die Klasse Worksheet bietet eine cells-Sammlung.
Benannten Bereich erstellen
Es ist möglich, einen benannten Bereich zu erstellen, indem die überladene createRange(string, string)-Methode der cells-Sammlung aufgerufen wird. Eine typische Version der createRange(string)-Methode nimmt die folgenden Parameter:
- Name der oberen linken Zelle, Name der oberen linken Zelle im Bereich.
- Name der unteren rechten Zelle, Name der unteren rechten Zelle im Bereich.
Wenn die Methode createRange(string) aufgerufen wird, wird der neu erstellte Bereich als Instanz der Klasse Range zurückgegeben. Verwenden Sie dieses Objekt Range, um den benannten Bereich zu konfigurieren. Setzen Sie beispielsweise den Namen des Bereichs mit der name-Eigenschaft. Das folgende Beispiel zeigt, wie man einen benannten Bereich von Zellen erstellt, der sich über B4:G14 erstreckt.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Create Named 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, 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();
// Instantiating a Workbook object by opening the Excel file from the file input
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.worksheets.get(0);
// Creating a named range
const range = worksheet.cells.createRange("B4", "G14");
// Setting the name of the named range
range.name = "TestRange";
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Named range created successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Daten in die Zellen des benannten Bereichs eingeben
Sie können Daten in die einzelnen Zellen eines Bereichs einfügen, indem Sie dem Muster folgen
- JavaScript: Range[row,Spalte]
Angenommen, Sie haben einen benannten Bereich von Zellen, der sich über A1:C4 erstreckt. Die Matrix enthält 4 * 3 = 12 Zellen. Die einzelnen Bereichszellen sind sequentiell angeordnet: Bereich[0,0], Bereich[0,1], Bereich[0,2], Bereich[1,0], Bereich[1,1], Bereich[1,2], Bereich[2,0], Bereich[2,1], Bereich[2,2], Bereich[3,0], Bereich[3,1], Bereich[3,2].
Verwenden Sie die folgenden Eigenschaften, um die Zellen im Bereich zu identifizieren:
- firstRow gibt den Index der ersten Zeile im benannten Bereich zurück.
- firstColumn gibt den Index der ersten Spalte im benannten Bereich zurück.
- rowCount gibt die Gesamtzahl der Zeilen im benannten Bereich zurück.
- columnCount gibt die Gesamtzahl der Spalten im benannten Bereich zurück.
Das folgende Beispiel zeigt, wie einige Werte in die Zellen eines bestimmten Bereichs eingegeben werden.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Create Named 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, 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');
// Instantiate or load workbook
let workbook;
if (fileInput.files && fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Get the first worksheet in the workbook.
const worksheet1 = workbook.worksheets.get(0);
// Create a range of cells based on H1:J4.
const range = worksheet1.cells.createRange("H1", "J4");
// Name the range.
range.name = "MyRange";
// Input some data into cells in the range.
range.get(0, 0).value = "USA";
range.get(0, 1).value = "SA";
range.get(0, 2).value = "Israel";
range.get(1, 0).value = "UK";
range.get(1, 1).value = "AUS";
range.get(1, 2).value = "Canada";
range.get(2, 0).value = "France";
range.get(2, 1).value = "India";
range.get(2, 2).value = "Egypt";
range.get(3, 0).value = "China";
range.get(3, 1).value = "Philipine";
range.get(3, 2).value = "Brazil";
// Save the excel file.
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'rangecells.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and range populated successfully! Click the download link to get the file.</p>';
});
</script>
</html>
Zellen im benannten Bereich identifizieren
Sie können Daten in die einzelnen Zellen eines Bereichs einfügen, indem Sie dem Muster folgen:
- JavaScript: Range[row,Spalte]
Wenn Sie einen benannten Bereich haben, der A1:C4 umfasst. Die Matrix umfasst 4 * 3 = 12 Zellen. Die einzelnen Bereichszellen sind sequentiell angeordnet: Bereich[0,0], Bereich[0,1], Bereich[0,2], Bereich[1,0] ,Bereich[1,1], Bereich[1,2], Bereich[2,0], Bereich[2,1], Bereich[2,2], Bereich[3,0], Bereich[3,1], Bereich[3,2].
Verwenden Sie die folgenden Eigenschaften, um die Zellen im Bereich zu identifizieren:
- firstRow gibt den Index der ersten Zeile im benannten Bereich zurück.
- firstColumn gibt den Index der ersten Spalte im benannten Bereich zurück.
- rowCount gibt die Gesamtzahl der Zeilen im benannten Bereich zurück.
- columnCount gibt die Gesamtzahl der Spalten im benannten Bereich zurück.
Das folgende Beispiel zeigt, wie einige Werte in die Zellen eines bestimmten Bereichs eingegeben werden.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Get Named Range</title>
</head>
<body>
<h1>Get Named 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, 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) {
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 the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Getting the specified named range
const range = workbook.worksheets.rangeByName("TestRange");
if (!range) {
document.getElementById('result').innerHTML = '<p style="color: red;">Named range "TestRange" not found.</p>';
return;
}
// Identify range cells and display properties
const firstRow = range.firstRow;
const firstColumn = range.firstColumn;
const rowCount = range.rowCount;
const columnCount = range.columnCount;
const html = [
`<p>First Row : ${firstRow}</p>`,
`<p>First Column : ${firstColumn}</p>`,
`<p>Row Count : ${rowCount}</p>`,
`<p>Column Count : ${columnCount}</p>`
].join('');
document.getElementById('result').innerHTML = html;
});
</script>
</html>
Zugriff auf benannte Bereiche
Auf einen bestimmten benannten Bereich zugreifen
Rufen Sie die Methode rangeByName(string) der worksheets-Sammlung auf, um einen Bereich nach dem angegebenen Namen zu erhalten. Eine typische rangeByName(string)-Methode nimmt den Namen des benannten Bereichs entgegen und gibt den angegebenen benannten Bereich als Instanz der Range-Klasse zurück. Das folgende Beispiel zeigt, wie auf einen bestimmten Bereich nach seinem Namen zugegriffen wird.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Named Range Example</title>
</head>
<body>
<h1>Get Named 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, 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();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Getting the specified named range
const worksheets = workbook.worksheets;
const range = worksheets.rangeByName("TestRange");
if (range !== null) {
document.getElementById('result').innerHTML = `<p>Named Range : ${range.refersTo}</p>`;
} else {
document.getElementById('result').innerHTML = '<p style="color: red;">Named range "TestRange" not found.</p>';
}
});
</script>
</html>
Zugriff auf alle benannten Bereiche in einer Arbeitsmappe
Rufen Sie die worksheet-Sammlung auf, um die namedRanges()-Methode aufzurufen und alle benannten Bereiche in einer Tabelle zu erhalten. Die namedRanges()-Methode gibt ein Array aller benannten Bereiche in der worksheets-Sammlung zurück.
Das folgende Beispiel zeigt, wie auf alle benannten Bereiche in einer Arbeitsmappe zugegriffen wird.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Named Ranges</title>
</head>
<body>
<h1>Get 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();
// Instantiating a Workbook object by opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Getting all named ranges
const ranges = workbook.worksheets.namedRanges;
if (ranges) {
// Some collections expose 'count', others may expose 'length'
const total = (typeof ranges.count !== 'undefined') ? ranges.count : ranges.length;
document.getElementById('result').innerHTML = `<p style="color: green;">Total Number of Named Ranges: ${total}</p>`;
} else {
document.getElementById('result').innerHTML = '<p style="color: orange;">No named ranges found.</p>';
}
});
</script>
</html>
Benannte Bereiche kopieren
Aspose.Cells bietet die range.copy(Range, PasteOptions)-Methode zum Kopieren eines Zellenbereichs mit Formatierung in einen anderen Bereich.
Das folgende Beispiel zeigt, wie ein Quellbereich von Zellen in einen anderen benannten Bereich kopiert wird.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Copy Ranges</title>
</head>
<body>
<h1>Copy Ranges Example</h1>
<p>Select an Excel file to modify, or leave empty to create a new workbook.</p>
<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, BorderType, CellBorderType, Color } = 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');
// Instantiate a new Workbook or load from selected file
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();
}
// Get all the worksheets in the book.
const worksheets = workbook.worksheets;
// Get the first worksheet in the worksheets collection.
const worksheet = workbook.worksheets.get(0);
// Create a range of cells.
const range1 = worksheet.cells.createRange("E12", "I12");
// Name the range.
range1.name = "MyRange";
// Set the outline border to the range.
range1.outlineBorder = { borderType: BorderType.TopBorder, style: CellBorderType.Medium, color: new Color(0, 0, 128) };
range1.outlineBorder = { borderType: BorderType.BottomBorder, style: CellBorderType.Medium, color: new Color(0, 0, 128) };
range1.outlineBorder = { borderType: BorderType.LeftBorder, style: CellBorderType.Medium, color: new Color(0, 0, 128) };
range1.outlineBorder = { borderType: BorderType.RightBorder, style: CellBorderType.Medium, color: new Color(0, 0, 128) };
// Input some data with some formattings into a few cells in the range.
range1.get(0, 0).putValue("Test");
range1.get(0, 4).putValue("123");
// Create another range of cells.
const range2 = worksheet.cells.createRange("B3", "F3");
// Name the range.
range2.name = "testrange";
// Copy the first range into second range.
range2.copy(range1);
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'copyranges.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>