1

When I'm on my Google Sheet, I have a checkbox that triggers a Modal Dialog box when selected via an onEdit function. I've added in the trigger on the project and when the checkbox is selected, the dialog shows just fine and I do not see an error.

However, in the Executions log, it lists the execution twice, and one of them fails and the other processes just fine. I checked in the project Overview, and the listed scope is present, so I'm not sure what the issue is. Here is the onEdit function and the function that has the Modal Dialog box.

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var ui = SpreadsheetApp.getUi();
  var sheet = ss.getName();
  var cell = e.range;
  var row = cell.getRow();
  var col = cell.getColumn();
  var val = cell.getValue();
  if (sheet == "Emails" && col == 5 && row == 2) {
    addEmailValidations(ss,ui);
  } else if (sheet == "Emails" && col == 10 && row != 1 && val == true) {
    previewEmail(ss,ui,row);                            //Row 24 in error message below
  } else {
  }
}

function previewEmail(ss,ui,row) {
  console.log('previewemail start');
  var sendCheck = ss.getRange(row,8).getValue();
  var emailTemp = ss.getRange(row,9).getValue();
  if (emailTemp == "") {
    console.log('Stopped process: template not selected.');
    ui.alert("Error","Please select an Email Template.",ui.ButtonSet.OK);
  } else {
    if (sendCheck == false) {
      console.log('send was false');
      ss.getRange(row,8).setValue(true);
      SpreadsheetApp.flush();
    } else {
    }
    var subject = ss.getRange(row,14).getValue();
    var body = ss.getRange(row,15).getValue();
    var rec = ss.getRange(row,16).getValue();
    var htmlOut = HtmlService
      .createHtmlOutput('<font face="Arial"><b>To:</b> ' + rec + '<br /><b>Subject:</b> ' + subject + '<br /><hr><br />' + body + '</font>')
      .setWidth(800)
      .setHeight(450);
    ui.showModalDialog(htmlOut,'Email Preview');       //Row 113 in error message below
    ss.getRange(row,10).setValue(false);
  }
}

From the user perspective, the dialog box shows just fine, but here is the execution log:

execution logs: Trigger - successful. Simple Trigger - error message

Exception: You do not have permission to call Ui.showModalDialog.
Required permissions:
https://www.googleapis.com/auth/script.container.ui
    at previewEmail(Code:113:8)
    at onEdit(Code:24:5)

But when I go into the Overview, the correct scope is listed:

Scopes in overview

Is there something weird that I've done with my onEdit trigger that's causing this to happen? And yes, I've gone into the Triggers section and added in a trigger to run this function on edit.

Any help would be greatly appreciated - thanks!

Rubén
  • 34,714
  • 9
  • 70
  • 166
Josh
  • 75
  • 1
  • 10
  • Hi @Marios - I already have an installable trigger on my project (as I mentioned), and I've already searched online for this issue, I've found nothing, which is why I'm here. – Josh Jan 25 '21 at 22:25
  • Rename the function from `onEdit` to something else and install the onEdit again........... – Marios Jan 25 '21 at 22:29
  • This one was asked today itself: https://stackoverflow.com/q/65881141/11225291 – Marios Jan 25 '21 at 22:33
  • Another one with more views (easier to be found on Google): https://stackoverflow.com/a/49120695/11225291 – Marios Jan 25 '21 at 22:34
  • If you also see the screenshot you just posted it says that you have an `onEdit` simple trigger. When you use `onEdit` as a function name a **simple** `onEdit` trigger is installed. Therefore rename that function to something like `myFunction` and create an **installable** `onEdit` trigger for `myFunction`. What I say here is suggested by all the links I posted so you can see the details of the answers if you need a more detailed explanation. – Marios Jan 25 '21 at 22:38
  • 2
    Thanks! Sometimes it's hard to remember all the little details required to make things like this function correctly since I don't do this every day. I did find all those questions you linked before posting via search, but unfortunately glazed over the 'rename onEdit to something else' part since I've had success using that name for an installable trigger before. I'm sorry to have wasted your time. – Josh Jan 25 '21 at 22:49
  • you didn't. I am glad I can help people here. Didn't mean to be rude :) – Marios Jan 25 '21 at 22:53

0 Answers0