Search code examples
javascriptgoogle-apps-scriptgoogle-drive-apigoogle-drive-shared-drive

"Shared Drive" support in Google Apps Script


I am writing a JavaScript tool in Google Apps Script to check some properties of documents, like "are all links valid", "are permissions set correctly", and so on. I am using the API documented in https://developers.google.com/apps-script/reference/drive/drive-app to look up files by ID, check their permissions, locate them in Google Drive etc., but I found that "Shared Drives" don't work very nicely with that API.

For example,

  • for the root folder of a Shared Drive, Folder.getName() only returns "Drive" rather than the Drive's name,
  • even though mygroup@domain.com is a "Manager" of the Shared Drive, folder.getAccess('mygroup@domain.com') is NONE and folder.getViewers() is empty,
  • some folders in Shared Drives are not (always) included in the DriveApp.getFolders() iterator.

In particular the second point is a blocker for me now, but what am I missing here? Is there some other API I should be using, or is it simply a bug that I should report? Is there some documentation of what functionality of the Drive API I can and cannot use with Shared Drives?


Solution

  • Use the Advanced Drive Service instead of DriveApp

    • Indeed, shared drives are not supported by DriveApp which has a limited scope
    • But if you enable the Advanced Drive Service, yuo will be able to use in Apps Script all methods of the Drive API v2 which support shared drives

    Sample:

    function myFunction() {
      var sharedDriveName = Drive.Drives.get("XXXXXXXXXXXXXXXXXXX").name;
      //it is important to specify that the folder is located on a shared drive with {"supportsAllDrives": true}
      var folderOnDriveName = Drive.Files.get("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",{"supportsAllDrives": true}).title;
      var folderPermissions = Drive.Permissions.list("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",{"supportsAllDrives": true});
    }