Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Conditional Formatting #525

Open
andrewgross opened this issue Mar 27, 2018 · 7 comments
Open

Add support for Conditional Formatting #525

andrewgross opened this issue Mar 27, 2018 · 7 comments

Comments

@andrewgross
Copy link

Hey,

Thanks for making this library, it has significantly lowered the bar to working with Google Sheets for me. It would be awesome to have support for Conditional Formatting on columns.

https://developers.google.com/sheets/api/samples/conditional-formatting

It looks like it could fit well in to the range style notation, with some kwargs for the formatting options. If I get a chance I will create a PR.

@mathben
Copy link

mathben commented Apr 3, 2018

I am interested to this feature too.
I think this issue is blocked by #482

@andrewgross
Copy link
Author

Gspread 2.0 should be on APIv4.

@alugowski
Copy link

FYI, you can easily make your own calls into the API from within gspread. Given a worksheet reference, i.e. wks = sh.sheet1 or somesuch:

body = {'requests': [
    {'repeatCell': {
        'range': {'startRowIndex': 0, 'endRowIndex': 1},
        'cell': {'userEnteredFormat': {'textFormat': {'bold': True}}},
        'fields': 'userEnteredFormat.textFormat.bold',
    }}
]}
wks.spreadsheet.batch_update(body)

Where body is the request as you see in your sample link. The conditional formatting one is long, so instead the above body makes the first row bold.

You can copy/paste from Google's examples, just remove the sheetId keys as gspread populates those for you.

@big-c-note
Copy link

big-c-note commented May 28, 2019

FYI, you can easily make your own calls into the API from within gspread. Given a worksheet reference, i.e. wks = sh.sheet1 or somesuch:

body = {'requests': [
    {'repeatCell': {
        'range': {'startRowIndex': 0, 'endRowIndex': 1},
        'cell': {'userEnteredFormat': {'textFormat': {'bold': True}}},
        'fields': 'userEnteredFormat.textFormat.bold',
    }}
]}
wks.spreadsheet.batch_update(body)

Where body is the request as you see in your sample link. The conditional formatting one is long, so instead the above body makes the first row bold.

You can copy/paste from Google's examples, just remove the sheetId keys as gspread populates those for you.

Thanks! I was getting this error:

No grid with id: 0

Realized that I needed to add:

body = {'requests': [
    {'repeatCell': {
        'range': {'sheetId': sheetId, 'startRowIndex': 0, 'endRowIndex': 1},
        'cell': {'userEnteredFormat': {'textFormat': {'bold': True}}},
        'fields': 'userEnteredFormat.textFormat.bold',
    }}
]}

where sheetId = https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit#gid={sheetId}
https://stackoverflow.com/questions/46696168/google-sheets-api-addprotectedrange-error-no-grid-with-id-0

@vikrant-pune
Copy link

FYI, you can easily make your own calls into the API from within gspread. Given a worksheet reference, i.e. wks = sh.sheet1 or somesuch:

body = {'requests': [
    {'repeatCell': {
        'range': {'startRowIndex': 0, 'endRowIndex': 1},
        'cell': {'userEnteredFormat': {'textFormat': {'bold': True}}},
        'fields': 'userEnteredFormat.textFormat.bold',
    }}
]}
wks.spreadsheet.batch_update(body)

Where body is the request as you see in your sample link. The conditional formatting one is long, so instead the above body makes the first row bold.

You can copy/paste from Google's examples, just remove the sheetId keys as gspread populates those for you.

Maybe this is too old,

but while trying to use this I'm getting error as .
TypeError: string indices must be integers
with stack tace as

  File "../venv/lib/python3.8/site-packages/gspread/utils.py", line 592, in wrapper
    return f(*args, **kwargs)
  File "../venv/lib/python3.8/site-packages/gspread/models.py", line 1186, in batch_update
    data = [
  File "../venv/lib/python3.8/site-packages/gspread/models.py", line 1187, in <listcomp>
    dict(vr, range=absolute_range_name(self.title, vr['range']))`
for

uploaded = d2g.upload(df, spreadsheet_key, wks_name, credentials=creds, row_names=False)
uploaded.batch_update(body)`

where uploaded is of type <class 'gspread.models.Worksheet'>

@lavigne958
Copy link
Collaborator

Hi @andrewgross, this is definitely something that can be done with gspread. You can do it yourself like mentioned by @alugowski but a helper function in theWorksheet object could be nice.

Hi @vikrant-pune, we you mention is a different issue and is getting off-topic. Please open a new issue with you message, backtrack and you code snippet and we'll help you from there. Thank you.

@lavigne958 lavigne958 added the Bug label May 17, 2021
@lavigne958
Copy link
Collaborator

In order to add conditional formating, one could make a new PR and introduce this feature. This can be done using other requests as example and using the following documentation:

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#addconditionalformatrulerequest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants