【问题标题】:How to obtain a list of all users with access to a Google shared drive using apps script如何使用应用程序脚本获取有权访问 Google 共享驱动器的所有用户的列表
【发布时间】:2020-10-05 06:02:50
【问题描述】:

我使用 getEditors 获取电子表格的编辑者列表,返回的列表包括共享云端硬盘用户。但是,对共享驱动器具有“内容管理员”访问权限的用户不包含在列表中。为什么会这样?

我还发现 getAccess 可用于获取特定用户对驱动器文件夹的访问类型。使用这种方法,我的目标是识别所有具有 FILE_ORGANIZER 或 ORGANIZER 权限的用户。见official documentation on permissions。 是否可以使用 if 语句或循环来获取此信息?

或者,是否有一种解决方法来获取我可能没有考虑过的可以访问共享驱动器的所有用户的列表?

PS:高级驱动服务未启用。

// This code aims to protect a sheet in a spreadsheet 
// by granting specific users access (super users) 
// and revoking the access of any other user that can edit the spreadsheet. 

/*
Attempted approach:

user1 and user2 have manager permission to the shared drive as a whole
user3 has content manager permission to the shared drive as a whole
user4 only has access to this spreadsheet in the shared drive

My objective is to ensure that only users 1 and 2 can edit the protected sheet.

Result:
Log shows that users 1 and 2 have access and user 4 does not.
However, user3 can still edit the sheet because they were no included in the getEditors() result hence their access could not be revoked. 
*/

function protectASheet() {
  var superusers = ['user1', 'user2'];
  var ss = SpreadsheetApp.getActive();
  var editors = ss.getEditors(); //get file editors
  var sheet = SpreadsheetApp.getActive().getSheetByName('TestSheet');

  //Set protection  
  var protection = sheet.protect().setDescription('This sheet is protected.');

  protection.addEditors(superusers); //Grant superusers edit permission
  protection.removeEditors(editors); //Revoke other file editors' permission

  Logger.log("Only the following can edit " + sheet.getSheetName() + ": " + protection.getEditors());


}

【问题讨论】:

  • 请分享您的代码示例和之前的尝试,以便更好地了解您的问题
  • 嗨@Alessandro,感谢您的回复。我已经编辑了帖子以展示我最初的方法。

标签: google-apps-script google-drive-api google-drive-shared-drive shared-drive


【解决方案1】:

从同事那里得到了一些帮助。 这种方法依赖于在脚本编辑器中激活的高级驱动服务。要打开,请转到资源 -> 高级 Google 服务。

它毫无例外地获取对文件或文件夹具有任何形式访问权限的每个用户的权限和其他详细信息。

代码如下:

function getPermissionsList() {
  const fileId = "<FILE, FOLDER OR SHARED DRIVE ID HERE>"; // ID of your shared drive

  // THIS IS IMPORTANT! The default value is false, so the call won't 
  // work with shared drives unless you change this via optional arguments
  const args = {
    supportsAllDrives: true
  };

  // Use advanced service to get the permissions list for the shared drive
  let pList = Drive.Permissions.list(fileId, args);

  //Put email and role in an array
  let editors = pList.items;
  var arr = [];

  for (var i = 0; i < editors.length; i++) {
    let email = editors[i].emailAddress;
    let role = editors[i].role;

    arr.push([email, role]);

  }

  Logger.log(arr);

}

【讨论】:

  • 仅供 2022 年阅读本文的其他人参考 - 我不知道是否有名称更改或合并,或者我刚开始使用 Apps 脚本时发生了什么,但“服务”下没有列出任何内容今天说“高级驱动器”。但是,有一个只是说“驱动器”,当我添加它时,我能够毫无问题地运行这个脚本。
猜你喜欢
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 2012-02-18
相关资源
最近更新 更多