Search code examples
pythongoogle-sheets-apigspread

How do I set dropdown colors using Google SpreadSheet API?


I am using the gspread library in python to send api requests.
The request is to set the dropdown.
However, I could not figure out how to set the following.

  1. set a color for each value in the dropdown
  2. set the display style to "Chip"

cells

Here is the code.

options = ["Apple", "Banana", "Orange"]
values = [{"userEnteredValue": option} for option in options]

request = {
    "requests": [
        {
            "setDataValidation": {
                "range": {
                    "sheetId": 0,
                    "startRowIndex": 1,
                    "endRowIndex": 4,
                    "startColumnIndex": 1,
                    "endColumnIndex": 2,
                },
                "rule": {
                    "condition": {"type": "ONE_OF_LIST", "values": values},
                    "strict": False,
                    "showCustomUi": True
                },
            }
        }
    ]
}

spreadSheet.batch_update(request)

Please let me know if there is a way to do this. Thanks!


Solution

  • I also wanted to create a "pretty" dropdown by API. But my finding is that API does not support detailed Display Style like "chip" (just True or False on 'showCustomUi' which maps to "Arrow" or "Plain Text".) A formatting property for dropdowns and the choice for the Display Style definitely exists, but it is not visible in the JSON. Hopefully, we can get/set the property in the next API version (v5).

    Alternatively, this is not optimal at all, still, I have found it useful that we can COPY the dropdown setting once we create and decorate the dropdown manually, by using "copyPaste" request with "pasteType": "PASTE_DATA_VALIDATION".