ピボットテーブルを更新し計算項目を持つピボットテーブルを更新する

計算項目を持つピボットテーブルを更新および計算する

以下のサンプルコードは、“add”、“div”、“div2"などの3つの計算項目を持つピボットテーブルを含むソースエクセルファイルを読み込みます。最初にセルD2の値を20に変更し、Aspose.CellsのAPIを使用してピボットテーブルを更新および計算し、その結果をPDF形式で保存します。出力PDFの結果で、Aspose.Cellsが計算項目を持つピボットテーブルを成功裏に更新および計算したことが示されています。手動でセルD2に値20を入力し、Alt+F5ショートカットキーを使用するか、ピボットテーブルの更新ボタンをクリックしてMicrosoft Excelで検証できます。

// 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 source excel file containing a pivot table having calculated items
Workbook wb = new Workbook(dataDir + "sample.xlsx");
// Access first worksheet
Worksheet sheet = wb.Worksheets[0];
// Change the value of cell D2
sheet.Cells["D2"].PutValue(20);
// Refresh and calculate all the pivot tables inside this sheet
foreach (PivotTable pt in sheet.PivotTables)
{
pt.RefreshData();
pt.CalculateData();
}
// Save the workbook in output pdf
wb.Save(dataDir + "RefreshAndCalculateItems_out.pdf", SaveFormat.Pdf);