行と列の自動調整
自動調整
Aspose.Cells for Python via .NETにはMicrosoft Excelファイルを表すWorkbookクラスが提供されています。WorkbookクラスにはMicrosoft Excelファイルの各ワークシートにアクセスを許可するworksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。Worksheetクラスにはワークシートを管理するための多くのプロパティとメソッドが提供されています。この記事では、Worksheetクラスを使用して行または列の自動調整を行う方法について説明します。
行の自動調整 - シンプル
行の幅と高さを自動的にサイズ変更する最も簡単な方法は、Worksheetクラスのauto_fit_rowメソッドを呼び出すことです。auto_fit_rowメソッドは、リサイズする行のインデックスをパラメータとして受け取ります。
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(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the 3rd row of the worksheet | |
worksheet.auto_fit_row(1) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
セル範囲内の行を自動調整する方法
行は多くの列から構成されています。Aspose.Cells for Python via .NETでは、auto_fit_rowメソッドのオーバーロードバージョンを呼び出すことで、行内のセル範囲の内容に基づいて行を自動調整することができます。以下のパラメーターが必要です:
- 行インデックス:自動調整される行のインデックス。
- 最初の列インデックス:行の最初の列のインデックス。
- 最後の列インデックス:行の最後の列のインデックス。
auto_fit_rowメソッドは、行のすべての列の内容を確認してから行を自動調整します。
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(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the 3rd row of the worksheet | |
worksheet.auto_fit_row(1, 0, 5) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
セル範囲内の列を自動調整する方法
列は複数の行から構成されています。列内のセル範囲のコンテンツに基づいて列を自動調整することが可能です。このためには、以下のパラメータを受け取るauto_fit_columnメソッドのオーバーロードバージョンを呼び出すことができます:
- 列インデックス:自動調整される列のインデックス。
- 最初の行インデックス:列の最初の行のインデックス。
- 最後の行インデックス:列の最後の行のインデックス。
auto_fit_columnメソッドは、列のすべての行の内容を確認してから列を自動調整します。
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(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the Column of the worksheet | |
worksheet.auto_fit_column(4, 4, 6) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
結合セルの行を自動調整する方法
Aspose.Cells for Python via .NET では、AutoFitterOptions API を使用してマージされたセルの行の自動調整が可能です。 AutoFitterOptions クラスは、マージされたセルの行を自動調整するために使用できる auto_fit_merged_cells_type プロパティを提供しています。 auto_fit_merged_cells_type は、以下のメンバーを持つ AutoFitMergedCellsType 列挙体を受け入れます。
- NONE: マージされたセルを無視します。
- FIRST_LINE: 最初の行の高さのみ拡大します。
- LAST_LINE: 最後の行の高さのみ拡大します。
- EACH_LINE: 各行の高さのみ拡大します。
from aspose.cells import AutoFitMergedCellsType, AutoFitterOptions, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Output directory | |
outputDir = RunExamples.Get_OutputDirectory() | |
# Instantiate a new Workbook | |
wb = Workbook() | |
# Get the first (default) worksheet | |
_worksheet = wb.worksheets[0] | |
# Create a range A1:B1 | |
range = _worksheet.cells.create_range(0, 0, 1, 2) | |
# Merge the cells | |
range.merge() | |
# Insert value to the merged cell A1 | |
_worksheet.cells.get(0, 0).value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end" | |
# Create a style object | |
style = _worksheet.cells.get(0, 0).get_style() | |
# Set wrapping text on | |
style.is_text_wrapped = True | |
# Apply the style to the cell | |
_worksheet.cells.get(0, 0).set_style(style) | |
# Create an object for AutoFitterOptions | |
options = AutoFitterOptions() | |
# Set auto-fit for merged cells | |
options.auto_fit_merged_cells_type = AutoFitMergedCellsType.EACH_LINE | |
# Autofit rows in the sheet(including the merged cells) | |
_worksheet.auto_fit_rows(options) | |
# Save the Excel file | |
wb.save(outputDir + "AutofitRowsforMergedCells.xlsx") |
また、選択した行/列を自動調整するために、auto_fit_rowsおよびauto_fit_columnsメソッドのオーバーロードバージョンを使用し、必要に応じてAutoFitterOptionsインスタンスを指定することもできます。
前述のメソッドのシグネチャは次のとおりです: