Steve
Steve

Reputation: 151

Is there a way to update the cell adjacent to the cell I searched for using .find in gspread api for python

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

Answers (1)

Tanaike
Tanaike

Reputation: 201553

  • You want to put a value of color to the next cell of the name.
    • About 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".
  • You have already been able to update cells using gspread.

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().

Modified script:

name = 'Joe'
color = 'Blue'

cell = sheet.find(name)  # Modified
sheet.update_cell(cell.row, cell.col + 1, color)  # Modified

Note:

  • When 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)
  • When 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)
  • When 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)

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 1

Related Questions