0

For a script that documents all the spreadsheet files with references in formulas to another spreadsheet, I want to retrieve the last modifying user of a file. This apps script is only being used within the domain I am owning; the spreadsheet documents exist both in MyDrive as in our shared drive.

I am using the following statement:

var docModifierEmail = Drive.Files.get(docId).lastModifyingUser.emailAddress

Documentation is very hard to find The error I'm getting all the time is:

API call to drive.files.get failed with error: File not found: 1Nz0_Kme172EQXAwgW55d7H.....

The scope I am using:

>       "oauthScopes": [
>           "https://www.googleapis.com/auth/spreadsheets",
>           "https://www.googleapis.com/auth/userinfo.email",
>           "https://www.googleapis.com/auth/drive",
>           "https://www.googleapis.com/auth/drive.activity"  <--- not sure if this is needed

Questions that I have:

  1. Am I using the right function call?
  2. Am I using the right scope?
  3. What should be the right statement to retrieve the last modifying user's email address of the file with id docId?
Harold H
  • 15
  • 5
  • Please see both answer and comments of this [link](https://stackoverflow.com/a/28019679/11225291) and [this one](https://stackoverflow.com/questions/29606803/file-not-found-error-google-drive-api). – Marios Jan 14 '21 at 15:13

1 Answers1

1

The Drive API version 2 available in Apps Script needs at least one of the following scopes for Files.get():

Scope:

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.readonly
https://www.googleapis.com/auth/drive.metadata.readonly
https://www.googleapis.com/auth/drive.appdata
https://www.googleapis.com/auth/drive.metadata
https://www.googleapis.com/auth/drive.photos.readonly

When trying to access a file from your shared drive/team drive, You need to set the optional query parameter supportsAllDrives to true

Drive.Files.get("file id",{supportsAllDrives:true}).lastModifyingUser.emailAddress;

Additional Reference:

File Resource Representation

Ron M
  • 5,791
  • 1
  • 4
  • 16
  • Thank you Ron. I have added the scope to the already present list in APPSSCRIPT.JSON with no luck – Harold H Jan 14 '21 at 16:12
  • Verify first if the file exist in your Google Drive. I also don't think this is a scope issue since normally it would be handled successfully in GAS – Ron M Jan 14 '21 at 16:13
  • Thank you Ron. I have added the scope to the already present list in APPSSCRIPT.JSON with no luck. And I have tested with a hard coded docId of a file that truly exists. No luck either. Do you have a source where the LastModifyingUser is better documented? Or an example of working code. – Harold H Jan 14 '21 at 16:19
  • Maybe it's good to mention that the file is on a Shared drive (previously called Teamdrive). Would that make a difference? – Harold H Jan 14 '21 at 16:25
  • Thank you that was helpful, I was able to replicate your issue. Please see the updated answer – Ron M Jan 14 '21 at 16:42
  • Great! That was the problem. Do you know where I should have found this documented? – Harold H Jan 14 '21 at 21:29
  • It should be available in the [Drive.Files.gef()](https://developers.google.com/drive/api/v2/reference/files/get?hl=en) optional query parameters. – Ron M Jan 14 '21 at 21:30
  • 1
    Thank you Ron, I just upvoted your answer. – Harold H Jan 14 '21 at 21:31