行や範囲をコピーしながらチャートのデータソースを宛先ワークシートに変更する方法(Node.js経由でC++)

可能な使用シナリオ

チャートを含む行や範囲を新しいワークシートにコピーするとき、チャートのデータソースは変わりません。たとえば、チャートのデータソースが=Sheet1!$A$1:$B$4である場合、行または範囲を新しいワークシートにコピーしても、データソースは=Sheet1!$A$1:$B$4のままです。これはMicrosoft Excelでも同じ動作です。しかし、データソースを新しい宛先ワークシートにしたい場合は、CopyOptions.getReferToDestinationSheet()プロパティをtrueに設定し、Cells.copyRows(Cells, number, number, number)メソッドを呼び出すことで実現可能です。たとえば、宛先シートがDestSheetの場合、チャートのデータソースは=Sheet1!$A$1:$B$4から=DestSheet!$A$1:$B$4に変わります。

行や範囲をコピーする際に、チャートのデータソースを宛先ワークシートに変更する

次のサンプルコードは、チャートを含む行や範囲を新しいワークシートにコピーする際に、CopyOptions.getReferToDestinationSheet()プロパティの使用例を示しています。このコードはサンプルExcelファイルを使用し、出力Excelファイルを生成します。

todo:image_alt_text

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.xlsx");
// Load sample excel file
const wb = new AsposeCells.Workbook(filePath);

// Access the first sheet which contains chart
const source = wb.getWorksheets().get(0);

// Add another sheet named DestSheet
const destination = wb.getWorksheets().add("DestSheet");

// Set CopyOptions.ReferToDestinationSheet to true
const options = new AsposeCells.CopyOptions();
options.setReferToDestinationSheet(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);

// Save workbook in xlsx format
wb.save(path.join(dataDir, "output_out.xlsx"), AsposeCells.SaveFormat.Xlsx);