自动化权限,我不断在那里获得额外的人

时间:2016-10-13 14:29:30

标签: google-apps-script permissions google-sheets

所以我讨厌权限,他们很糟糕。但是,当人们写出他们没有意识到的重要内容时,我也很讨厌...我很高兴发现我可以使用谷歌脚本自动完成整个授权许可。

所以我在一个小的虚拟表中测试了一些东西,并按照我想要的方式运行。然而!当我在实际工作表的测试模板上使用它时,我将需要使用它,它会(由于某种原因)将我共享文件的其他人添加到所有权限级别

这不是我想要的。另一个人目前是我(在此脚本中的SHOULDN' TBEINEVERYTHING@WHYYYYY.com),但该帐户应该具有较低级别的权限,因此我可以确保一切按照我希望的方式工作。这是我使用的代码的虚拟版本:

function plus10Protection() {

var ss = SpreadsheetApp.getActive();

    // Removes protections from Ranges
 var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
 for (var i = 0; i < protections.length; i++) {
   var protection = protections[i];
   if (protection.canEdit()) {
     protection.remove();
   }
 }

      //removes sheet protections
 var protections = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET);
 for (var i = 0; i < protections.length; i++) {
   var protection = protections[i];
   if (protection.canEdit()) {
     protection.remove();
   }
 }

  //SHEET1
  var sh7 = ss.getSheetByName("Sheet1");
  var protection = sh7.protect().setDescription('Sheet1 - Wizards');  
//protects whole sheet
  protection.addEditors(['wizard1@test.com', 
                         'wizard2@test.com']); 
  //adds email addresses to WIZARD range - SHEET1

  var unprotected = sh7.getRange('A1:D');
  protection.setUnprotectedRanges([sh7.getRange("B1:B1"),
                                   sh7.getRange("B3:D")]); 
  //unprotects

  var range = sh7.getRange('B1:B1'); //selects the range
  var protection = range.protect().setDescription('Sheet1 - Part Number'); 
  //names the range
  var me = Session.getEffectiveUser();
  protection.addEditors(['wizard1@test.com',
                         'wizard2@test.com',
                         'supervisor1@test.com']); 
  //put emails here SHEET1 SUPERVISORS   

  var range = sh7.getRange('B3:D'); //selects the range
  var protection = range.protect().setDescription('Sheet1 - Workers');
  //names the range
  var me = Session.getEffectiveUser();
  protection.addEditors(['wizard1@test.com',
                         'wizard2@test.com',
                         'supervisor1@test.com'
                         'worker1@test.com'
                         'worker2@test.com'
                         'SHOULDN'TBEINEVERYTHING@WHYYYYY.com']);  
  //put emails here SHEET1 WORKERS 

重复大约十五个其他标签。我真的希望这能解决人们离开或加入时更新权限的问题,因为这个脚本会删除旧权限然后再添加新权限。但是,该表已经与公司的每个人共享,如果突然每个人都有向导级别的访问权限,那么它几乎是最差的。

我通过与另一个人分享来再次测试它,并且它们也被添加到所有内容中。是否有一些代码段指定与我提供的电子邮件共享,而不是所有可能与之共享的人? (停止过度兴奋,谷歌。)

我不想与所有人分享,然后重新分享,因为这似乎是个大问题。我希望我在网上某个地方窃取的代码中有一些东西(我应该将它加入书签!!!)实际上是在呼叫每个人都有编辑权限....也许是getEffectiveUser?虽然我是从我的test1@test.com帐户运行它,而不是从另一个在不同浏览器中打开的帐户运行它! (Firefox和我在Chrome中编写脚本。)

我还测试过是否可能是因为我已登录并使用我的SHOULDN&#39; TBEINEVERYTHING@WHYYYYY.com帐户查看表单,但不是,但不是。

脚本共享是否可能?所以我可以首先取消与大家的共享,清除权限,设置新权限,然后重新分享?如果这是唯一的解决方案?我可以做得越少越好。 :S

我试图在谷歌论坛论坛上找到这个问题的答案,但到目前为止没有咬人。我希望能够投入更广泛的网络。

1 个答案:

答案 0 :(得分:0)

仍然没有想出如何更改用户的权限级别,但是由于我找到了一些代码(我认为我已经添加了书签,但现在无法找到)我可以删除每个人的权限,运行我的保护脚本,然后再与所有人分享。

   //removes all Editors
  var SpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
  var editors = SpreadSheet.getEditors();
  for (var i = 0; i < editors.length; i++) {
     SpreadSheet.removeEditor(editors[i]);
    };

然后

  //Adds all editors back.
  var SpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
  var editors = SpreadSheet.getEditors();
     SpreadSheet.addEditors(['PUT EMAILS HERE', 'separated by commas']);

我需要了解如何将运行脚本的人员专门添加到权限中,以防万一并非所有者运行脚本。今晚的任务!