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

可能な使用シナリオ

チャートを含む行または範囲を新しいワークシートにコピーする場合、チャートのデータソースは変更されません。たとえば、チャートのデータソースが=Sheet1!$A$1:$B$4の場合、行または範囲を新しいワークシートにコピーした後も、データソースは=Sheet1!$A$1:$B$4のままです。これはMicrosoft Excelでも同様の動作です。ただし、新しい宛先ワークシートを参照するようにしたい場合は、CopyOptions.ReferToDestinationSheetプロパティを使用してCells.CopyRows()メソッドを呼び出す際にそれをtrueに設定してください。たとえば、宛先ワークシートがDestSheetである場合、チャートのデータソースは=Sheet1!$A$1:$B$4から=DestSheet!$A$1:$B$4に変更されます。

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

次のサンプルコードは、チャートを含む行または範囲を新しいワークシートにコピーする際にCopyOptions.ReferToDestinationSheet プロパティの使用方法を説明しています。コードはサンプルExcelファイルを使用し、出力Excelファイルを生成します。

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Load sample excel file
Workbook wb = new Workbook(dataDir + "sample.xlsx");
// Access the first sheet which contains chart
Worksheet source = wb.Worksheets[0];
// Add another sheet named DestSheet
Worksheet destination = wb.Worksheets.Add("DestSheet");
// Set CopyOptions.ReferToDestinationSheet to true
CopyOptions options = new CopyOptions();
options.ReferToDestinationSheet = 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);
// Save workbook in xlsx format
wb.Save(dataDir + "output_out.xlsx", SaveFormat.Xlsx);