1

I want to get list all spreadsheets from Google drive by using Google drive API. I have wrote a script in python which is returning me all spreadsheets list from my Google drive but the issue is that this list also contains files (and folders) that are are in bin folder (that i have deleted). I don't want that list. i only want to get list of spreadsheets that are currently in my Google sheets account. Here is my code.

result = drive_service.files().list().execute().get('files', [])

drive_service is service instance that i have created to access user's google drive. It will be also helpful if someone tell that how to get list of files from google drive from only particular folder (like how to get list of all files that are only present in my_drive folder in google drive)

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Landi logan
  • 73
  • 1
  • 7

1 Answers1

1

you should look at the q parameter search-files

The following code would show only the sheets on your drive account.

response = drive_service.files().list(q="mimeType='application/vnd.google-apps.spreadsheet'",
                                          spaces='drive',
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()

You can add and trashed = false if you dont want to see the files that are in trash.

response = drive_service.files().list(q="mimeType='application/vnd.google-apps.spreadsheet' and trashed = false",
                                          spaces='drive',
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()

If you want to see only files in a specific folder then you can add parents in 'folder id'

response = drive_service.files().list(q="mimeType='application/vnd.google-apps.spreadsheet' and trashed = false and parents in 'FolderId'",
                                          spaces='drive',
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Can you please explain what is page_token ?. from where i will get it's value? – Landi logan Feb 14 '22 at 10:04
  • Thanks bro for the answer. You saved my day – Landi logan Feb 14 '22 at 10:09
  • Page token is only for paginating over additional requests. you only need it if you have more rows to return. – Linda Lawton - DaImTo Feb 14 '22 at 10:11
  • Bro is it possible to get files from only particular folder? Like suppose i have created a folder named My_sheets and i want to get files from only that folder. Is it possible to do so? – Landi logan Feb 14 '22 at 10:17
  • Princess you just need the file id of the my_sheets folder but yes just add parents in 'FolderId' . Heads up not everyone on SO is a *bro* [the-use-of-gender-specific-pronouns-on-stack-overflow](https://meta.stackoverflow.com/questions/262119/the-use-of-gender-specific-pronouns-on-stack-overflow) – Linda Lawton - DaImTo Feb 14 '22 at 10:18
  • This video will show you how to find the folder id. Just alter it a bit for python its the same principle. [Google Drive API Easiest way to find folder Id quick and simple.](https://www.youtube.com/watch?v=m3euwXcuvrs) Let me know if you need help though – Linda Lawton - DaImTo Feb 14 '22 at 10:22