行または範囲をコピーする際、チャートを新しいワークシートにコピーすると、チャートのデータソースは変更されません。

可能な使用シナリオ

行または範囲を新しいワークシートにコピーする際、チャートのデータソースが変更されないことがあります。

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

行または範囲に含まれるチャートを新しいワークシートにコピーする際にCopyOptions.ReferToDestinationSheetプロパティの使用方法を示したサンプルコードです。コードではサンプルエクセルファイルが使用され、出力エクセルファイルが生成されます。スクリーンショットでは、出力エクセルファイル内のチャートのデータソースがSheet1ではなくDestSheetを参照していることが示されています。

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(ChangeDataSource.class);
// Load sample excel file
Workbook wb = new Workbook(dataDir + "sample.xlsx");
// Access the first sheet which contains chart
Worksheet source = wb.getWorksheets().get(0);
// Add another sheet named DestSheet
Worksheet destination = wb.getWorksheets().add("DestSheet");
// Set CopyOptions.ReferToDestinationSheet to true
CopyOptions options = new 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(dataDir + "output.xlsx", SaveFormat.XLSX);