Use Cell Objects
stagingA Cell object represents a single cell in a spreadsheet. It has properties like value, value_unformatted, formula, note, and address.
Linked vs Unlinked Cells
- Linked Cells: Created via
worksheet.cell('A1'). Changes to these objects sync instantaneously with the spreadsheet. If you modify properties likecolorvalue, the spreadsheet updates. - Unlinked Cells: Created via
Cell('A1', 'value'). These are local models and do not affect the spreadsheet until you explicitly link them using.link(worksheet, True).
Key Operations
cell.fetch(): Fetches the latest data from the spreadsheet for that cell (required if accessing properties other thanvaluedirectly).cell.neighbour(direction): Gets a neighboring cell (e.g.,'topright'or a tuple like(1, 1)).cell.set_number_format(type, format): Sets the number format.cell.set_text_format(style, value): Sets text styles like'bold'.cell.update(): Syncs changes for a linked cell.
# Using linked cells
c1 = worksheet.cell('B1')
c1.col = 5 # Now c1 corresponds to E1
c1.value = "hoho" # Changes E1
# Get a range of cells
cell_list = worksheet.range('A1:C7')
# Unlink and relink to update multiple sheets
c.unlink()
c.note = "offline note"
c.link(wks1, True)
c.link(wks2, True)# Getting cell objects
c1 = Cell('A1',"hello") # create a unlinked cell
c1 = worksheet.cell('A1') # creates a linked cell whose changes syncs instantanously
cl.value # Getting cell value
c1.value_unformatted #Getting cell unformatted value
c1.formula # Getting cell formula if any
c1.note # any notes on the cell
c1.address # address object with cell position
cell_list = worksheet.range('A1:C7') # get a range of cells
cell_list = worksheet.col(5, returnas='cell') # return all cells in 5th column(E)