行内の最大列インデックスと列内の最大行インデックスの取得
可能な使用シナリオ
行または列のデータを操作する必要がある場合には、行と列のデータ範囲が必要です。Aspose.Cells for Python via .NETはこの機能を提供しています。行の最大列インデックスを取得するには、Row.last_cellとRow.last_data_cellプロパティを取得し、その後Cell.columnプロパティを使用して最大列インデックスと最大データ列インデックスを取得します。ただし、列の最大行インデックスと最大行データインデックスを取得するには、列上に範囲を作成し、範囲をトラバースして最後のセルを見つけ、最後にセルのCell.row属性を取得する必要があります。
Aspose.Cells for Python via .NETは、目標を達成するための次のプロパティとメソッドを提供しています。
Aspose.Cells for Python Excelライブラリを使用して行内の最大列インデックスと列内の最大行インデックスを取得する
この例では、次のことができます:
- サンプルファイルをロードする。
- 最大列インデックスと最大データ列インデックスを取得する行を取得します。
- セル上のCell.column属性を取得します。
- 列に基づいて範囲を作成します。
- イテレータを取得して範囲をトラバースします。
- セル上のCell.row属性を取得します。
import io | |
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Cells | |
workbook = Workbook("sample.xlsx") | |
sheet = workbook.worksheets[0] | |
cells = sheet.cells; | |
row = cells.check_row(1); | |
if row: | |
#get Maximum column index of Row which contains data or style. | |
print("Max column index in row: " + str(row.last_cell.column)) | |
#get Maximum column index of Row which contains data. | |
print("Max data column index in row: " + str(row.last_data_cell.column)) | |
# create the range of column B | |
columnRange = cells.create_range(1, 1, True) | |
max_row_index = cells.max_row + 1 | |
maxRow = 0 | |
maxDataRow = 0 | |
for row_index in range(0,max_row_index): | |
curr_cell = cells.check_cell(row_index, 1) | |
if curr_cell and curr_cell.string_value: | |
maxDataRow = curr_cell.row | |
if curr_cell and (curr_cell.string_value or curr_cell.has_custom_style): | |
maxRow = curr_cell.row | |
# Maximum row index of Column which contains data or style. | |
print("Max row index in Column: " + str(maxRow)) | |
# Maximum row index of Column which contains data. | |
print("Max data row index in Column: " + str(maxDataRow)) |