Reputation: 151
I am trying to use the .find feature in gspread to locate a cell and update the cell next to it.
I have some python code that makes use of gspread api. The Google Sheet has columns Name and Color. I am able to use the .find feature to search for the cell that holds the name "Joe" however I need to update the cell next to the cell "Joe" in the column Colors with another variable called 'color'.
#Define the variables
name = 'Joe'
color = 'Blue'
# Search the google sheet to find the location of the cell named 'Joe'
sheet.find(name)
# Update the cell next to the location of the cell named 'Joe' with the color variable.
sheet.update(name, color)
I realise that sheet.update(name,color) is incorrect but not sure how else to express what I'm trying to do. I have checked the gspread documentation however I have had no luck. Perhaps there is another way I could achieve this result that im not aware of.
Upvotes: 1
Views: 572
Reputation: 201553
color
to the next cell of the name
.
the cell next to the location of the cell named 'Joe'
, when Joe
is "B2", you want to put the value of color
to "C2".If my understanding is correct, how about this modification? In this modification, from the returned value from sheet.find(name)
, the coordinate is retrieved. Then, color
is put using update_cell()
.
name = 'Joe'
color = 'Blue'
cell = sheet.find(name) # Modified
sheet.update_cell(cell.row, cell.col + 1, color) # Modified
Joe
is "B2", if you want to put the value of color
to "B1", please use the following script.
sheet.update_cell(cell.row - 1, cell.col, color)
Joe
is "B2", if you want to put the value of color
to "B3", please use the following script.
sheet.update_cell(cell.row + 1, cell.col, color)
Joe
is "B2", if you want to put the value of color
to "A2", please use the following script.
sheet.update_cell(cell.row, cell.col - 1, color)
If I misunderstood your question and this was not the result you want, I apologize.
Upvotes: 1