行と列のグループ化および展開
紹介
Microsoft Excelファイルでは、データの概要を作成して、1回のマウスクリックで詳細のレベルを表示したり非表示にしたりできます。
アウトライン記号の1、2、3、+、および-をクリックして、ワークシートのセクションの要約または見出しを迅速に表示したり、個々の要約または見出しの詳細を表示する際に使用できます。下の図で示されているように、個々の要約または見出しの詳細を表示するためにシンボルを使用できます。
行と列のグループ化 |
---|
![]() |
行と列のグループ管理
Aspose.Cells for Python via .NET には、Microsoft Excel ファイルを表す Workbook クラスが用意されています。Workbook クラスにはExcelファイル内の各ワークシートにアクセスすることを可能にする WorksheetCollection が含まれています。ワークシートは Worksheet クラスで表されます。Worksheet クラスにはワークシート内のすべてのセルを表す Cells コレクションがあります。
Cellsコレクションには、ワークシート内の行や列を管理するためのいくつかのメソッドが提供されており、そのうちいくつかについて以下で詳しく説明します。
行と列のグループ化方法
行と列をグループ化するには、Cellsコレクションのgroup_rowsおよびgroup_columnsメソッドを呼び出すことができます。両方のメソッドは以下のパラメーターを受け取ります:
- 最初の行/列インデックス、グループ内の最初の行または列
- グループ内の最後の行/列のインデックス、最後の行または列。
- 非表示かどうか、グループ化後に行または列を非表示にするかどうかを指定するブールパラメータ。
from aspose.cells import 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(".") | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(dataDir + "book1.xls", "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Grouping first six rows (from 0 to 5) and making them hidden by passing true | |
worksheet.cells.group_rows(0, 5, True) | |
# Grouping first three columns (from 0 to 2) and making them hidden by passing true | |
worksheet.cells.group_columns(0, 2, True) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
グループ設定
Microsoft Excelでは、以下を表示するためのグループ設定を構成できます:
- 詳細の下の要約行。
- 詳細の右側の要約列。
開発者は、Worksheetクラスのoutlineプロパティを使用して、これらのグループ設定を構成できます。
要約行を詳細の下に設定する方法
サマリー行が詳細の下に表示されるかどうかは、Outlineクラスのsummary_row_belowプロパティをtrueまたはfalseに設定することで制御できます。
from aspose.cells import 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(".") | |
workbook = Workbook(dataDir + "sample.xlsx") | |
worksheet = workbook.worksheets[0] | |
# Grouping first six rows and first three columns | |
worksheet.cells.group_rows(0, 5, True) | |
worksheet.cells.group_columns(0, 2, True) | |
# Setting SummaryRowBelow property to false | |
worksheet.outline.summary_row_below = False | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") |
要約列を詳細の右に設定する方法
開発者は、Outlineクラスのsummary_column_rightプロパティをtrueまたはfalseに設定することで、詳細の右側にサマリー列を表示することもできます。
from aspose.cells import 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(".") | |
workbook = Workbook(dataDir + "sample.xlsx") | |
worksheet = workbook.worksheets[0] | |
# Grouping first six rows and first three columns | |
worksheet.cells.group_rows(0, 5, True) | |
worksheet.cells.group_columns(0, 2, True) | |
worksheet.outline.summary_column_right = True | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") |
行と列のグループ解除方法
グループ化された行または列を解除するには、{1}のコレクションの{2}および{3}メソッドを呼び出します。どちらのメソッドも2つのパラメーターを取ります。
- 最初の行または列のインデックス、グループ化を解除する最初の行/列。
- 最後の行または列のインデックス、グループ化を解除する最後の行/列。
ungroup_rowsには、ブール値の第三パラメーターを取るオーバーロードがあります。これをtrueに設定すると、グループ化された情報がすべて削除されます。それ以外の場合は、外部グループ化情報のみが削除されます。
from aspose.cells import 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(".") | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(dataDir + "book1.xls", "rb") | |
# Instantiating a Workbook object | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Ungrouping first six rows (from 0 to 5) | |
worksheet.cells.ungroup_rows(0, 5) | |
# Ungrouping first three columns (from 0 to 2) | |
worksheet.cells.ungroup_columns(0, 2) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |