Node.jsとC++を使用したピボットテーブルのグローバリゼーション設定のカスタマイズ。
可能な使用シナリオ
時には、「合計、小計、総計、すべてのアイテム、複数選択項目、列ラベル、行ラベル、空白値」などのテキストをカスタマイズしたいことがあります。Aspose.Cells for Node.js via C++は、これらのシナリオに対応するためにピボットテーブルのグローバリゼーション設定をカスタマイズできます。この機能を使って、アラビア語、ヒンディー語、ポーランド語などの他の言語にラベルを変更することも可能です。
ピボットテーブルのグローバリゼーション設定のカスタマイズ
以下のサンプルコードは、ピボットテーブルのグローバリゼーション設定をカスタマイズする方法を説明しています。これにより、基本クラス PivotGlobalizationSettings から派生したクラス CustomPivotTableGlobalizationSettings を作成し、必要なすべてのメソッドをオーバーライドします。これらのメソッドは、Pivot Total、Sub Total、Grand Total、All Items、Multiple Items、Column Labels、Row Labels、Blank Values のカスタマイズされたテキストを返します。次に、そのクラスのオブジェクトを WorkbookSettings.getPivotSettings() プロパティに割り当てます。コードは、ピボットテーブルを含むソースExcelファイルをロードし、データを更新・計算し、出力PDFファイルとして保存します。次のスクリーンショットは、このサンプルコードの効果を示したもので、出力されたPDF内のピボットテーブルの異なる部分に、PivotGlobalizationSettingsクラスのオーバーライドされたメソッドによるカスタマイズされたテキストが表示されています。
サンプルコード
const path = require("path");
const AsposeCells = require("aspose.cells.node");
class CustomPivotTableGlobalizationSettings extends AsposeCells.PivotGlobalizationSettings {
// Gets the name of "Total" label in the PivotTable.
getTextOfTotal() {
console.log("---------GetPivotTotalName-------------");
return "AsposeGetPivotTotalName";
}
// Gets the name of "Grand Total" label in the PivotTable.
getTextOfGrandTotal() {
console.log("---------GetPivotGrandTotalName-------------");
return "AsposeGetPivotGrandTotalName";
}
// Gets the name of "(Multiple Items)" label in the PivotTable.
getTextOfMultipleItems() {
console.log("---------GetMultipleItemsName-------------");
return "AsposeGetMultipleItemsName";
}
// Gets the name of "(All)" label in the PivotTable.
getTextOfAll() {
console.log("---------GetAllName-------------");
return "AsposeGetAllName";
}
// Gets the name of "Column Labels" label in the PivotTable.
getTextOfColumnLabels() {
console.log("---------GetColumnLabelsOfPivotTable-------------");
return "AsposeGetColumnLabelsOfPivotTable";
}
// Gets the name of "Row Labels" label in the PivotTable.
getTextOfRowLabels() {
console.log("---------GetRowLabelsNameOfPivotTable-------------");
return "AsposeGetRowLabelsNameOfPivotTable";
}
// Gets the name of "(blank)" label in the PivotTable.
getTextOfEmptyData() {
console.log("---------GetEmptyDataName-------------");
return "(blank)AsposeGetEmptyDataName";
}
// Gets the name of PivotFieldSubtotalType type in the PivotTable.
getTextOfSubTotal(subTotalType) {
console.log("---------GetSubTotalName-------------");
switch (subTotalType) {
case AsposeCells.PivotFieldSubtotalType.Sum:
return "AsposeSum";
case AsposeCells.PivotFieldSubtotalType.Count:
return "AsposeCount";
case AsposeCells.PivotFieldSubtotalType.Average:
return "AsposeAverage";
case AsposeCells.PivotFieldSubtotalType.Max:
return "AsposeMax";
case AsposeCells.PivotFieldSubtotalType.Min:
return "AsposeMin";
case AsposeCells.PivotFieldSubtotalType.Product:
return "AsposeProduct";
case AsposeCells.PivotFieldSubtotalType.CountNums:
return "AsposeCount";
case AsposeCells.PivotFieldSubtotalType.Stdev:
return "AsposeStdDev";
case AsposeCells.PivotFieldSubtotalType.Stdevp:
return "AsposeStdDevp";
case AsposeCells.PivotFieldSubtotalType.Var:
return "AsposeVar";
case AsposeCells.PivotFieldSubtotalType.Varp:
return "AsposeVarp";
}
return "AsposeSubTotalName";
}
}
async function run() {
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Load your excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "samplePivotTableGlobalizationSettings.xlsx"));
workbook.getSettings().setGlobalizationSettings(new AsposeCells.GlobalizationSettings());
// Setting Custom Pivot Table Globalization Settings
workbook.getSettings().getGlobalizationSettings().setPivotSettings(new CustomPivotTableGlobalizationSettings());
// Hide first worksheet that contains the data of the pivot table
workbook.getWorksheets().get(0).setIsVisible(false);
// Access second worksheet
const ws = workbook.getWorksheets().get(1);
// Access the pivot table, refresh and calculate its data
const pt = ws.getPivotTables().get(0);
pt.setRefreshDataFlag(true);
pt.refreshData();
pt.calculateData();
pt.setRefreshDataFlag(false);
// Pdf save options - save entire worksheet on a single pdf page
const options = new AsposeCells.PdfSaveOptions();
options.setOnePagePerSheet(true);
// Save the output pdf
workbook.save(path.join(dataDir, "outputPivotTableGlobalizationSettings.pdf"), options);
}
run().catch(console.error);