Copy Shapes between Worksheets with Node.js via C++
Sometimes, you need to copy elements on a worksheet, for example, pictures, charts and other drawing objects, between worksheets. Aspose.Cells for Node.js via C++ supports this feature. Charts, images, and other objects can be copied with the highest degree of precision.
This article gives you a detailed understanding of how to copy shapes between worksheets.
Copying a Picture from one Worksheet to Another
To copy a picture from one worksheet to another, use the PictureCollection.add(number, number, number, number, Uint8Array) method as shown in the sample code below.
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, "sample_picture.xlsx");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);
// Get the Picture from the "Picture" worksheet.
const picturesource = workbook.getWorksheets().get("Picture").getPictures().get(0);
// Save Picture to Memory Stream
const ms = picturesource.getData();
// Copy the picture to the Result Worksheet
workbook.getWorksheets().get("Result").getPictures().add(picturesource.getUpperLeftRow(), picturesource.getUpperLeftColumn(), ms, picturesource.getWidthScale(), picturesource.getHeightScale());
// Save the Worksheet
workbook.save(path.join(dataDir, "PictureCopied_out.xlsx"));
Copy a Chart from one Worksheet to Another
The following code demonstrates the use of ShapeCollection.addCopy(Shape, number, number, number, number) method to copy a chart from one worksheet to another.
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, "sample_chart.xlsx");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);
// Get the Chart from the "Chart" worksheet.
const chartsource = workbook.getWorksheets().get("Chart").getCharts().get(0);
const cshape = chartsource.getChartObject();
// Copy the Chart to the Result Worksheet
workbook.getWorksheets().get("Result").getShapes().addCopy(cshape, 20, 0, 2, 0);
// Save the Worksheet
workbook.save(path.join(dataDir, "ChartCopied_out.xlsx"));
Copy Controls and Other Drawing Objects from One Worksheet to Another
To copy controls and other drawing objects, use the ShapeCollection.addCopy(Shape, number, number, number, number) method as shown in the example below.
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, "sample_control.xlsx");
// Open the template file
const workbook = new AsposeCells.Workbook(filePath);
// Get the Shapes from the "Control" worksheet.
const shape = workbook.getWorksheets().get("Control").getShapes();
// Copy the Textbox to the Result Worksheet
workbook.getWorksheets().get("Result").getShapes().addCopy(shape.get(0), 5, 0, 2, 0);
// Copy the Oval Shape to the Result Worksheet
workbook.getWorksheets().get("Result").getShapes().addCopy(shape.get(1), 10, 0, 2, 0);
// Save the Worksheet
workbook.save(path.join(dataDir, "ControlsCopied_out.xlsx"));