Alexander Holzinger
Alexander Holzinger

Reputation: 3

Why is my google apps script with an onEdit trigger not invoking a modal dialog (html)?

In a google sheet I am trying to open an html dialog box when the user edits a specific cell (D4 in my testsheet). Content of script and html file below. The onEdit trigger is working and the function which should open the modal dialog is being called, but the html dialog won't open. But if I simply run the dialog function (OpenMyHtmlDialog) it opens.

I am not a programmer and I mostly copy stuff together and try to understand it...

Any ideas or hints would be appreciated.

The script file looks like this:

function onEdit(e) {
  const specificSheet = "OpenHtmlDialogOnTrigger"   // for example
  const specificCell = "D4"       // for example

  let sheetCheck = (e.range.getSheet().getName() == specificSheet)
  let cellCheck = (e.range.getA1Notation() == specificCell)

  if (!(sheetCheck && cellCheck)) {
    return
  }
  else {
    OpenMyHtmlDialog();
  }
}

function OpenMyHtmlDialog() {
  // just to check if function is actually called
  SpreadsheetApp.getUi().alert("Function was called.");
  //Open HTML dialog
  var htmlDlg = HtmlService.createHtmlOutputFromFile('HTML_myHtmlDialog')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setWidth(200)
      .setHeight(150);
  SpreadsheetApp.getUi()
      .showModalDialog(htmlDlg, 'A Title Goes Here');
};

The html file like this:

<select name="nameYouWant">
  <option value="something">Text</option>
  <option value="anything">Drop Down Selection</option>
</select>

<hr/>
<ul>
  <li>This is a list.</li>
  <li>This is line two.</li>

</ul>

<button onmouseup="closeDia()">Close</button>

<script>
  window.closeDia = function() {
    google.script.host.close();
  };
</script>

Upvotes: 0

Views: 772

Answers (1)

Amir Hossain
Amir Hossain

Reputation: 154

Your code is OK.

The problem is in the authentication scope. I generally avoid the onEdit() function for this reason. Instead, you can add the trigger manually. This will solve the problem (in that case, you can use any name for the function).

  1. Go to Triggers
  2. Click "Add trigger"
  3. Configure the trigger as like the image file and click "Save"

This will automatically invoke the necessary authentication scope.

enter image description here

Upvotes: 3

Related Questions