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

Added Conditional Formatting Rules logic #1509

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions gspread/spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,22 @@ def update_locale(self, locale: str) -> Any:
self._properties["locale"] = locale
return res

def list_conditional_formatting_rules(self, sheetid: int) -> List[Any]:
"""Lists the spreadsheet's conditional formats"""
sheets: List[Mapping[str, Any]] = self.fetch_sheet_metadata(
params={"fields": "sheets.properties,sheets.conditionalFormats"}
)["sheets"]

try:
sheet = finditem(
lambda sheet: sheet["properties"]["sheetId"] == sheetid, sheets
)

except StopIteration:
raise WorksheetNotFound("worksheet id {} not found".format(sheetid))

return sheet.get("conditionalFormats", [])

def list_protected_ranges(self, sheetid: int) -> List[Any]:
"""Lists the spreadsheet's protected named ranges"""
sheets: List[Mapping[str, Any]] = self.fetch_sheet_metadata(
Expand Down
20 changes: 20 additions & 0 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,26 @@ def delete_protected_range(self, id: str) -> JSONResponse:
}

return self.client.batch_update(self.spreadsheet_id, body)

def delete_conditional_formatting_rule(self, index: int) -> JSONResponse:
"""Delete conditional formatting rule identified by the index ``index``.

To retrieve the ID of a conditional formatting rule use the following method
to list them all: :func:`~gspread.Spreadsheet.list_conditional_formatting_rules`
"""

body = {
"requests": [
{
"deleteConditionalFormatRule": {
"sheetId": self.id,
"index": index,
}
}
]
}

return self.client.batch_update(self.spreadsheet_id, body)

def delete_dimension(
self, dimension: Dimension, start_index: int, end_index: Optional[int] = None
Expand Down
11 changes: 11 additions & 0 deletions tests/spreadsheet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,14 @@ def test_export_spreadsheet(self):
self.assertEqual(
values[0], res_values, "exported values are not the value initially set"
)

@pytest.mark.vcr()
def test_listing_conditional_format_rules(self):
"""Test listing conditional format rules of a spreadsheet"""

worksheet = self.spreadsheet.sheet1
worksheet.format("A1", {"backgroundColor": {"red": 1.0}})

rules = self.spreadsheet.list_conditional_formatting_rules(worksheet.id)

self.assertEqual(len(rules), 1)
25 changes: 25 additions & 0 deletions tests/worksheet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1927,3 +1927,28 @@ def test_add_validation(self):
# Further ensure we are able to access the exception's properties after pickling
reloaded_exception = pickle.loads(pickle.dumps(ex.exception)) # nosec
self.assertEqual(reloaded_exception.args[0]["status"], "INVALID_ARGUMENT")

@pytest.mark.vcr()
def test_delete_conditional_formatting_rule(self):
sheet = self.sheet
spreadsheet = self.spreadsheet
worksheet = self.spreadsheet.sheet1

# add a conditional format rule to the spreadsheet
worksheet.format(
"A1:A2",
{
"backgroundColor": {"green": 1, "blue": 1},
},
)

# list the conditions on the spreadsheet
rules = spreadsheet.list_conditional_formatting_rules(0)
self.assertEqual(len(rules), 1)

# delete the first rule by index
sheet.delete_conditional_formatting_rule(0)

# verify rule was removed
rules = spreadsheet.list_conditional_formatting_rules(0)
self.assertEqual(len(rules), 0)
Loading