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

Protect/unprotect #1273

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,7 @@ def add_protected_range(
description=None,
warning_only=False,
requesting_user_can_edit=False,
domain_users_can_edit=False,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be added to the Sphinx docs below (in function docstring)

):
"""Add protected range to the sheet. Only the editors can edit
the protected range.
Expand Down Expand Up @@ -1962,6 +1963,7 @@ def add_protected_range(
"editors": {
"users": editor_users_emails,
"groups": editor_groups_emails,
"domainUsersCanEdit": domain_users_can_edit,
},
}
}
Expand Down Expand Up @@ -2964,3 +2966,26 @@ def cut_range(
}

return self.spreadsheet.batch_update(body)

def column_count(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I separated this out into #1274. It can be removed from here

"""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",
)
Comment on lines +2978 to +2986
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we try to call the API to add a new protected range but with no actual range. Something like, instead of giving: sheet1!A1:Z99 what if we give it: sheet1 as "range" does the API understand that we want to cover everything and then protects everything?? 🙃


def unprotect(self):
"""Unprotect all ranges in current Worksheet"""
for range in self.list_protected_ranges():
self.delete_protected_range(range)
Loading