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

Feature: Add "Remove tab colour" method #1199

Merged
merged 19 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/user-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ Deleting a Worksheet
sh.del_worksheet(worksheet)


Updating a Worksheet's name and color
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python

worksheet.update_title("December Transactions")
worksheet.update_tab_color({"red": 1, "green": 0.5, "blue": 0.5})


Getting a Cell Value
~~~~~~~~~~~~~~~~~~~~

Expand Down
54 changes: 40 additions & 14 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ def is_gridlines_hidden(self):

@property
def tab_color(self):
"""Tab color style."""
"""Tab color style. Dict with RGB color values.
If any of R, G, B are 0, they will not be present in the dict.
"""
return self._properties.get("tabColorStyle", {}).get("rgbColor", None)

def _get_sheet_property(self, property, default_value):
Expand Down Expand Up @@ -1412,41 +1414,65 @@ def update_title(self, title):
self._properties["title"] = title
return response

def update_tab_color(self, color):
def update_tab_color(self, color: dict):
"""Changes the worksheet's tab color.
Use clear_tab_color() to remove the color.

:param dict color: The red, green and blue values of the color, between 0 and 1.
"""
red, green, blue = color["red"], color["green"], color["blue"]
body = {
"requests": [
{
"updateSheetProperties": {
"properties": {
"sheetId": self.id,
"tabColor": {
"red": color["red"],
"green": color["green"],
"blue": color["blue"],
},
"tabColorStyle": {
"rgbColor": {
"red": color["red"],
"green": color["green"],
"blue": color["blue"],
"red": red,
"green": green,
"blue": blue,
}
},
},
"fields": "tabColor,tabColorStyle",
"fields": "tabColorStyle",
}
}
]
}

response = self.spreadsheet.batch_update(body)
self._properties["tabColorStyle"] = {
"rgbColor": color,

sheet_color = {
"red": red,
"green": green,
"blue": blue,
}
self._properties["tabColor"] = color

self._properties["tabColorStyle"] = {"rgbColor": sheet_color}
return response

def clear_tab_color(self):
"""Clears the worksheet's tab color.
Use update_tab_color() to set the color.
"""
body = {
"requests": [
{
"updateSheetProperties": {
"properties": {
"sheetId": self.id,
"tabColorStyle": {
"rgbColor": None,
},
},
"fields": "tabColorStyle",
},
},
],
}
response = self.spreadsheet.batch_update(body)
self._properties.pop("tabColorStyle")
return response

def update_index(self, index):
Expand Down
Loading