Home > front end >  getting spreadsheet from google sheets only if that is not deleted
getting spreadsheet from google sheets only if that is not deleted

Time:02-19

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.

CodePudding user response:

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