我有一个电子表格,在其中打开带有订单详细信息的侧边栏,然后显示警报,询问用户是否准备好发送邮件。如果用户选择“取消”,我想停止整个脚本并关闭侧边栏。 google.script.host.close 给出了来自未定义错误的无法读取属性“脚本”。 我将如何简单地关闭侧边栏而无需手动进行操作?
// 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;
}
答案 0 :(得分:2)
如果我的理解是正确的,该解决方法如何?在这种解决方法中,通过用时间边栏覆盖来关闭边栏。我认为针对您的情况有几种解决方法。因此,请将此视为其中之一。
当这反映到您的脚本时,修改后的脚本将如下所示。
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
var html = HtmlService.createHtmlOutput("<script>google.script.host.close();</script>"); // Added
SpreadsheetApp.getUi().showSidebar(html); // Added
//cancel the script
return;
}
如果不是您想要的解决方法,对不起。
答案 1 :(得分:1)
没有直接方法可以从警报窗口关闭补充工具栏。但是,您可以使用解决方法。
当用户单击“取消”时,请在服务器端设置一个属性。
PropertiesService.getDocumentProperties().setProperty("CLOSED", "CLOSED");
在侧边栏中,设置一个计时器,该计时器每秒轮询一次此属性值。如果返回值是CLOSED,则关闭侧边栏。
// Server side
function checkClosedStatus() {
var props = PropertiesService.getDocumentProperties();
var value = props.getProperty("CLOSED");
if (value === "CLOSED") {
props.deleteProperty("CLOSED");
}
return value;
}
// Inside the sidebar html
google.script
.run
.withSuccessHandler(function(e) {
if (e === "CLOSED")
google.script.host.close();
})
.checkClosedStatus()
答案 2 :(得分:1)
我发现此解决方案有效。
function example(formObject) {
function onSucces(e) {
google.script.host.close();
}
// Inside the sidebar html
var runner = google.script.run.withSuccessHandler(onSucces)
.formHandlerFunction(formObject);
return;
}
使用withSuccessHandler使我成功了。
在完成其余脚本之前,我在执行google.script.host.close();
时遇到问题。