繁体   English   中英

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

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

我有一个谷歌电子表格,有 6 位编辑,包括我作为所有者。 当除工作表所有者之外的任何编辑器在下拉菜单中选择某个值时,附加到 classAttendance() 的 onEdit 触发器开始工作。 没关系。 但问题是,每次数据都受到所有者名称的保护(具有编辑权限),尽管用户是不同的编辑者。 它应该在该特定的编辑名称上,并且应该具有对其的编辑权限。 如何解决?

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);     
 
}

如可安装触发器的文档中所述:

可安装版本在创建触发器的用户的授权下运行,即使另一个具有编辑权限的用户打开电子表格也是如此。

现在, removeEditor()的文档说:

电子表格的所有者和当前用户都不能被删除。

换句话说:

  • 您的代码删除了所有编辑器减去触发器所有者 *you) 和电子表格所有者(可能还有您)
  • 声明protectionm.addEditor(me); 只会将您添加为编辑器(但无论如何您已经是编辑器)。
  • 此外,在removeEditos()之前使用addEditor是没有意义的——后者将删除之前添加的所有编辑器。

解决方案:

  • 使用事件 object user检索活动用户
  • 删除所有其他用户后将此用户添加为编辑器(请记住,您作为电子表格所有者无法从编辑器中删除)

实施示例:

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.

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