Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Sometimes, you need to copy rows and columns in a worksheet without copying the entire worksheet. With Aspose.Cells, it is possible to copy rows and columns within or between workbooks.
When a row (or column) is copied, the data contained in it, including formulas - with updated references - and values, comments, formatting, hidden cells, images, and other drawing objects are copied too.
Aspose.Cells provides the Cells.copyRow(Cells, number, number) method of the Cells class. This method copies all types of data including formulas, values, comments, cell formats, hidden cells, images and other drawing objects from the source row to the destination row.
The Cells.copyRow(Cells, number, number) method takes the following parameters:
Use this method to copy a row within a sheet, or to another sheet. The Cells.copyRow(Cells, number, number) method works in a similar way to Microsoft Excel. So, for example, you don’t need to set the height of the destination row explicitly, that value is copied too.
The following example shows how to copy a row in a worksheet. It uses a template Microsoft Excel file and copies the second row (complete with data, formatting, comments, images and so on) and pastes it to the 12th row in the same worksheet.
You can skip the step that gets the source row height using the Cells.getRowHeight(number, boolean, CellsUnitType) method and then sets the destination row height using the Cells.setRowHeight(number, number) method as the Cells.copyRow(Cells, number, number) method automatically takes care of the row height.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open the existing excel file.
const excelWorkbook1 = new AsposeCells.Workbook(path.join(dataDir, "book1.xls"));
// Get the first worksheet in the workbook.
const wsTemplate = excelWorkbook1.getWorksheets().get(0);
// Copy the second row with data, formattings, images and drawing objects
// To the 16th row in the worksheet.
wsTemplate.getCells().copyRow(wsTemplate.getCells(), 1, 15);
// Save the excel file.
excelWorkbook1.save(path.join(dataDir, "output.xls"));
When copying rows, it is important to note related images, charts or other drawing objects as this is the same with Microsoft Excel:
You can also copy multiple rows onto a new destination while using the Cells.copyRows(Cells, number, number, number) method which takes an additional parameter of type integer to specify the number of source rows to be copied.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "aspose-sample.xlsx");
// Create an instance of Workbook class by loading the existing spreadsheet
const workbook = new AsposeCells.Workbook(filePath);
// Get the cells collection of first worksheet
const cells = workbook.getWorksheets().get(0).getCells();
// Copy the first 3 rows to 7th row
cells.copyRows(cells, 0, 6, 3);
// Save the result on disc
workbook.save(path.join(dataDir, "output_out.xlsx"));
Aspose.Cells provides the Cells.copyColumn(Cells, number, number) method of the Cells class, this method copies all types of data, including formulas - with updated references - and values, comments, cell formats, hidden cells, images and other drawing objects from the source column to the destination column.
The Cells.copyColumn(Cells, number, number) method takes the following parameters:
Use the Cells.copyColumn(Cells, number, number) method to copy a column within a sheet or to another sheet.
This example copies a column from a worksheet and pastes it into a worksheet in another workbook.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "book1.xls");
// Create another Workbook.
const excelWorkbook1 = new AsposeCells.Workbook(filePath);
// Get the first worksheet in the book.
const ws1 = excelWorkbook1.getWorksheets().get(0);
// Copy the first column from the first worksheet of the first workbook into
// The first worksheet of the second workbook.
ws1.getCells().copyColumn(ws1.getCells(), ws1.getCells().getColumns().get(0).getIndex(), ws1.getCells().getColumns().get(2).getIndex());
// Autofit the column.
ws1.autoFitColumn(2);
// Save the excel file.
excelWorkbook1.save(path.join(dataDir, "output.xls"));
Similar to Cells.copyRows(Cells, number, number, number) method, the Aspose.Cells APIs also provide the Cells.copyColumns(Cells, number, number, number, PasteOptions) method in order to copy multiple source columns to a new location.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create an instance of Workbook class by loading the existing spreadsheet
const workbook = new AsposeCells.Workbook(path.join(dataDir, "aspose-sample.xlsx"));
// Get the first worksheet's cells collection
const worksheet = workbook.getWorksheets().get(0);
const cells = worksheet.getCells();
// Copy the first 3 columns to the 7th column
cells.copyColumns(cells, 0, 6, 3);
// Save the result on disc
workbook.save(path.join(dataDir, "output_out.xlsx"));
Aspose.Cells now provides PasteOptions while using functions Cells.copyRows(Cells, number, number, number) and Cells.copyColumns(Cells, number, number, number, PasteOptions). It allows to set appropriate paste option similar to Excel.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const sourceDir = path.join(__dirname, "data");
const outputDir = path.join(__dirname, "output");
// Load sample excel file
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "sampleChangeChartDataSource.xlsx"));
// Access the first sheet which contains chart
const source = workbook.getWorksheets().get(0);
// Add another sheet named DestSheet
const destination = workbook.getWorksheets().add("DestSheet");
// Set CopyOptions.ReferToDestinationSheet to true
const options = new AsposeCells.CopyOptions();
options.setReferToDestinationSheet(true);
// Set PasteOptions
const pasteOptions = new AsposeCells.PasteOptions();
pasteOptions.setPasteType(AsposeCells.PasteType.Values);
pasteOptions.setOnlyVisibleCells(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.getCells().copyRows(source.getCells(), 0, 0, source.getCells().getMaxDisplayRange().getRowCount(), options, pasteOptions);
// Save workbook in xlsx format
workbook.save(path.join(outputDir, "outputChangeChartDataSource.xlsx"), AsposeCells.SaveFormat.Xlsx);
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.