“Shared Drive” support in Google Apps Script

我怕爱的太早我们不能终老 提交于 2020-12-12 06:21:20

问题


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?


回答1:


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});
}


来源:https://stackoverflow.com/questions/62403468/shared-drive-support-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!