Satırları veya Aralığı Kopyalarken Grafiğin Veri Kaynağını Hedef Çalışma Sayfasına Değiştirin

Olası Kullanım Senaryoları

Grafik içeren satırları veya aralığı yeni bir çalışma sayfasına kopyaladığınızda, grafiğin veri kaynağı değişmez. Örneğin, grafiğin veri kaynağı =Sayfa1!$A$1:$B$4 ise, satırları veya aralığı yeni çalışma sayfasına kopyaladıktan sonra, veri kaynağı aynı kalacaktır, yani =Sayfa1!$A$1:$B$4. Hala eski çalışma sayfasına, yani Sayfa1’e atıfta bulunuyor. Bu aynı zamanda Microsoft Excel’deki davranıştır. Ancak, yeni hedef çalışma sayfasına atıfta bulunmasını istiyorsanız, lütfenCopyOptions.ReferToDestinationSheetözellik ve bunu ayarlayındoğru çağrılırkenCells.CopyRows()yöntem. Şimdi, hedef çalışma sayfanız DestSheet ise, grafiğinizin veri kaynağı =Sheet1!$A$1:$B$4 iken =DestSheet!$A$1:$B$4 olarak değişir.

Satırları veya Aralığı Kopyalarken Grafiğin Veri Kaynağını Hedef Çalışma Sayfasına Değiştirin

Aşağıdaki örnek kod, kullanımını açıklarCopyOptions.ReferToDestinationSheet grafikleri içeren satırları veya aralığı yeni bir çalışma sayfasına kopyalarken özelliği. kod kullanırörnek excel dosyası ve oluştururçıktı excel dosyası.

yapılacaklar:resim_alternatif_metin

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