Scott Brave
Scott Brave

Reputation: 1

Google Apps Script "google.script.run" not working on Safari

I'm using Apps Script in Google Sheets. I open a dialog (which works fine) and then when I click the Go button I expect handleGo to run and display "GO-ing!". It works fine on Chrome, but on Safari (17.4.1) it alerts:

Error: ScriptError: We're sorry, a server error occurred. Please wait a bit and try again.

There is no additional information on the error in the Javascript console or in the app script execution log. I have tried disabling cross-origin restrictions as well as disabling prevent cross-site tracking in Safari. I am logged in with the correct google account.

Here's my code:

Code.gs

function openDialog() {
    var html = HtmlService.createHtmlOutputFromFile('Dialog.html').setWidth(300).setHeight(150);
    SpreadsheetApp.getUi().showModalDialog(html, 'Test Dialog');
}

function handleGo() {
    SpreadsheetApp.getActiveSpreadsheet().toast("GO-ing!")
}

Dialog.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function go() {
        google.script.run
          .withSuccessHandler(function() {
            alert("Function executed successfully");
          })
          .withFailureHandler(function(error) {
            alert("Error: " + error);
          })
          .handleGo();
      }
    </script>
  </head>
  <body>
    <button onclick="go()">Go</button>
  </body>
</html>

Upvotes: 0

Views: 415

Answers (1)

Wicket
Wicket

Reputation: 38347

This works fine on Safari 17.5

Code.gs

function onOpen(e) {
  SpreadsheetApp
    .getUi()
    .createMenu('Demo')
    .addItem('Show dialog', 'showDialog')
    .addToUi()
}


function showDialog() {
  const dialog = HtmlService.createHtmlOutputFromFile('index');
  SpreadsheetApp
    .getUi()
    .showModalDialog(dialog, 'A dialog');
}

function doSomething() {
  return 'something';
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <button>Click</button>
    <script>
      document.querySelector('button')
        .addEventListener('click', doSomething)

      function doSomething(){
        google.script
        .run
        .withSuccessHandler(successHandler)
        .withFailureHandler(failureHandler)
        .doSomething();
      } 

      function successHandler(){
        alert('Success')
      }

      function failureHandler(){
        alert('Failure')
      }
    </script>
  </body>
</html>

Upvotes: 0

Related Questions