I have a spreadsheet where I open a sidebar with the order details and then an alert asking if user is ready to send the mail. If the user chooses cancel I want to stop the whole script and close the sidebar. google.script.host.close is giving a Cannot read property "script" from undefined error. How would I simply close the sidebar without the use having to manually do so?
// Display a modal dialog box in sidebar with custom HtmlService content to preview the order.
var htmlOutput = HtmlService.createHtmlOutput('<h1>'+ supplier + '</h1><br/>' + previewOrder)
.setTitle('Order Details');
SpreadsheetApp.getUi().showSidebar(htmlOutput);
// now also show an alert asking if you want to send the mail
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Confirm Sending','You are about to send this order to '+ supplier + ' (' + emailAddress + ') - are you sure?', ui.ButtonSet.YES_NO_CANCEL);
// Process the user's response.
if (response == ui.Button.YES) {
var subject = "Order for Tomorrow ";
MailApp.sendEmail(emailAddress,subject + dayname + " - " + Utilities.formatDate(tomorrow, "GMT+2", "d MMM") + "" ,emailBody, {to: emailAddress,cc: ccEmailAddress, htmlBody: emailBody});
}
else if (response == ui.Button.NO) {
//if user chooses NO then ignore and continue the loop
} else {
//close the sidebar
SpreadsheetApp.getUi().google.script.host.close();
//cancel the script
return;
}