ピボットテーブルの書式設定

ピボットテーブルの外観

ピボットテーブルの外観をカスタマイズする方法については、「ピボットテーブルの作成」でシンプルなピボットテーブルの作成方法が説明されています。この記事では、さまざまなプロパティを設定してピボットテーブルの外観をカスタマイズする方法について説明します:

  • ピボットテーブルの書式オプション
  • ピボットフィールドの書式オプション
  • データフィールドの書式オプション

ピボットテーブルフォーマットオプションの設定方法

PivotTable クラスは、全体のピボットテーブルを制御し、さまざまな方法で書式設定できます。

自動フォーマットタイプの設定方法

Microsoft Excelにはさまざまなプリセットのレポートフォーマットがあります。Aspose.Cells for Python via .NETもこれらの書式設定オプションをサポートしています。アクセスするためには:

  1. PivotTable.is_auto_formattrue に設定します。
  2. PivotTableAutoFormatType 列挙型から書式設定オプションを割り当てます。
from aspose.cells import Workbook
from aspose.cells.pivot import PivotTableAutoFormatType
# 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 a template file
workbook = Workbook(dataDir + "Book1.xls")
pivotindex = 0
# Get the first worksheet
worksheet = workbook.worksheets[0]
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Setting the PivotTable report is automatically formatted
pivotTable.is_auto_format = True
# Setting the PivotTable atuoformat type.
pivotTable.auto_format_type = PivotTableAutoFormatType.REPORT5
# Saving the Excel file
workbook.save(dataDir + "output.xls")

フォーマットオプションの設定方法

以下のコードサンプルは、ピボットテーブルを書式設定して行と列の合計を表示する方法、およびレポートのフィールド順序を設定する方法を示しています。また、null 値にカスタム文字列を設定する方法も示しています。

from aspose.cells import PrintOrderType, 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 a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotindex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Setting the PivotTable report shows grand totals for rows.
pivotTable.row_grand = True
# Setting the PivotTable report shows grand totals for columns.
pivotTable.column_grand = True
# Setting the PivotTable report displays a custom string in cells that contain null values.
pivotTable.display_null_string = True
pivotTable.null_string = "null"
# Setting the PivotTable report's layout
pivotTable.page_field_order = PrintOrderType.DOWN_THEN_OVER
# Saving the Excel file
workbook.save(dataDir + "output.xls")

見た目と手触りの書式設定

事前設定されたレポート形式を使用せずに、ピボットテーブルレポートの外観を手動でフォーマットする場合、PivotTable.format_all(style) メソッドと PivotTable.format(row, column, style) メソッドを使用します。所望のフォーマットのためのスタイルオブジェクトを作成します。たとえば:

from aspose.cells import BackgroundType, Workbook
from aspose.cells.pivot import PivotTableStyleType
from aspose.pydrawing import Color
# 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 a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivot = workbook.worksheets[0].pivot_tables[0]
pivot.pivot_table_style_type = PivotTableStyleType.PIVOT_TABLE_STYLE_DARK1
style = workbook.create_style()
style.font.name = "Arial Black"
style.foreground_color = Color.yellow
style.pattern = BackgroundType.SOLID
pivot.format_all(style)
# Saving the Excel file
workbook.save(dataDir + "output.xls")

ピボットフィールドのフォーマットオプションの設定方法

PivotField クラスは、ピボットテーブルのフィールドを表し、さまざまな方法で書式設定できます。以下のコードサンプルは、次のように行います:

  • 行フィールドへのアクセス。
  • 小計の設定。
  • 自動並べ替えの設定。
  • 自動表示の設定。

行/列/ページフィールドのフォーマット設定方法

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldSubtotalType
# 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 a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotindex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Setting the PivotTable report shows grand totals for rows.
pivotTable.row_grand = True
# Accessing the row fields.
pivotFields = pivotTable.row_fields
# Accessing the first row field in the row fields.
pivotField = pivotFields[0]
# Setting Subtotals.
pivotField.set_subtotals(PivotFieldSubtotalType.SUM, True)
pivotField.set_subtotals(PivotFieldSubtotalType.COUNT, True)
# Setting autosort options.
# Setting the field auto sort.
pivotField.is_auto_sort = True
# Setting the field auto sort ascend.
pivotField.is_ascend_sort = True
# Setting the field auto sort using the field itself.
pivotField.auto_sort_field = -5
# Setting autoShow options.
# Setting the field auto show.
pivotField.is_auto_show = True
# Setting the field auto show ascend.
pivotField.is_ascend_show = False
# Setting the auto show using field(data field).
pivotField.auto_show_field = 0
# Saving the Excel file
workbook.save(dataDir + "output.xls")

データフィールドのフォーマット設定方法

以下のコードサンプルは、データフィールドの表示形式と数値形式を設定する方法を示しています。

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldDataDisplayFormat, PivotItemPosition
# 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 a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotindex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Accessing the data fields.
pivotFields = pivotTable.data_fields
# Accessing the first data field in the data fields.
pivotField = pivotFields[0]
# Setting data display format
pivotField.data_display_format = PivotFieldDataDisplayFormat.PERCENTAGE_OF
# Setting the base field.
pivotField.base_field_index = 1
# Setting the base item.
pivotField.base_item_position = PivotItemPosition.NEXT
# Setting number format
pivotField.number = 10
# Saving the Excel file
workbook.save(dataDir + "output.xls")

ピボットフィールドをクリアする方法

PivotFieldCollection には、ピボットフィールドをクリアするための clear() メソッドがあります。たとえば、ページ、列、行、またはデータなど、領域内のすべてのピボットフィールドをクリアしたい場合に使用します。 以下のコードサンプルは、データ領域内のすべてのピボットフィールドをクリアする方法を示しています。

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldType
# 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 a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
sheet = workbook.worksheets[0]
# Get the pivot tables in the sheet
pivotTables = sheet.pivot_tables
# Get the first PivotTable
pivotTable = pivotTables[0]
# Clear all the data fields
pivotTable.data_fields.clear()
# Add new data field
pivotTable.add_field_to_area(PivotFieldType.DATA, "Betrag Netto FW")
# Set the refresh data flag on
pivotTable.refresh_data_flag = False
# Refresh and calculate the pivot table data
pivotTable.refresh_data()
pivotTable.calculate_data()
# Saving the Excel file
workbook.save(dataDir + "output.xls")