行または範囲のコピー中にチャートのデータ ソースを宛先ワークシートに変更する

考えられる使用シナリオ

グラフを含む行または範囲を新しいワークシートにコピーしても、グラフのデータ ソースは変更されません。たとえば、グラフのデータ ソースが =Sheet1!$A$1:$B$4 の場合、行または範囲を新しいワークシートにコピーした後、データ ソースは同じままになります (つまり、=Sheet1!$A$1:$B$4)。それはまだ古いワークシート、つまりSheet1を参照しています。これは、Microsoft Excel の動作でもあります。ただし、新しい宛先ワークシートを参照する場合は、CopyOptions.ReferToDestinationSheetプロパティに設定し、真実を呼び出しながらCells.CopyRows()方法。宛先ワークシートが DestSheet の場合、グラフのデータ ソースは =Sheet1!$A$1:$B$4 から =DestSheet!$A$1:$B$4 に変更されます。

行または範囲のコピー中にチャートのデータ ソースを宛先ワークシートに変更する

次のサンプル コードは、CopyOptions.ReferToDestinationSheetプロパティを使用して、グラフを含む行または範囲を新しいワークシートにコピーします。コードは、サンプルエクセルファイルを生成し、出力エクセルファイル.

todo:画像_代替_文章

// 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);