PivotTableのPivotFieldのDisplayNameによってCellオブジェクトを取得する
Contents
[
Hide
]
Aspose.Cells for Python via .NETでは、ピボットフィールドのディスプレイ名によってセルオブジェクトにアクセスするために使用できるPivotTable.get_cell_by_display_name(display_name)メソッドを提供しています。このメソッドは、ピボットフィールドヘッダーを強調表示または書式設定したい場合に役立ちます。この記事では、データフィールドの表示名によってセルオブジェクトを取得し、その後書式設定を適用する方法について説明しています。
PivotTableのPivotFieldのDisplayNameによってCellオブジェクトを取得する方法
以下のコードは、ワークシートの最初のピボットテーブルにアクセスし、ピボットテーブルの2番目のデータフィールドのDisplay名によるセルを取得します。そして、セルの塗りつぶし色とフォント色をそれぞれライトブルーとブラックに変更します。以下のスクリーンショットは、コードの実行前と後のピボットテーブルの様子を示しています。
ピボットテーブル - 実行前 |
---|
![]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import Workbook | |
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(".") | |
# Create workbook object from source excel file | |
workbook = Workbook(dataDir + "source.xlsx") | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
# Access first pivot table inside the worksheet | |
pivotTable = worksheet.pivot_tables[0] | |
# Access cell by display name of 2nd data field of the pivot table | |
cell = pivotTable.get_cell_by_display_name(pivotTable.data_fields[0].display_name) | |
# Access cell style and set its fill color and font color | |
style = cell.get_style() | |
style.foreground_color = Color.light_blue | |
style.font.color = Color.black | |
# Set the style of the cell | |
pivotTable.format(cell.row, cell.column, style) | |
# Save workbook | |
workbook.save(dataDir + "output_out.xlsx") |
ピボットテーブル - 実行後 |
---|
![]() |