Копирование строк и столбцов с помощью JavaScript через C++
Введение
Иногда вам нужно скопировать строки и столбцы в рабочем листе без копирования всего листа. С помощью Aspose.Cells это возможно скопировать строки и столбцы внутри или между книгами.
При копировании строки (или столбца) копируются также содержащиеся в нем данные, включая формулы - с обновленными ссылками - и значения, комментарии, форматирование, скрытые ячейки, изображения и другие объекты рисования.
Как скопировать строки и столбцы с помощью Microsoft Excel
- Выберите строку или колонку, которую вы хотите скопировать.
- Чтобы скопировать строки или колонки, нажмите Копировать на панели Стандартные функции или нажмите CTRL+C.
- Выберите строку или колонку ниже или справа от места, куда вы хотите скопировать ваш выбор.
- При копировании строк или колонок нажмите Скопированные ячейки на меню Вставка.
Как вставить строки и столбцы с использованием опций вставки в программе Microsoft Excel
- Выберите ячейки, содержащие данные или другие параметры, которые вы хотите скопировать.
- На вкладке “Главная” нажмите Копировать.
- Щелкните первую ячейку в области, куда вы хотите вставить скопированное.
- На вкладке “Главная” щелкните стрелку рядом с Вставить, затем выберите Специальная вставка.
- Выберите нужные опции.
** Как копировать строки и столбцы с помощью Aspose.Cells for JavaScript через C++**
Как скопировать отдельные строки
Aspose.Cells предоставляет метод Cells.copyRow(Cells, number, number) класса Cells. Этот метод копирует все типы данных, включая формулы, значения, комментарии, форматы ячеек, скрытые ячейки, изображения и другие объекты рисования из исходной строки в целевую.
Метод Cells.copyRow(Cells, number, number) принимает следующие параметры:
- исходный объект Cells,
- индекс исходной строки, и
- индекс строки назначения.
Используйте этот метод для копирования строки внутри листа или в другой лист. Метод Cells.copyRow(Cells, number, number) работает аналогично в Microsoft Excel. Например, вам не нужно явно задавать высоту целевой строки — это значение также копируется.
Следующий пример показывает, как скопировать строку в листе. Используется шаблонный файл Excel, копируется вторая строка (со всеми данными, форматами, комментариями, изображениями и т. д.) и вставляется на 12-ую строку того же листа.
Можно пропустить шаг получения высоты исходной строки, используя метод Cells.rowHeight(number, boolean, CellsUnitType), а затем установить высоту целевой строки методом Cells.rowHeight(number, number), потому что метод Cells.copyRow(Cells, number, number) автоматически позаботится о высоте строки.
<!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>
При копировании строк важно учитывать связанные изображения, диаграммы или другие объекты рисования, так же как и в Microsoft Excel:
- Если индекс исходной строки равен 5, изображение, диаграмма и т. д. копируются, если они содержатся в трех строках (начальный индекс строки равен 4, а конечный индекс строки равен 6).
- Существующие изображения, диаграммы и т. д. в целевой строке не будут удалены.
Как скопировать несколько строк
Также можно копировать несколько строк на новую позицию, используя метод Cells.copyRows(Cells, number, number, number), который принимает дополнительный целочисленный параметр, указывающий количество копируемых исходных строк.
<!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>
Как копировать столбцы
Aspose.Cells предоставляет метод Cells.copyColumn(Cells, number, number) класса Cells, который копирует все типы данных, включая формулы — с обновленными ссылками — и значения, комментарии, форматы ячеек, скрытые ячейки, изображения и другие объекты рисования из исходного столбца в целевой.
Метод Cells.copyColumn(Cells, number, number) принимает следующие параметры:
- исходный объект Cells,
- индекс исходного столбца, и
- индекс столбца назначения.
Используйте метод Cells.copyColumn(Cells, number, number) для копирования столбца внутри листа или в другой лист.
В этом примере копируется столбец из листа и вставляется в лист другой книги.
<!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>
Как скопировать несколько столбцов
Аналогично методу Cells.copyRows(Cells, number, number, number), API Aspose.Cells также предоставляет метод Cells.copyColumns(Cells, number, number, number, PasteOptions) для копирования нескольких исходных столбцов в новое место.
<!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>
Как вставить строки и столбцы с параметрами вставки
Теперь Aspose.Cells предоставляет PasteOptions при использовании функций Cells.copyRows(Cells, number, number, number) и Cells.copyColumns(Cells, number, number, number, PasteOptions). Это позволяет установить соответствующий параметр вставки, аналогичный 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>