简体   繁体   English

Google 表格 - 允许用户在受保护范围内运行脚本

[英]Google Sheets - Allow Users to run scripts on a protected range

I'm new to Apps Script and have been slowing teaching myself.我是 Apps Script 的新手,并且一直在放慢自学速度。 I have searched Stack Over Flow and found similar questions and tried to work through the issue using the information found but thus far with no success.我搜索了 Stack Over Flow 并发现了类似的问题,并尝试使用找到的信息解决问题,但到目前为止没有成功。 Hopefully someone here can give a newbie some guidance.希望这里有人可以给新手一些指导。

I have a spreadsheet that I am sharing with other users.我有一个与其他用户共享的电子表格。 There are a few ranges with formulas in them that I am trying to protect.我试图保护一些带有公式的范围。 I have written a script that can change the forumlas within the protected ranges.我编写了一个脚本,可以在受保护范围内更改论坛。 It runs perfectly under my account but the shared users are unable to run the script because they "don't have permission".它在我的帐户下完美运行,但共享用户无法运行该脚本,因为他们“没有权限”。

I have tried to write a bit of code that tries to grant temporary access to edit the protected ranges.我试图编写一些代码来尝试授予临时访问权限以编辑受保护的范围。 My thought was to have three different functions.我的想法是具有三种不同的功能。 The first function removes the protections.第一个函数删除保护。 The second function performs the changes to the now unprotected ranges.第二个函数对现在不受保护的范围执行更改。 The third function then protects the ranges again.然后第三个函数再次保护范围。 The code below works perfectly under my account.下面的代码在我的帐户下完美运行。 However when logged in as a shared user it causes an alert saying that "You do not have permission to perform.."但是,当以共享用户身份登录时,它会发出警报,提示“您无权执行..”

Can someone please tell me where I am going wrong and point me in the right direction?有人可以告诉我哪里出错并指出正确的方向吗?

function myFunction() {
  //add protections back
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('Spray Template');
  var range = sheet.getRange('B15:E15');
  var range2 = sheet.getRange('G15:H15');
  var range3 = sheet.getRange('D16:E16');
  var ranges = [range,range2,range3];
  for (var j = 0; j<3; j++){
    ranges[j].protect().removeEditor('email_here@gmail.com');
  }
}


function myFunction2(){
//remove protections
var ss = SpreadsheetApp.getActive();
var ss2= ss.getSheetByName('Spray Template');  
var protections = ss2.getProtections(SpreadsheetApp.ProtectionType.RANGE);
 f
 or (var i = 0; i < protections.length; i++) {
   var protection = protections[i];

protection.addEditor('email_here@gmail.com');
SpreadsheetApp.flush();
protection.remove();   
 }
}

function test() {
  myFunction2();
  functionThatChangesRange();
  myFunction();
}

I imagine that without permission to edit/access those protected ranges, other people will also lack permission to edit the protection itself (even through scripts).我想如果没有编辑/访问这些受保护范围的权限,其他人也将缺乏编辑保护本身的权限(即使通过脚本)。

This is because the script inherits its permissions from the user that is running it (ie the script runs under that person's account).这是因为脚本从运行它的用户那里继承了它的权限(即脚本在那个人的帐户下运行)。

Obviously, you can't both protect a range, and also allow anyone to change the protection - that would defeat the whole purpose!显然,你不能既保护一个范围,又允许任何人改变保护——这将破坏整个目的!

I saw it suggested elsewhere to leave one cell unprotected and setup a script within the document that runs 'onedit'.我在其他地方看到有人建议让一个单元格不受保护,并在运行“onedit”的文档中设置一个脚本。 Whenever the user changes the value of that cell the script should run.每当用户更改该单元格的值时,脚本就应该运行。

One can use onEdit to run the script or installable trigger.可以使用onEdit来运行脚本或可安装的触发器。 However, I have not identified a way in which a script can be called from a custom menu or a button and edit successfully protected ranges.但是,我还没有确定可以从自定义菜单或按钮调用脚本并成功编辑受保护范围的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM