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

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

次のサンプルコードでは、“add”、“div”、“div2"などの3つの計算されたアイテムを持つピボットテーブルを含む元のExcelファイルを読み込みます。まず、セルD2の値を20に変更し、Aspose.Cells for Python via .NETのAPIを使用してピボットテーブルを更新および計算し、ワークブックをPDF形式で保存します。その結果得られる出力PDFは、Aspose.Cells for Python via .NETが計算されたアイテムを持つピボットテーブルを正常に更新および計算したことを示しています。これをMicrosoft Excelで確認するには、手動でセルD2に値20を入力し、Alt+F5ショートカットキーを使用するか、ピボットテーブルの更新ボタンをクリックしてピボットテーブルを更新してください。

from aspose.cells import SaveFormat, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load source excel file containing a pivot table having calculated items
wb = Workbook(dataDir + "sample.xlsx")
# Access first worksheet
sheet = wb.worksheets[0]
# Change the value of cell D2
sheet.cells.get("D2").put_value(20)
# Refresh and calculate all the pivot tables inside this sheet
for pt in sheet.pivot_tables:
pt.refresh_data()
pt.calculate_data()
# Save the workbook in output pdf
wb.save(dataDir + "RefreshAndCalculateItems_out.pdf", SaveFormat.PDF)