0
function onOpen() {
  var ui=SpreadsheetApp.getUi();
  
  let buttonpushed=ui.alert("🔔 Polite Notice 🔔", "✅ Please ensure you have read the MUST READ tab before filling out your sheet. 🛒 Jump to sheet by using the MODULE INFO tab. 🌟 Thank you.", ui.ButtonSet.OK)
  if (buttonpushed=="CLOSE") {
    SpreadsheetApp.getActiveSpreadsheet().toast("💡 Remember you can quickly jump to your sheet via the MODULE INFO tab")
  } else if(buttonpushed=="OK") {
    SpreadsheetApp.getActiveSpreadsheet().toast("👍 You accepted me")
  }
}

There is something going on in the close section of the script and I cant figure out what it is.

When I run it, it says 'An unknown error has occurred, please try again later.'

1 Answer 1

1

You must improve your understanding of the capabilities and limitations of extending Google Sheets with Google Apps Scripts.

On the other side, there was a change on the SpreadsheetApp.getUi().alert(...) that introduced a bug when the user dismisses the alert by clicking outside the alert, pressing the Esc key, etc., the alert disappears, sometimes the script execution continues to be suspended, waiting for the alert response until the new user interface element is opened or the execution fails due to the execution time limit, other times it throws an error.

Please go to Pressing Escape or clicking outside Apps Script prompt leads to "Error resuming script execution" and click the star to inform Google that you are facing this issue.

Example

It is recommended once the bug is fixed.

function showAlert(){
   const ui = SpreadsheetApp.getUi();
   const response = ui.alert("message", ui.ButtonSet.OK);
   const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
   if(response == ui.Button.OK){ 
     spreadsheet.toast("OK message")
   } else {
     spreadsheet.toast("No OK message")   
   }
}

Google Sheets Alert

So that you know, the alert doesn't include a close button. If the user clicks outside the alert, then an error occurs.

Error message

The execution logs show (timestamp not included)

Info    Execution cancelled.

Easiest improvement

Instead of using ui.ButtonSet.OK, use ui.ButtonSet.OK_CANCEL. or ui.ButtonSet.YES_NO. The following example uses the first option. This way, the user can dismiss the alert; however, it doesn't avoid having an error when it is dismissed, i.e., by clicking outside the alert.

function showAlertWithCancelButton(){
   const ui = SpreadsheetApp.getUi();
   const response = ui.alert("message", ui.ButtonSet.OK_CANCEL);
   const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
   if(response == ui.Button.OK){ 
     spreadsheet.toast("OK message")
   } else {
     spreadsheet.toast("No OK message")   
   }
}

Another option is to use the HTML Service to create a dialog, which might be overkill for your needs.

Reference


The code has several issues.

  1. It uses a reserved function name, onOpen. Ref. https://developers.google.com/apps-script/guides/triggers. This is not a problem when the function is run from the Google Apps Script Editor, but when using it on a bounded script, it will run every time the document, form (in edit mode), presentation, and spreadsheet containing the script are opened. When this happens, the function runs in a limited authorization mode, causing some methods to fail.
  2. As mentioned above, when a function named onOpen is run due to opening the contained file, it can't execute several methods. This includes any method that opens a user interface, like SpreadsheetApp.Spreadsheet.toast(...).
  3. It uses strings instead of the corresponding Enum in the comparison expressions. This might not cause the execution to fail, but it's not a best practice.

Related

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.