如何使用应用程序脚本获取有权访问Google共享驱动器的所有用户的列表

问题描述

我使用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());


}

解决方法

得到了同事的帮助。 此方法依赖在脚本编辑器中激活高级驱动器服务。要打开,请转到“资源”->“高级Google服务”。

它会获取对任何具有对文件或文件夹的任何访问形式的用户的权限和其他详细信息,而没有例外。

以下代码:

0.63.2