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")
}
}

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

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.
- 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.
- 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(...).
- 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