セルのインデックスの取得
Contents
[
Hide
]
ExcelのデフォルトビューはA1形式の参照です。各列はA、B、C….と定義され、セルはA1、B2、C3…などと名前が付けられます。
このセルがどの行や列にあるか知りたい場合があるかもしれません。
可能な使用シナリオ
ワークシート上の特定のデータを列や行インデックスで操作する必要がある場合、その特定のセルの列と行のインデックスを知る必要があります。 Aspose.Cells for Python via .NETでは、行と列の名前によって行と列のインデックスを取得する機能を提供しています。 Aspose.Cells for Python via .NETは、目標を達成するための次のプロパティとメソッドを提供しています。
- CellsHelper.cell_name_to_index
- CellsHelper.column_index_to_name
- CellsHelper.column_name_to_index
- CellsHelper.row_index_to_name
- CellsHelper.row_name_to_index
注意:Aspose.Cells for Python via .NETではインデックス付けは0から始まりますが、MS Excelでは行のIDは1から始まります。
Aspose.Cells for PythonのExcelライブラリを使用してセルのインデックスを取得する
この例では、次のことができます:
- ワークブックを作成し、いくつかのデータを追加します。
- 最初のワークシートで特定のセルを見つけます。
- セルの名前によって行インデックスと列インデックスを取得します。
- 列の名前によって列インデックスを取得します。
- 行の名前によって行インデックスを取得します。
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, CellsHelper | |
# Instantiating an Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the newly added worksheet | |
ws = workbook.worksheets[0] | |
cells = ws.cells | |
# Setting the value to the cells | |
cell = cells.get("A1") | |
cell.put_value("Fruit") | |
cell = cells.get("B1") | |
cell.put_value("Count") | |
cell = cells.get("C1") | |
cell.put_value("Price") | |
cell = cells.get("A2") | |
cell.put_value("Apple") | |
cell = cells.get("A3") | |
cell.put_value("Mango") | |
cell = cells.get("A4") | |
cell.put_value("Blackberry") | |
cell = cells.get("A5") | |
cell.put_value("Cherry") | |
cell = cells.get("B2") | |
cell.put_value(5) | |
cell = cells.get("B3") | |
cell.put_value(3) | |
cell = cells.get("B4") | |
cell.put_value(6) | |
cell = cells.get("B5") | |
cell.put_value(4) | |
cell = cells.get("C2") | |
cell.put_value(5) | |
cell = cells.get("C3") | |
cell.put_value(20) | |
cell = cells.get("C4") | |
cell.put_value(30) | |
cell = cells.get("C5") | |
cell.put_value(60) | |
curr = cells.find("Blackberry", None) | |
# get row and column index of current cell | |
currRow = [] | |
currCol = [] | |
CellsHelper.cell_name_to_index(curr.name, currRow, currCol) | |
print("Row Index: " + str(currRow[0]) + " Column Index: " + str(currCol[0])) | |
# get column name by column index | |
columnName = CellsHelper.column_index_to_name(currCol[0]) | |
# get row name by row index | |
rowName = CellsHelper.row_index_to_name(currRow[0]) | |
print("Column Name: " + columnName + " Row Name: " + rowName); | |
# get column index by column name | |
columnIndex = CellsHelper.column_name_to_index(columnName); | |
# get row index by row name | |
rowIndex = CellsHelper.row_name_to_index(rowName) | |
print("Column Index: " + str(columnIndex) + " Row Index: " + str(rowIndex)) |