ピボットテーブルの書式設定
ピボットテーブルの外観
ピボットテーブルの外観をカスタマイズする方法については、「ピボットテーブルの作成」でシンプルなピボットテーブルの作成方法が説明されています。この記事では、さまざまなプロパティを設定してピボットテーブルの外観をカスタマイズする方法について説明します:
- ピボットテーブルの書式オプション
- ピボットフィールドの書式オプション
- データフィールドの書式オプション
ピボットテーブルフォーマットオプションの設定方法
PivotTable クラスは、全体のピボットテーブルを制御し、さまざまな方法で書式設定できます。
自動フォーマットタイプの設定方法
Microsoft Excelはさまざまなプリセットレポートフォーマットを提供しています。Aspose.Cells for Node.js via C++もこれらのフォーマットオプションをサポートしています。アクセス方法は次の通り:
- PivotTable.setIsAutoFormat(value) を true に設定します。
- PivotTableAutoFormatType 列挙型から書式設定オプションを割り当てます。
const AsposeCells = require("aspose.cells.node"); | |
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
//The path to the documents directory. | |
var dataDir = RunExamples.GetDataDir("."); | |
//Load a template file | |
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls"); | |
var pivotindex = 0; | |
//Get the first worksheet | |
var worksheet = workbook.getWorksheets().get(0); | |
//Accessing the PivotTable | |
var pivotTable = worksheet.getPivotTables().get(pivotindex); | |
//Setting the PivotTable report is automatically formatted | |
pivotTable.setIsAutoFormat(true); | |
//Setting the PivotTable atuoformat type. | |
pivotTable.setAutoFormatType(AsposeCells.PivotTableAutoFormatType.Report5); | |
//Saving the Excel file | |
workbook.save(dataDir + "output.xls"); |
フォーマットオプションの設定方法
以下のコードサンプルは、ピボットテーブルを書式設定して行と列の合計を表示する方法、およびレポートのフィールド順序を設定する方法を示しています。また、null 値にカスタム文字列を設定する方法も示しています。
const AsposeCells = require("aspose.cells.node"); | |
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
//The path to the documents directory. | |
var dataDir = RunExamples.GetDataDir("."); | |
//Load a template file | |
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls"); | |
//Get the first worksheet | |
var worksheet = workbook.getWorksheets().get(0); | |
var pivotindex = 0; | |
//Accessing the PivotTable | |
var pivotTable = worksheet.getPivotTables().get(pivotindex); | |
//Setting the PivotTable report shows grand totals for rows. | |
pivotTable.setRowGrand(true); | |
//Setting the PivotTable report shows grand totals for columns. | |
pivotTable.setColumnGrand(true); | |
//Setting the PivotTable report displays a custom string in cells that contain null values. | |
pivotTable.setDisplayNullString(true); | |
pivotTable.setNullString("null"); | |
//Setting the PivotTable report's layout | |
pivotTable.setPageFieldOrder(AsposeCells.PrintOrderType.DownThenOver); | |
//Saving the Excel file | |
workbook.save(dataDir + "output.xls"); |
見た目と手触りの書式設定
事前設定されたレポート形式を使用せずに、ピボットテーブルレポートの外観を手動でフォーマットする場合、PivotTable.formatAll(style) メソッドと PivotTable.format(row, column, style) メソッドを使用します。所望のフォーマットのためのスタイルオブジェクトを作成します。たとえば:
const AsposeCells = require("aspose.cells.node"); | |
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
//The path to the documents directory. | |
var dataDir = RunExamples.GetDataDir(".") | |
//Load a template file | |
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls"); | |
//Get the first worksheet | |
var worksheet = workbook.getWorksheets().get(0); | |
var pivot = worksheet.getPivotTables().get(0); | |
pivot.setPivotTableStyleType(AsposeCells.PivotTableStyleType.PivotTableStyleDark1); | |
var style = workbook.createStyle(); | |
style.getFont().setName("Arial Black"); | |
style.setPattern(AsposeCells.BackgroundType.Solid); | |
style.setForegroundColor(AsposeCells.Color.Yellow); | |
pivot.formatAll(style); | |
//Saving the Excel file | |
workbook.save(dataDir + "output.xls"); |
ピボットフィールドのフォーマットオプションの設定方法
PivotField クラスは、ピボットテーブルのフィールドを表し、さまざまな方法で書式設定できます。以下のコードサンプルは、次のように行います:
- 行フィールドへのアクセス。
- 小計の設定。
- 自動並べ替えの設定。
- 自動表示の設定。
行/列/ページフィールドのフォーマット設定方法
const AsposeCells = require("aspose.cells.node"); | |
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
//The path to the documents directory. | |
var dataDir = RunExamples.GetDataDir("."); | |
//Load a template file | |
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls"); | |
//Get the first worksheet | |
var worksheet = workbook.getWorksheets().get(0); | |
var pivotindex = 0; | |
//Accessing the PivotTable | |
var pivotTable = worksheet.getPivotTables().get(pivotindex); | |
//Setting the PivotTable report shows grand totals for rows. | |
pivotTable.setRowGrand(true); | |
//Accessing the row fields. | |
var pivotFields = pivotTable.getRowFields(); | |
//Accessing the first row field in the row fields. | |
var pivotField = pivotFields.get(0); | |
//Setting Subtotals. | |
pivotField.setSubtotals(AsposeCells.PivotFieldSubtotalType.Sum, true); | |
pivotField.setSubtotals(AsposeCells.PivotFieldSubtotalType.Count, true); | |
//Setting autosort options. | |
//Setting the field auto sort. | |
pivotField.setIsAutoSort(true); | |
//Setting the field auto sort ascend. | |
pivotField.setIsAscendSort(true); | |
//Setting the field auto sort using the field itself. | |
pivotField.setAutoSortField(-5); | |
//Setting autoShow options. | |
//Setting the field auto show. | |
pivotField.setIsAutoShow(true); | |
//Setting the field auto show ascend. | |
pivotField.setIsAscendShow(false); | |
//Setting the auto show using field(data field). | |
pivotField.setAutoShowField(0); | |
//Saving the Excel file | |
workbook.save(dataDir + "output.xls"); |
データフィールドのフォーマット設定方法
以下のコードサンプルは、データフィールドの表示形式と数値形式を設定する方法を示しています。
const AsposeCells = require("aspose.cells.node"); | |
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
//The path to the documents directory. | |
var dataDir = RunExamples.GetDataDir("."); | |
//Load a template file | |
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls"); | |
//Get the first worksheet | |
var worksheet = workbook.getWorksheets().get(0); | |
var pivotindex = 0; | |
//Accessing the PivotTable | |
var pivotTable = worksheet.getPivotTables().get(pivotindex); | |
//Accessing the data fields. | |
var pivotFields = pivotTable.getDataFields(); | |
//Accessing the first data field in the data fields. | |
var pivotField = pivotFields.get(0); | |
//Setting data display format | |
pivotField.showValuesAs(AsposeCells.PivotFieldDataDisplayFormat.PercentageOf, 1, AsposeCells.PivotItemPositionType.Next, 0); | |
//Setting number format | |
pivotField.setNumber(10); | |
//Saving the Excel file | |
workbook.save(dataDir + "output.xls"); |
ピボットフィールドをクリアする方法
PivotFieldCollection には、ピボットフィールドをクリアするための clear() メソッドがあります。たとえば、ページ、列、行、またはデータなど、領域内のすべてのピボットフィールドをクリアしたい場合に使用します。 以下のコードサンプルは、データ領域内のすべてのピボットフィールドをクリアする方法を示しています。
const AsposeCells = require("aspose.cells.node"); | |
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
//The path to the documents directory. | |
var dataDir = RunExamples.GetDataDir(".") | |
//Load a template file | |
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls"); | |
//Get the first worksheet | |
var sheet = workbook.getWorksheets().get(0); | |
//Get the pivot tables in the sheet | |
var pivotTables = sheet.getPivotTables(); | |
//Get the first PivotTable | |
var pivotTable = pivotTables.get(0); | |
//Clear all the data fields | |
pivotTable.getDataFields().clear(); | |
//Add new data field | |
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "Betrag Netto FW"); | |
//Set the refresh data flag on | |
pivotTable.setRefreshDataFlag(true); | |
//Refresh and calculate the pivot table data | |
pivotTable.refreshData(); | |
pivotTable.calculateData(); | |
pivotTable.setRefreshDataFlag(false); | |
//Saving the Excel file | |
workbook.save(dataDir + "output.xls"); |