1
votes

Google App script not firing all JavaScript pop up boxes using on edit trigger. Does not fire when a change is made to the sheet for multiple users. Also only works when it feels like it. I have set the trigger to onEdit(). My code:

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 protected]";
      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');
    
  }}}}
  
}

Any help would be appreciated. I need it to run every signal time for users who have access to the sheet.

2

2 Answers

4
votes

You'll have to assume that onEdit triggers are best-effort, but may not catch all edits done to the spreadsheet.

Bug thread is here.

0
votes

To get around the bug as mentioned by Edward implemente a dumb router.

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