Open PFython opened 1 year ago
Hi thank you for this generous proposal, we'll have a close look at it when we have time.
It does look interesting. As long as you needed to do these wrappers that means we are lacking some features.
We'll be back here with more details and we'll happy if you can provide a PR to introduce some of it.
Thanks - I was reluctant to submit a PR because I didn't know whether to overwrite/modify an existing class or just add it as an optional wrapper/utility. If you can direct me a little bit as to how/where to try grafting it on, I'll be happy to give it a try, but actually suspect you'll be able to do so quicker, knowing the codebase as you do :)
Hi, great issue! Thanks for taking the time to make these suggestions.
I have some replies, some may be opinionated. I will give a small overview of each attribute/method you suggest.
For reference, I use book
and Spreadsheet
interchangeably, and also sheet
and Worksheet
the same.
.view()
Your code is already succinct. I would suggest using gspread.Spreadsheet.url to open the spreadsheet in a browser, with your own code. This means we do not have to add a dependency (webbrowser
).
import webbrowser
def open_in_browser(book):
webbrowser.open(book.url)
...
open_in_browser(book)
.protect()
and .unprotect()
These sound like they would be good additions to gspread! I could eventually give it a go to implement them, but I implore you to give it a try:
For .protect()
, I would:
gspread.Worksheet.protect_all
which:
gspread.Worksheet.add_protected_range
with parameters:gspread.Worksheet.row_count
& gspread.Worksheet.col_count
For .unprotect()
, I would:
gspread.Worksheet.unprotect_all
which:
gspread.Spreadsheet.list_protected_ranges
to find all protected rangesgspread.Worksheet.delete_protected_range
to delete them allHave a look at the Contributing Guide for how to get started with the repository (and tests!)
.df
and pandas.DataFrame.to_gspread()
Personally, I would keep converting between these in your own code. In my opinion, there is no need for these to be internal methods of in either library. In the same way as view()
above. Check the documentation for suggestion on how to convert to/from pandas.
.email
and .account_path
These are both variables which are required for creating a gspread.Client
in the first place. I do not see why they should belong to the Spreadsheet.
You can find the email in your credentials.json
file, and you use the account_path
in your code to create the gspread instance, so you can just use those if you need either variable.
Thanks for the issue! Hopefully you can give implementing protect/unprotect a go!
Many thanks for the constructive and friendly reply! Yep I'll definitely have a go now you've pointed me in the right direction - probably next week now :) Please feel free to assign this Issue to me!
I saw you've got a list_protected_ranges
method so have updated my snippets above and will follow your suggestions for protect/unprotect shortcuts.
All the best!
Super! Let us know if you need help with anything!
@PFython We would still love your help on this, if you are interested.
If you no longer wish to, then I will make a PR adding protect/unprotect shortcuts in the coming weeks.
Thanks for your patience and gentle prod @alifeee!
I'm almost finished but got stuck because gspread.Worksheet.add_protected_range()
requires an email. As far as I can tell from auth.py
the Client
attribute .auth._service_account_email
is only created upon initialisation, so unless .email
is added to the Spreadsheet or Worksheet (like my original, verbose, solution) I'm not sure how to recreate it?
Thanks in advance for any insight! Pete
Ah, I see the issue.
Did you get remove
working?
If you are happy to, I'd suggest you submit a draft PR so we don't lose out on any code you wrote. Then, we can take a look at it with our knowledge of the codebase and see if we can think of a good solution. auth is more in @lavigne958's area of knowledge.
Thanks guys - I just submitted a draft PR: https://github.com/burnash/gspread/pull/1273
All the best, Pete
PR exists, but we are unsure how to get the email needed for the protected range. See https://github.com/burnash/gspread/pull/1273#pullrequestreview-1580026601
Is your feature request related to a problem? Please describe. First of all, thanks for all the effort you've put into this package already - it's really helped me on my latest project.
I created the following general purpose 'wrapper' for quickly creating a Worksheet/Tab based on key, title, sheet name, sheet index, or account path. It also includes convenience methods for:
.view()
: Opening a worksheet in your browser directly.protect()
: Protecting a whole sheet (except for the service account running the code).unprotect()
: Unprotecting a whole sheet.df
: Converting directly to a Pandas DataFramepandas.DataFrame.to_gspread()
: Saving a Pandas DataFrame (created using.df
) back to its associated worksheetI've also added convenience attributes to the worksheet object:
.email
: the service account email (needs to be 'invited' to share a Google Sheet before having access).account_path
: path to the service account JSON fileIt wasn't immediately obvious to me where to add these in your structure otherwise I'd have created a pull request but the basic code is below if you think this would be a helpful utility to include?
Describe the solution you'd like
If you like (some or all of) this and want to incorporate it, I'd be happy submit relevant updates to the README doc.