每次使用onedit触发器时,Google App脚本不会触发

时间:2018-07-04 08:12:33

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

Google App脚本未使用编辑触发器触发所有JavaScript弹出框。为多个用户更改工作表时不会触发。也只能在感觉良好时使用。我已将触发器设置为onEdit()。我的代码:

function Error() {
  
  var sheet = SpreadsheetApp.getActiveSheet();
  
  if (sheet.getName() == "Protocal  Check List 2"){
  var ui = SpreadsheetApp.getUi(); // Same variations.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var active = ss.getActiveCell();
  var getactive = ss.getActiveCell().getDisplayValue();
  var column = ss.getActiveRange().getColumn();
  var row = ss.getActiveRange().getRow();
  var msg = '';
  var section = sheet.getRange('C'+ row);
    
  switch (column) {
    case 9:
        var msg = "error 1";
        break;
    case 10:
        var msg = "Error 2";
        break;
    default: 
        msg = "Error";
        break;
        }
   if(getactive == "TRUE"){
 
  if(column >= 9 && column <= 60
    ){ 
  var result = ui.alert(
      "pika  Says",
      msg,
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    window.alert('Task Complete: \n\n' + msg);
    active.setValue('True');
  } else {
    
      var i = 0;
      
      while (i < 1) {
      var result = ui.prompt(
      'Please detail cause of the problem:',
      ui.ButtonSet.OK);

      
      var text = result.getResponseText();
      
      var textcount = text.length;
      
      if(textcount > 0) {
      i++;
      }}
    
    var cell = "H" + row;
      
      var emailAddress = "email@gmail.com";
      var d = new Date();
      var n = d.toDateString();
      var t = d.toTimeString();
      var staffname = ss.getRange(cell).getValues();
      
      var message = "Date: " + n +"\n\nTime: " + t +"\n\nStaff: " + staffname + "\n\nError: " + msg + "\n\nProblem: " + text;
      var subject = msg;
      
      var thedate = n + " / " + t;
      
      ss.getRange('A1').setValue(thedate);
      ss.getRange('B1').setValue(staffname);
      ss.getRange('C1').setValue(msg);
      ss.getRange('D1').setValue(text);
     
      var s1 = ss.getRange('A1:D1'); //assign the range you want to copy

      var s1v = s1.getValues();
      
      var tss = SpreadsheetApp.openById('1mOzdRgKxiP5iB9j7PqUWKKo5oymWuAeQZ1jJ1s6qL9E'); //replace with destination ID

      var ts = tss.getSheetByName('AB Protocal'); //replace with destination Sheet tab name
      
      ts.getRange(ts.getLastRow()+1, 1, 1,4).setValues(s1v); //you will need to define the size of the copied data see getRange()
      
      MailApp.sendEmail(emailAddress, subject, message);
  
      // User clicked "No" or X in the title bar.
      //ui.alert("The following note has been sent to the Duty Manager: \n\n" + text);
      active.setValue('FALSE');
    
  }}}}
  
}

任何帮助将不胜感激。对于需要访问工作表的用户,我需要它在每个信号时间运行。

2 个答案:

答案 0 :(得分:1)

  

您必须   假定onEdit触发器是尽力而为的,但可能无法完全抓住   对电子表格进行的编辑。

错误线程为here

答案 1 :(得分:0)

要解决Edward中提到的错误,请实施一个哑路由器。

// create a new file -> router.gs
function routerOnEdit(e) {
   myOnEditHook1(e);
   myOnEditHook2(e);
   myOnEditHook3(e);
}