Accessing existing Google Sheet with Python - python

There's an existing google sheet that I need to read from with a Python script. Is there any way I can enable the Google Sheets or Drive API for an existing spreadsheet (so I can download the necessary credentials for a Python app)?
All the online guides tell you how to do this for a New Project (like this one or this one) but not an existing sheet. Thanks!

If you look at the Google Developers quickstart, you can see the constants
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'
If you look at your sheet URL, you'll see it has a separate ID. You can replace the spreadsheet ID in the code with your own URL, and change the range you're querying depending on your specific application.

Related

Using python gspread delete a spreadsheet with Client.del_spreadsheet()

What is the easiest way to delete a sheet created by an API service account? Using the gspread API, I was able to create a sheet, and then share back to myself as editor. Now I can't delete because I am not the owner.
name = 'mysheet'
gc = gspread.service_account(filename=path_to_credentials)
try:
spreadsheet = gc.open(name)
except gspread.SpreadsheetNotFound:
spreadsheet = gc.create(name)
sharewith = 'me#myedu.edu'
spreadsheet.share(sharewith, perm_type='user',
notify=True, role='editor')
I want to reuse the service account credentials, but I am having trouble thinking about how to get the credentials in a Google OAuth object suitable to use with gspread.Client(). Below kind of resembles what I am thinking, but I feel I am missing something.
oa = gspread.oauth(credentials_filename=path_to_credentials)
c = gspread.Client(auth=oa)
c.del_spreadsheet('<spreadsheetId>')
If anyone has insights to offer, I would listen.

How would I Use Python to Create a Google Sheet Similarly to R's googlesheets4 Package?

I'm trying to send a simple Pandas DataFrame to Google Sheets. It's way more complicated than I expected and I would appreciate some help.
Creating a Google Sheet in R is incredible simple - using the googlesheets4 package, I just have to run gs4_create('Sheet_Name', sheets = My_Data), sign in to my Drive account when it prompts me to, and Voila! the sheet is created.
I have extensively researched doing the same with Python. From everything I've gathered, I need to access the Google Developers Console, create a project, enable the API, create a service account, give that service account editor permission, generate credentials, store them on my machine, and then run a script that accesses my credentials, authorizes them, and then maybe if all that works I can create a Google Sheet. Given all of this can be accomplished in one line of R code, do you understand why I'm a little frustrated and confused?
Is there not a simpler way to do this?

getting spreadsheet from google sheets only if that is not deleted

I want to get particular spreadsheet from it's sheet id with get method of Google sheets api. i am using below method to get a speadsheet.
sheets_file = service.spreadsheets().get(spreadsheetId=sheets_id).execute()
Here sheet_id is the id of sheet which i want to get. However, this is even returning the sheet if it's moved to bin. I don't want that. i only want to get sheet with specified sheet_id if it's not deleted (or if it's not moved to bin). Can anyone please tell me how to do that.
What you are trying to do is out of scope for the Google sheets api, the work around would be to use the google drive api.
If you look at the documentation for Spreadshets.get
Returns the spreadsheet at the given ID. The caller must specify the spreadsheet ID.
Google sheets is just going to get you the sheet. If you send a valid sheet id its going to return it to you. Google sheets assumes that you know what you are asking for. It is not going to check if this file has been trashed or not, or what directory it resides in. As long as the file id exists and you have access to it then its going to return it to you.
workaround
If you want to check the current location of a file and check if its been trashed then you should go though the Google drive api
The files.get method will take your sheet id or file id. This method will return a file recourse this contains a property called trashed. So you will be able to see if the file has been trashed or not.
trashed boolean Whether the file has been trashed, either explicitly or from a trashed parent folder. Only the owner may trash a file. The trashed item is excluded from all files.list responses returned for any user who does not own the file. However, all users with access to the file can see the trashed item metadata in an API response. All users with access can copy, download, export, and share the file.
So the solution to your problem is to use a file.get from the google drive api to check first if the file has been trashed if it has not then you can load it with the google sheets api.

Is there any way to access a google sheets, and read it column by column, without the google API (in python)

I want to, for example, check column 3 of row 7, and see what is written there. I can't use the google sheets API
only if it has been published to the web, or by "downloading" the web view. There are some other methods, but these are "initiated" from the sheet itself, not from separate code not running on the sheet.

Check if spreadsheet exists

I'm developing an application that saves an acquired value to a google spreadsheet. I would like to know if I can do following things via google-spreadsheet API while using python
Check if a spreadsheet exists with given name
Get a list of spreadsheets while requesting via name or id
Thanks in advance!

Categories