获取单元格索引

可能的使用场景

当您只需通过行和列索引操作工作表上的特定数据时,您需要了解该特定单元格的行索引和列索引。 Aspose.Cells for Python via .NET 提供了通过行、列和单元格的名称获取行和列索引的功能。 Aspose.Cells for Python via .NET提供以下属性和方法,以帮助您实现您的目标。

注意:在Aspose.Cells for Python via .NET中,索引是以零为基础的,但是MS Excel中的行标识是以一为基础的。

使用Aspose.Cells for Python Excel库获取单元格索引

此示例演示如何:

  1. 创建一个工作簿并添加一些数据。
  2. 在第一个工作表中查找特定单元格。
  3. 通过单元格名称获取行索引和列索引。
  4. 通过列名获取列索引。
  5. 通过行名获取行索引。
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))