Search code examples
pythongoogle-sheetsgoogle-sheets-apigspread

Is there any way to color google sheet cells using python?


if data:
    print("Redirected")
    cellNum = wks.acell(f'A{countt}')  
    # I want to color this cell (the cell number is stored in cellNum (<Cell R2C1>))
else:
    print("Not") 

I want to color the cell for which my condition is true.


Solution

  • I believe your goal is as follows.

    • You want to set a background color to a cell.
    • From your showing script, you want to achieve this using gspread for python.
    • You have already been able to get and put values to Google Spreadsheet using Sheets API.

    In this case, how about the following modification?

    Modified script:

    if data:
        print("Redirected")
        cellNum = wks.acell(f"A{countt}")
    
        wks.format(cellNum.address, {"backgroundColor": {"red": 1, "green": 0, "blue": 0}}) # <--- Added
    
    else:
        print("Not")
    
    • When this script is run, the background color of cell of cellNum is changed to red.
    • In this case, the values of RGB are 0 to 1. So, please divide the value of RGB by 255. So, the red color is {"red": 1, "green": 0, "blue": 0}. Please be careful about this.

    References: