ピボットテーブルの操作

可能な使用シナリオ

新しいピボットテーブルを作成する以外に、新しいおよび既存のピボットテーブルを操作することができます。ピボットテーブルのソース範囲のデータを変更してから、それを更新して計算し、ピボットテーブルセルの新しい値を取得できます。データを変更した後、PivotTable.RefreshData()およびPivotTable.CalculateData()メソッドを使用してピボットテーブルを更新してください。

ピボットテーブルの操作

次のサンプルコードは、サンプルエクセルファイルをロードし、その最初のワークシート内の既存のピボットテーブルにアクセスします。ピボットテーブルのソース範囲内のセルB3の値を変更し、その後ピボットテーブルをリフレッシュします。ピボットテーブルをリフレッシュする前に、ピボットテーブルセルH8の値にアクセスし、その値が15であることを確認し、ピボットテーブルをリフレッシュした後、その値が6に変わったことを確認できます。このコードで生成された出力エクセルファイルと、サンプルエクセルファイルに対するサンプルコードの効果を示すスクリーンショット、および下に示すコンソール出力を参照してください。コンソール出力には、ピボットテーブルセルH8の値がリフレッシュ前後で表示されます。

todo:image_alt_text

サンプルコード

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
U16String dirPath(u"..\\Data\\PivotTables\\");
//Output directory path
U16String outPath(u"..\\Data\\Output\\");
//Path of input excel file
U16String sampleManipulatePivotTable = dirPath + u"sampleManipulatePivotTable.xlsx";
//Path of output excel file
U16String outputManipulatePivotTable = outPath + u"outputManipulatePivotTable.xlsx";
//Load the sample excel file
Workbook wb(sampleManipulatePivotTable);
//Access first worksheet
Worksheet ws = wb.GetWorksheets().Get(0);
//Change value of cell B3 which is inside the source data of pivot table
ws.GetCells().Get(u"B3").PutValue(u"Cup");
//Get the value of cell H8 before refreshing pivot table
U16String val = ws.GetCells().Get(u"H8").GetStringValue();
std::cout << "Before refreshing Pivot Table value of cell H8: " << val.ToUtf8() << std::endl;
//Access pivot table, refresh and calculate it
PivotTable pt = ws.GetPivotTables().Get(0);
pt.RefreshData();
pt.CalculateData();
//Get the value of cell H8 after refreshing pivot table
val = ws.GetCells().Get(u"H8").GetStringValue();
std::cout << "After refreshing Pivot Table value of cell H8: " << val.ToUtf8() << std::endl;
//Save the output excel file
wb.Save(outputManipulatePivotTable);
Aspose::Cells::Cleanup();

コンソール出力

上記のサンプルコードを提供されたサンプルエクセルファイルで実行したときのコンソール出力は次のとおりです。

 Before refreshing Pivot Table value of cell H8: 15

After refreshing Pivot Table value of cell H8: 6