I am trying to learn how to conditionally format cells in a worksheet using pygsheets. The reason for this is to only allow the end user interacting with a google sheet to only be able to input values that are in the correct format.
For example, how can we format the sheet so that:
- cells A2 to A5 to be numbers between 0 and 50.
- cells B2 to B5 to be an email address (a string say having @gmail.com or @yahoo.com).
- cells C2 to C5 to be some string corresponding to a date.
If the cell does not meet the correct format the colour can change to a slight red to signify that it is not the correct format.
Using the two documentation for pygsheets I was able to create the sheet and start with formatting for the headers. I was however unable to use a conditional statement to change the colour as need be, I think I am supposed to use the set_data_validation
function to do this but I could not get it to work. The documentation referred me to google sheets API spreadsheets conditional page but I could not understand this documentation well enough to do set up these conditions.
I have added my example and solution for the first point which did not do what I thought it should.
Working example
number int | string | date |
---|---|---|
import pygsheets
import pandas as pd
# get credentials from the json file
service = pygsheets.authorize(service_file='key.json')
spread_sheet_id = ".... insert id here ...." # id to get example worksheet
spreadsheet = pygsheets.Spreadsheet(client=service,id=spread_sheet_id)
# open work sheet
worksheet = spreadsheet.worksheet(0)
header_list = ["number int","string","date"]
for index, element in enumerate(header_list):
# get the cell A1,B1 and C1 and set elements to header_list elements
header_cell = wksheet.cell( str(chr(65+index)+"1"))
header_cell.value = element
# format colour and make bold
header_cell.text_format['bold'] = True # make the header bold
header_cell.color = (0.9529412, 0.9529412, 0.9529412, 0) # set background color of this cell as a tuple (red, green, blue, alpha)
# update
header_cell.update()
# format A2 to A5 to be an integer between 0 and 53 (DIDN'T WORK)
for index in ["A2","A3","A4","A5"]:
model_cell = pygsheets.Cell(index)
print(index)
model_cell.set_number_format(format_type = pygsheets.FormatType.NUMBER, pattern = "^(?:[1-9]|[1-4][0-9]|5[0-3])$") # This pattern will match any integer between 0 and 53, inclusive
header_cell.update()
# (TRY OTHER WAY as a number between 0 and 53) (DIDN'T WORK)
cells = worksheet.range('B2:B5') # select the cells to be formatted
# create the data validation rule
# rule = pygsheets.data_validation.RangeRule(minimum=0, maximum=53)
# model_cell = pygsheets.Cell("A1")
# model_cell.set_number_format(format_type = pygsheets.FormatType.NUMBER,pattern = "0")
# worksheet.add_conditional_formatting('C2', 'C6', 'NUMBER_BETWEEN', {'backgroundColor':{'red':1}}, ['0','53'])
# ... format B2 to B5 to be a string less than 30 characters
# ... format C2 to C5 to be a string that is a relevent email address less than 50 characters
From the google sheets API documentation, I think that each rule can be set up as:
|# create pygsheets conditional formatting rule using
# https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#conditiontype
rule = {
"type": enum (NUMBER_BETWEEN),
"values": [
{
object (ConditionValue)
}
]
}