diff --git a/README.md b/README.md index 94ef8db36..77d503cdf 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,14 @@ worksheet = sh.add_worksheet(title="A worksheet", rows="100", cols="20") sh.del_worksheet(worksheet) ``` +### Protecting / Unprotecting a Worksheet + +```python +worksheet.protect() +# Run your code without risk from other users (apart from the sheet owner!) +worksheet.unprotect() +``` + ### Getting a Cell Value ```python diff --git a/gspread/worksheet.py b/gspread/worksheet.py index 606b71a31..2b7080b26 100644 --- a/gspread/worksheet.py +++ b/gspread/worksheet.py @@ -1910,6 +1910,7 @@ def add_protected_range( description=None, warning_only=False, requesting_user_can_edit=False, + domain_users_can_edit=False, ): """Add protected range to the sheet. Only the editors can edit the protected range. @@ -1962,6 +1963,7 @@ def add_protected_range( "editors": { "users": editor_users_emails, "groups": editor_groups_emails, + "domainUsersCanEdit": domain_users_can_edit, }, } } @@ -2964,3 +2966,26 @@ def cut_range( } return self.spreadsheet.batch_update(body) + + def column_count(self): + """Full English alias for .col_count""" + return self.col_count() + + def list_protected_ranges(self): + """List protected ranges in current Worksheet""" + return self.spreadsheet.list_protected_ranges(self.id) + + def protect(self): + """Protect all ranges in current Worksheet""" + email = get_email_from_somewhere_tbd() # TODO ? + last_cell = rowcol_to_a1(self.row_count, self.col_count) + self.add_protected_range( + f"A1:{last_cell}", + email, + description="LOCKED by gspread user", + ) + + def unprotect(self): + """Unprotect all ranges in current Worksheet""" + for range in self.list_protected_ranges(): + self.delete_protected_range(range)