Få cellerindex

Möjliga användningsscenario

När du bara behöver manipulera en specifik data på arbetsbladet genom rad- och kolumnindex behöver du veta kolumn- och kolumnindex för den specifika cellen. Aspose.Cells för Python via .NET erbjuder denna funktion för att få rad- och kolumnindex genom namnet på raden, kolumnen och cellen. Aspose.Cells för Python via .NET tillhandahåller följande egenskaper och metoder för att hjälpa dig att uppnå dina mål.

Observera: Indexeringen är nollbaserad i Aspose.Cells för Python via .NET, men radens ID är baserad på ett i MS Excel.

Få cellindex med hjälp av Aspose.Cells för Python Excel-bibliotek

Detta exempel visar hur man:

  1. Skapa en arbetsbok och lägg till lite data.
  2. Hitta den specifika cellen i det första arbetsbladet.
  3. Få radindex och kolumnindex efter namnet på cellen.
  4. Få kolumnindex efter namnet på kolumnen.
  5. Få radindex efter namnet på raden.
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))