getting spreadsheet from google sheets only if that is not deleted - python

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.

Related

How do I assign a google sheet a owner and save location using pygsheets?

I'm using pygsheets to work with google sheets and need to assign myself as the owner with my email and have it save to my google drive but I'm struggling to find a solution in the documentation.
The best I have is:
sheetbook=gs.create(gsheet_name)
sheetbook.share('my#email.com', role='writer')
I tried changing role to owner but come up with a error saying that transferOwnership needs a permission change and that's the issue of me not knowing how to do so. I know of the "folder=" option in create but I don't know how to link my google drive root folder.

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!

Accessing existing Google Sheet with 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.

Get file size from Google Drive Api (python)

I'm trying to figure out how to retrieve the file size of a file uploaded to Google Drive. According to the docs this should be in the file metadata... but when I request it, file size is not in the metadata at all.
file = self.drive_service.files().get(fileId=file_id).execute()
print(file)
>>> {u'mimeType': u'application/x-zip', u'kind': u'drive#file', u'id': u'0B3JGbAfem1CrWnhtWq5qYlkzSXf', u'name': u'myfile.ipa'}
What am I missing here? How can I check the file size?
Per default, only a few select attributes are included in the metadata.
To request specific attributes, use the fields parameter:
file = self.drive_service.files().get(fileId=file_id, fields='size,modifiedTime').execute()
This would query a file's size and modification time.
By the way, the link you posted refers to the old v2 API. You can find a list of all file attributes in the current v3 API here.
You are missing the 'fields' special query parameter here. It is used for giving partial response for google apis. The partial response is used mainly to improve api call performance.
There is a slight change in the newly introduced v3 apis, the file list apis response give some default attributes in the response, unlike the v2 apis, which give all the attributes in response by default.
Although, if you want all the attributes in the response, pass ' fields=* ' as query.
Hope, this helps!
Passing 'size' within fields should work.
Example: fields='size'
Although if the file is native to Google Drive such as a file made within Google Docs or Sheets those files do not take up space against your quota and thus don't have a size.

Categories