Copia Righe e Colonne con JavaScript tramite C++
Introduzione
A volte è necessario copiare righe e colonne in un foglio di lavoro senza copiare l’intero foglio di lavoro. Con Aspose.Cells, è possibile copiare righe e colonne all’interno o tra i fogli di lavoro.
Quando viene copiata una riga (o colonna), vengono copiati anche i dati contenuti al suo interno, inclusi formule - con riferimenti aggiornati - e valori, commenti, formattazione, celle nascoste, immagini e altri oggetti grafici.
Come copiare righe e colonne con Microsoft Excel
- Seleziona la riga o la colonna che desideri copiare.
- Per copiare righe o colonne, fai clic su Copia sulla barra degli strumenti Standard, oppure premi CTRL+C.
- Seleziona una riga o una colonna sotto o alla destra di dove desideri copiare la tua selezione.
- Quando stai copiando righe o colonne, fai clic su Celle Copiate nel menu Inserisci.
Come incollare righe e colonne utilizzando le opzioni di incolla con Microsoft Excel
- Seleziona le celle che contengono i dati o altri attributi che desideri copiare.
- Nella scheda Home, fai clic su Copia.
- Fai clic sulla prima cella nell’area in cui desideri incollare quello che hai copiato.
- Nella scheda Home, fai clic sulla freccia accanto a Incolla, quindi seleziona Incolla speciale.
- Seleziona le opzioni desiderate.
Come Copiare Righe e Colonne Usando Aspose.Cells for JavaScript via C++
Come copiare singole righe
Aspose.Cells fornisce il metodo Cells.copyRow(Cells, number, number) della classe Cells. Questo metodo copia tutti i tipi di dati inclusi formule, valori, commenti, formati di cella, celle nascoste, immagini e altri oggetti di disegno dalla riga di origine alla riga di destinazione.
Il metodo Cells.copyRow(Cells, number, number) accetta i seguenti parametri:
- l’oggetto Cells di origine,
- l’indice della riga di origine e
- l’indice della riga di destinazione.
Usa questo metodo per copiare una riga all’interno di un foglio o in un altro foglio. Il metodo Cells.copyRow(Cells, number, number) funziona in modo simile a Microsoft Excel. Quindi, per esempio, non è necessario impostare l’altezza della riga di destinazione esplicitamente, quel valore viene copiato anch’esso.
Il seguente esempio mostra come copiare una riga in un foglio di lavoro. Usa un file modello di Microsoft Excel e copia la seconda riga (completa di dati, formattazione, commenti, immagini e altri) e la incolla alla 12a riga nello stesso foglio.
Puoi saltare il passaggio che ottiene l’altezza della riga di origine usando il metodo Cells.rowHeight(number, boolean, CellsUnitType) e poi impostare l’altezza della riga di destinazione usando il metodo Cells.rowHeight(number, number), poiché il metodo Cells.copyRow(Cells, number, number) si occupa automaticamente dell’altezza della riga.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Copy Row</title>
</head>
<body>
<h1>Copy Row 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 using the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get the first worksheet in the workbook.
const wsTemplate = workbook.worksheets.get(0);
// Copy the second row (index 1) with data, formatting, images and drawing objects
// To the 16th row (index 15) in the worksheet.
wsTemplate.cells.copyRow(wsTemplate.cells, 1, 15);
// Save the excel file in Excel97To2003 format (.xls)
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Row copied successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Quando si copiano le righe, è importante notare immagini correlate, grafici o altri oggetti disegnati poiché è lo stesso con Microsoft Excel:
- Se l’indice della riga di origine è 5, l’immagine, il grafico, ecc., vengono copiati se sono contenuti nelle tre righe (l’indice della riga di inizio è 4 e l’indice della riga di fine è 6).
- Le immagini, i grafici, ecc., esistenti nella riga di destinazione non verranno rimossi.
Come Copiare Più Righe
Puoi anche copiare più righe su una nuova destinazione usando il metodo Cells.copyRows(Cells, number, number, number) che accetta un parametro aggiuntivo di tipo intero per specificare il numero di righe di origine da copiare.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells 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 workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get the cells collection of first worksheet
const cells = workbook.worksheets.get(0).cells;
// Copy the first 3 rows to 7th row (indexes are zero-based)
cells.copyRows(cells, 0, 6, 3);
// Save the result and provide 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_out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Rows copied successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Come Copiare Colonne
Aspose.Cells fornisce il metodo Cells.copyColumn(Cells, number, number) della classe Cells, che copia tutti i tipi di dati, incluse formule - con riferimenti aggiornati - e valori, commenti, formati di cella, celle nascoste, immagini e altri oggetti di disegno dalla colonna di origine a quella di destinazione.
Il metodo Cells.copyColumn(Cells, number, number) accetta i seguenti parametri:
- l’oggetto Cells di origine,
- l’indice della colonna di origine e
- l’indice della colonna di destinazione.
Usa il metodo Cells.copyColumn(Cells, number, number) per copiare una colonna all’interno di un foglio o in un altro foglio.
Questo esempio copia una colonna da un foglio di lavoro e la incolla in un foglio di lavoro in un altro documento.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
<meta charset="utf-8" />
</head>
<body>
<h1>Copy Column 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');
const resultDiv = document.getElementById('result');
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 using the uploaded file
const excelWorkbook1 = new Workbook(new Uint8Array(arrayBuffer));
// Get the first worksheet in the book.
const ws1 = excelWorkbook1.worksheets.get(0);
// Copy the first column from the first worksheet into the third column of the same worksheet.
const cells = ws1.cells;
cells.copyColumn(cells, cells.columns.get(0).index, cells.columns.get(2).index);
// Autofit the column.
ws1.autoFitColumn(2);
// Save the excel file (Excel97To2003 format for .xls)
const outputData = excelWorkbook1.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultDiv.innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Come Copiare Più Colonne
Simile al metodo Cells.copyRows(Cells, number, number, number), le API Aspose.Cells forniscono anche il metodo Cells.copyColumns(Cells, number, number, number, PasteOptions) per copiare più colonne di origine in una nuova posizione.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells 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();
// Create an instance of Workbook class by loading the existing spreadsheet
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get the first worksheet's cells collection
const worksheet = workbook.worksheets.get(0);
const cells = worksheet.cells;
// Copy the first 3 columns to the 7th column
cells.copyColumns(cells, 0, 6, 3);
// Save the result 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_out.xlsx';
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>
Come Incollare Righe e Colonne con Opzioni di Incollaggio
Aspose.Cells fornisce ora PasteOptions utilizzando le funzioni Cells.copyRows(Cells, number, number, number) e Cells.copyColumns(Cells, number, number, number, PasteOptions). Consente di impostare l’opzione di incollaggio appropriata simile a Excel.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Change Chart Data Source</title>
</head>
<body>
<h1>Change Chart Data Source Example</h1>
<p>Select an Excel file (sampleChangeChartDataSource.xlsx) from your local machine.</p>
<input type="file" id="fileInput" accept=".xls,.xlsx,.xlsm,.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, PasteType, CopyOptions, PasteOptions } = 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 sheet which contains chart
const source = workbook.worksheets.get(0);
// Add another sheet named DestSheet
const destination = workbook.worksheets.add("DestSheet");
// Set CopyOptions.ReferToDestinationSheet to true
const options = new CopyOptions();
options.referToDestinationSheet = true;
// Set PasteOptions
const pasteOptions = new PasteOptions();
pasteOptions.pasteType = PasteType.Values;
pasteOptions.onlyVisibleCells = true;
// Copy all the rows of source worksheet to destination worksheet which includes chart as well
// The chart data source will now refer to DestSheet
destination.cells.copyRows(source.cells, 0, 0, source.cells.maxDisplayRange.rowCount, options, pasteOptions);
// Save workbook in xlsx format and provide 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 = 'outputChangeChartDataSource.xlsx';
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>