简体   繁体   English

具有 onEdit() 触发器的 Google App Scripts 中除所有者之外的其他编辑器的数据保护问题

[英]Data protection issue with other editors except owner in Google App Scripts having onEdit() trigger

I have a google spreadsheets with 6 editors including me as a owner.我有一个谷歌电子表格,有 6 位编辑,包括我作为所有者。 When any editor except the owner of the sheets selects certain value in the dropdown menu, the onEdit trigger attached with classAttendance() starts working.当除工作表所有者之外的任何编辑器在下拉菜单中选择某个值时,附加到 classAttendance() 的 onEdit 触发器开始工作。 That's fine.没关系。 But the issue is, every time the data is protected on the owner name (has edit access) though the user is different editor.但问题是,每次数据都受到所有者名称的保护(具有编辑权限),尽管用户是不同的编辑者。 It should be on that specific editor name and should have edit access to it.它应该在该特定的编辑名称上,并且应该具有对其的编辑权限。 How to solve it?如何解决?

function classAttendance(e){ 
  var spreadsheet = SpreadsheetApp.getActive();
  var dashboard = spreadsheet.getSheetByName("Dashboard");
  var sheetName = dashboard.getRange("A4").getValue();    
  
  if (e.range.getA1Notation() === 'C6' && e.range.getValue() === "Start 1-Period") {  
      refreshSheet();
      onePeriod();    
  }
  if (e.range.getA1Notation() === 'C6' && e.range.getValue() === "Start 2-Period") {    
      refreshSheet();
      twoPeriod();
  }    
}

function refreshSheet() {   
  //For protecting dashboard while scripts running
  var spreadsheet = SpreadsheetApp.getActive();  
  var dashboard = spreadsheet.getSheetByName("Dashboard");
   
  var rangem = dashboard.getRange("A1:K71");
  
  var timeZone = Session.getScriptTimeZone();
  var stringDate = Utilities.formatDate(new Date(), timeZone, 'dd/MM/yy HH:mm');
  var me = Session.getEffectiveUser();
  var description = 'Scripts running on ' + stringDate + ' by ' + me;
  
  var protectionm = rangem.protect().setDescription(description);  
  protectionm.addEditor(me);
  protectionm.removeEditors(protectionm.getEditors());
  if (protectionm.canDomainEdit()) {
      protectionm.setDomainEdit(false);
  }  
 
 Utilities.sleep(300000);     
 
}

As specified in the documentation for installable triggers:如可安装触发器的文档中所述:

The installable version runs with the authorization of the user who created the trigger, even if another user with edit access opens the spreadsheet.可安装版本在创建触发器的用户的授权下运行,即使另一个具有编辑权限的用户打开电子表格也是如此。

Now, the documentaiton for removeEditor() says:现在, removeEditor()的文档说:

Neither the owner of the spreadsheet nor the current user can be removed.电子表格的所有者和当前用户都不能被删除。

In other words:换句话说:

  • Your code removes all editors minus the trigger owner *you) and the spreadsheet owner (probably also you)您的代码删除了所有编辑器减去触发器所有者 *you) 和电子表格所有者(可能还有您)
  • The statement protectionm.addEditor(me);声明protectionm.addEditor(me); will only add you as an editor (but you are already an editor anyway).只会将您添加为编辑器(但无论如何您已经是编辑器)。
  • Also, it does not make sense to use addEditor before removeEditos() - the latter will remove all editors that have been added before.此外,在removeEditos()之前使用addEditor是没有意义的——后者将删除之前添加的所有编辑器。

Solution:解决方案:

  • Use the event object user to retrieve the active user使用事件 object user检索活动用户
  • Add this user as an editor after removing all the other users (remember that you as the spreadsheet owner cannot be removed from the editors)删除所有其他用户后将此用户添加为编辑器(请记住,您作为电子表格所有者无法从编辑器中删除)

Sample for implementation:实施示例:

function classAttendance(e){
//////////MODIFICATION HERE
  var user = e.user; 
  var spreadsheet = SpreadsheetApp.getActive();
  var dashboard = spreadsheet.getSheetByName("Dashboard");
  var sheetName = dashboard.getRange("A4").getValue();    
  
  if (e.range.getA1Notation() === 'C6' && e.range.getValue() === "Start 1-Period") {  
//////////MODIFICATION HERE
      refreshSheet(user);
      onePeriod();    
  }
  if (e.range.getA1Notation() === 'C6' && e.range.getValue() === "Start 2-Period") {  
//////////MODIFICATION HERE  
      refreshSheet(user);
      twoPeriod();
  }    
}
//////////MODIFICATION HERE
function refreshSheet(user) {   
  //For protecting dashboard while scripts running
  var spreadsheet = SpreadsheetApp.getActive();  
  var dashboard = spreadsheet.getSheetByName("Dashboard");
   
  var rangem = dashboard.getRange("A1:K71");
  
  var timeZone = Session.getScriptTimeZone();
  var stringDate = Utilities.formatDate(new Date(), timeZone, 'dd/MM/yy HH:mm');
  var me = Session.getEffectiveUser();
  var description = 'Scripts running on ' + stringDate + ' by ' + me;
  
  var protectionm = rangem.protect().setDescription(description);  
  protectionm.removeEditors(protectionm.getEditors());
//////////MODIFICATION HERE
  protectionm.addEditor(user);
  if (protectionm.canDomainEdit()) {
      protectionm.setDomainEdit(false);
  }   
 Utilities.sleep(300000);      
}

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

相关问题 onEdit触发Google脚本不起作用 - onEdit trigger google scripts not working 在Google Apps脚本中编写onEdit触发器 - Writing onEdit trigger in Google Apps Scripts Google App 脚本从保护中删除编辑器 - Google App script Remove Editors From Protection 如何从Google表格和应用脚本的撤消历史记录列表中删除onEdit触发器采取的操作 - How to remove actions taken by onEdit trigger from the undo history list in Google Sheets and App Scripts Google App Scripts OnEdit触发器帮助可避免“太多同时调用” - Google App Scripts OnEdit Trigger Help to avoid “too many simultaneous invocations” 在google工作表的google脚本中的特定工作表上使用onedit()触发器 - use onedit() trigger on a specific sheet within google scripts for google sheets google scripts onedit->范围的电子邮件触发器会为每次编辑生成一封电子邮件 - google scripts onedit -> email trigger for range produces an email for each edit 如何在 Google Sheet 中组合多个 onEdit/Trigger 脚本? - How can I combine multiple onEdit/Trigger Scripts in a Google Sheet? 从远程 onedit 可安装触发器访问其他谷歌电子表格 - accessing other google spreadsheet from a remote onedit installable trigger Google Spreadsheets-为什么我的脚本对我来说是文档所有者,但对共享编辑器不起作用? - Google Spreadsheets - why do my scripts work for me the doc owner but not shared editors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM