使用GAS将工作表上列出的查看器和编辑器添加到共享驱动器中的文件夹和文件中 [英] Add Viewers and Editors listed on Sheets to folders and files in Shared Drive using GAS

查看:55
本文介绍了使用GAS将工作表上列出的查看器和编辑器添加到共享驱动器中的文件夹和文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在共享驱动器中列出了需要与各种用户(如查看者和编辑者)共享的文件夹和文件的列表.当我复制文件夹和文件时,它没有复制用户.

I have a list of folders and files that need to be shared with various users (as Viewers and Editors) in Shared Drive. When I copied over the folders and files, it did not copy over the users.

我下面有一张纸,上面有以下信息.在将文件夹及其内容复制到共享驱动器之前,我先拉了查看器和编辑器.我的目标是运行代码以分别在Share Drive和查看器和编辑器中提取新的Folder或File ID.

I have a sheet with the following information below. I pulled the viewers and editors prior to copying the folder and its contents to Shared Drive. My goal is to run code to pull the new Folder or File Id in Share Drive and the viewers and editors respectively.

复制到共享驱动器以获取查看者和编辑者之前的信息:路径,名称,文件夹ID,用户访问权限,所有者,查看者,编辑者,文件夹/文件日期

Info prior to copying to Shared Drive to get the viewers and editors: Path, Name, Folder ID, User Access, Owner, Viewers, Editors, Folder/File Date

复制到共享驱动器后的信息:路径,名称,文件夹ID,用户访问权限,查看者,编辑者,文件夹/文件日期

Info after copying to Shared Drive: Path, Name, Folder ID, User Access, Viewers, Editors, Folder/File Date

我过去曾使用此代码,并且对于MyDrive文件夹共享(共享给< 3个人)来说工作得很好,但是当我尝试对Shared Drive文件夹/文件执行相同操作时,它似乎不起作用.有没有简单的方法来添加这些用户并以静默方式共享文件夹和文件?

I have used this code in the past and works just fine for MyDrive folder sharing (shared to <3 people) but when I try to do the same for a Shared Drive folder/files, it does not seem to work. Is there an easy way to add these users and silently sharing the folders and files?

 function setPermissions(){
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('sheetname');
  var start = 2;
  var last = sheet.getLastRow();
  var sheetValues = ssFilesSheet.getRange(start,1,last,ssFilesSheet.getLastColumn()).getValues();

  for (var i = 0; i < sheetValues.length; i++) {
    var row = sheetValues[i];   
    var id = row[0];
    var reader = row[3];
    var writer = row[4];

    var roles = {
  writer: [writer],
  reader: [reader]      
};

for(var key in roles) {
  var role = roles[key];
  role.forEach(function (email) {
    var resource = {
      role: key,
      type: "user",
      value: email
    }
    if(key == "commenter") {
      resource.role = "reader";
      resource.additionalRoles = ["writer"]
    }        
    Drive.Permissions.insert(resource, 
                             id, 
                             {sendNotificationEmails: false, 
                              // supportsTeamDrives:true,
                              supportsAllDrives: true, 
                              // useDomainAdminAccess:false });
                             });

  }
}

非常感谢您的时间和帮助!

Your time and help are greatly appreciated!

推荐答案

  • 要将用户设置为共享驱动器上文件的 writer reader ,截至目前,您需要设置请求参数 supportsAllDrives true ,因此在Apps脚本中:
    • To set a user as writer or reader for a file on a shared drive, as of now you need to set the request paramter supportsAllDrives to true, so in Apps Script:
    • Drive.Permissions.insert(
              {
                'role': 'owner',
                'type': 'user',
                'value': 'user email'
              },
              folder/fileid,
              {
                'sendNotificationEmails': 'false',
                'supportsAllDrives' : `true`
              });
      

        当前无法通过Drive API
      • 将用户设置为共享驱动器上文件的文件的所有者".已经有对此功能的要求,因此将来Google可能会合并此功能.
      • 更新

        根据更新后的代码,如果要提取位于同一单元格中的多个电子邮件地址,则需要将它们作为字符串检索,然后将其转换为数组,例如使用 split().

        Based on your updated code, if you want to extract several email addresses located in the same cell, you need to retrieve them as a string a convert them into an array e.g. with split().

        示例:

         for (var i = 0; i < sheetValues.length; i++) {
        
            var row = sheetValues[i];   
            var id = row[0];
            var reader = row[1];
            var writer = row[2];
        
            var roles = {
              writer: writer,
              reader: reader
            };
        
            for(var key in roles) {
              var role = roles[key];
              var roleArray = role.split(", ");
              roleArray.forEach(function (email) {
                if(email !="" && email !=" "){
                  var resource = {
                    role: key,
                    type: "user",
                    value: email
                  }
                  if(key == "commenter") {
                    resource.role = "reader";
                    resource.additionalRoles = ["writer"]
                  }        
                  Logger.log("value: " + email);
                  //Customize parameters sendNotificationEmails:false,supportsTeamDrives:true,useDomainAdminAccess:false
                  Drive.Permissions.insert(resource, 
                                           id, 
                                           {sendNotificationEmails: false, 
                                            // supportsTeamDrives:true,
                                            supportsAllDrives: true, 
                                            // useDomainAdminAccess:false });
                                           });
                }
              })
            };
        
          }
          ssFilesSheet.getRange(i+2,6).setValue('Added User Permissions');
        

        这篇关于使用GAS将工作表上列出的查看器和编辑器添加到共享驱动器中的文件夹和文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆