错误:尝试删除文件或文件夹的权限时没有此类用户

时间:2019-04-23 20:04:48

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

我正在使用google-app-scripts撤消对Google驱动器文件和文件夹的权限,我循环浏览我的google驱动器文件和文件夹,当我发现该用户有权访问该文件时,我尝试撤消该文件,但随后例外:“没有此类用户”发生,我找不到发生这种情况的任何原因。

function run() {
    var emailsToRevoke = ["EmailA", "EmailB"];
    revoke(emailsToRevoke);
}

function revoke(emails){
  var files = DriveApp.getFiles();
  var folders = DriveApp.getFolders();

  emails.forEach(function(email){
    try {
      while(files.hasNext()) {
        var file = files.next();

        if(file.getAccess(email) !== DriveApp.Permission.NONE) {
          file.revokePermissions(email)
        }

        var editors = file.getEditors();
        var viewers = file.getViewers();

        console.log("Checking Permission for: " + file)

        for (var i = 0; i < editors.length; i++) {
          if(editors[i].getEmail() === email) {
            console.log("Revoking Permission to " + file.getName() + " for: " + email )
            file.removeEditor(email)
          };
        }

        for (var i = 0; i < viewers.length; i++) {
          if(viewers[i].getEmail() === email) {
            console.log("Revoking Permission to " + file.getName() + " for: " + email )
            file.removeViewer(email)
          };
        }

        doubleCheckUser(file, email)
      }

      while(folders.hasNext()) {
        var folder = folders.next();


        if(folder.getAccess(email) !== DriveApp.Permission.NONE) {
          folder.revokePermissions(email)
        }

        var editors = folder.getEditors();
        var viewers = folder.getViewers();

        console.log("Checking Permission for: " + folder)

        for (var i = 0; i < editors.length; i++) {
          if(editors[i].getEmail() === email) {
            console.log("Revoking Permission to " + folder.getName() + " for: " + email )
            folder.removeEditor(email)
          };
        }

        for (var i = 0; i < viewers.length; i++) {
          if(viewers[i].getEmail() === email) {
            console.log("Revoking Permission to " + folder.getName() + " for: " + email )
            folder.removeViewer(email)
          };
        }

        doubleCheckUser(folder, email);
      }
    } catch(error) {
      console.error("Error During Revoking Access Process: " + error)
    }   
  });
}

function doubleCheckUser(objectToCheck, email) {
  try {
    var access = objectToCheck.getAccess(email)
    if(access !== DriveApp.Permission.NONE) {
      console.warn("Access to: " + objectToCheck.getName() + " has not been revoked properly. Current Access: " + access)
    }
  } catch(error) {
    console.error("Error Double Checking If User: " + email + " Was Removed Properly From: " + objectToCheck.getName())
  }
}

我确定在我将电子邮件登录到doubleCheckUser()时,该电子邮件具有对文件的访问权限,我也确保该电子邮件有效。

它的触发方式只是用户将电子邮件添加到emailsToRevoke数组,然后在google脚本用户界面中选择运行功能,然后按运行。

我的问题是,我做错了什么以及如何解决此问题?最终,我需要做的就是撤销访问权限,因此,如果您知道更好的处理方法,请不要犹豫。

编辑---
请注意,该驱动器中有很多文件(肯定超过1000个文件和文件夹)

0 个答案:

没有答案