Search code examples
google-sheetsgoogle-apps-scriptdialog

How to call a function based on message box response


I have been working on an Apps Script that allows me to automatically delete responses if an applicant failed an exam.

I am using this script:

function onEdit1(e) {
  const sh=e.range.getSheet();
  if(sh.getName()=='{I} Apps' && e.range.columnStart==13 && e.value=='TRUE') {
    var resp=SpreadsheetApp.getUi().alert('Did the applicant pass their exam?', SpreadsheetApp.getUi().ButtonSet.YES_NO);
    if(resp==SpreadsheetApp.getUi().Button.YES) {
      var resp=SpreadsheetApp.getUi().alert('Send out the appropriate letter so they can do their practical', SpreadsheetApp.getUi().ButtonSet.OK);
      if(resp==SpreadsheetApp.getUI().Button.OK){
        return;
      }
    }else{
      var resp=SpreadsheetApp.getUi().alert('By pressing OK, you confirm the applicant failed their exam, that you sent the correct letter and that their previous results can be removed.', SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
      if(resp==SpreadsheetApp.getUI().Button.OK){
        deleteExam();
        return;
    }
     
      return;
    }
  }
}

function deleteExam(){
    var sheet = SpreadsheetApp.getActive().getSheetByName('{I} Exams')
    var rows = sheet.getDataRange();
    var numRows = rows.getNumRows();
    var values = rows.getValues();

    var rowsDeleted = 0;
    for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    if (row[20] == 'delete') { // This searches all cells in columns A (change to row[1] for columns B and so on) and deletes row if cell has value 'delete'.
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
      rowsDeleted++;
    }
  }
}

For reference

{I} Apps -> my overview tab where I check a box, this triggers (right now also succesfully) the message box to appear.

{I} Exams -> a form submission tab.

It is good to know, that the individual functions work as intended (the message box appears , and if I would manually run the deleteExam function, this works too).

What I tried;

  • Using Triggers to have deleteExam work, but doesn't work
  • Putting the code from deleteExam right under the if response = OK where now deleteExam() is placed.
  • Having the functions on seperate scripts.
  • Triple checked the column number for deleteExam as well as name references

Does anyone have a suggestion on what to alter in order for the "deleteExam" function to work when I press "OK" on the confirmation message box?


Solution

  • I suspect that this doesn't work but perhaps you could explain why and then I might be able to suggest an alternative

    function onMyEdit(e) {
      const sh = e.range.getSheet();
      if (sh.getName() == '{I} Apps' && e.range.columnStart == 13 && e.value == 'TRUE') {
        var resp = SpreadsheetApp.getUi().alert('Did the applicant pass their exam?', SpreadsheetApp.getUi().ButtonSet.YES_NO);
        if (resp == SpreadsheetApp.getUi().Button.YES) {
          resp = SpreadsheetApp.getUi().alert('Send out the appropriate letter so they can do their practical', SpreadsheetApp.getUi().ButtonSet.OK);
          if (resp == SpreadsheetApp.getUI().Button.OK) {
            return;
          }
        } else {
          resp = SpreadsheetApp.getUi().alert('By pressing OK, you confirm the applicant failed their exam, that you sent the correct letter and that their previous results can be removed.', SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
          if (resp == SpreadsheetApp.getUI().Button.OK) {
            let sh2 = e.source.getSheetByName('{I} Exams')
            var vs = sh2.getRange(2,1,sh2.getLastRow() - 1, sh2.getLastColumn()).getValues();
            var d = 0;
            vs.forEach((r, i) => {
              if (r[20] == 'delete') {
                sh2.deleteRow((i + 1) - d++);
              }
            })
          }
        }
      }
    }