Google apps script 关闭Google应用程序脚本中“警报”按钮的侧栏
我有一个电子表格,在那里我打开一个侧边栏,上面有订单的详细信息,然后会出现一个警报,询问用户是否准备好发送邮件。如果用户选择取消,我想停止整个脚本并关闭侧栏google.script.host.close从未定义的错误中给出了无法读取属性“script”。 我如何简单地关闭侧边栏而不用手动关闭Google apps script 关闭Google应用程序脚本中“警报”按钮的侧栏,google-apps-script,web-applications,sidebar,Google Apps Script,Web Applications,Sidebar,我有一个电子表格,在那里我打开一个侧边栏,上面有订单的详细信息,然后会出现一个警报,询问用户是否准备好发送邮件。如果用户选择取消,我想停止整个脚本并关闭侧栏google.script.host.close从未定义的错误中给出了无法读取属性“script”。 我如何简单地关闭侧边栏而不用手动关闭 // Display a modal dialog box in sidebar with custom HtmlService content to preview the order. var
// 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;
}
//在侧栏中显示带有自定义HtmlService内容的模式对话框,以预览订单。
var htmlOutput=HtmlService.createHtmlOutput(''+supplier+'
'+previewworder)
.setTitle(“订单详情”);
SpreadsheetApp.getUi().showSidebar(htmlOutput);
//现在还显示一个提示,询问您是否要发送邮件
var ui=SpreadsheetApp.getUi();
var response=ui.alert('确认发送','您将要将此订单发送给'+供应商+'('+电子邮件地址+')-确定吗?',ui.ButtonSet.YES\u NO\u取消);
//处理用户的响应。
如果(响应==ui.Button.YES){
var subject=“明天的订单”;
MailApp.sendmail(emailAddress,subject+dayname+“-”+Utilities.formatDate(明天,“GMT+2”,“d MMM”)+”,emailBody,{to:emailAddress,cc:ccEmailAddress,htmlBody:emailBody});
}
else if(响应==ui.Button.NO){
//如果用户选择否,则忽略并继续循环
}否则{
//关闭侧边栏
SpreadsheetApp.getUi().google.script.host.close();
//取消脚本
返回;
}
无法直接从警报窗口关闭侧栏。但是,您可以使用变通方法
PropertiesService.getDocumentProperties().setProperty("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()
没有直接的方法从警报窗口关闭侧栏。但是,您可以使用变通方法
PropertiesService.getDocumentProperties().setProperty("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()
- 您想关闭已经打开的侧边栏
- 您想通过其他不是边栏脚本的函数来实现上述功能
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;
}
var ui=SpreadsheetApp.getUi();
var response=ui.alert('确认发送','您将要将此订单发送给'+供应商+'('+电子邮件地址+')-确定吗?',ui.ButtonSet.YES\u NO\u取消);
//处理用户的响应。
如果(响应==ui.Button.YES){
var subject=“明天的订单”;
MailApp.sendmail(emailAddress,subject+dayname+“-”+Utilities.formatDate(明天,“GMT+2”,“d MMM”)+”,emailBody,{to:emailAddress,cc:ccEmailAddress,htmlBody:emailBody});
}
else if(响应==ui.Button.NO){
//如果用户选择否,则忽略并继续循环
}否则{
//关闭侧边栏
var html=HtmlService.createHtmlOutput(“google.script.host.close();”);//添加
SpreadsheetApp.getUi().showSidebar(html);//已添加
//取消脚本
返回;
}
注:
- 当这个修改后的脚本在主侧栏未打开的情况下运行时,临时侧栏会立即打开。例如,如果您不想这样做,请在打开主侧栏时使用PropertiesService设置主侧栏的存在。这样,当主侧栏未打开时,可以防止打开临时侧栏
- 您想关闭已经打开的侧边栏
- 您想通过其他不是边栏脚本的函数来实现上述功能
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;
}
var ui=SpreadsheetApp.getUi();
var response=ui.alert('确认发送','您将要将此订单发送给'+供应商+'('+电子邮件地址+')-确定吗?',ui.ButtonSet.YES\u NO\u取消);
//处理用户的响应。
如果(响应==ui.Button.YES){
var subject=“明天的订单”;
MailApp.sendmail(emailAddress,subject+dayname+“-”+Utilities.formatDate(明天,“GMT+2”,“d MMM”)+”,emailBody,{to:emailAddress,cc:ccEmailAddress,htmlBody:emailBody});
}
else if(响应==ui.Button.NO){
//如果用户选择否,则忽略并继续循环
}否则{
//关闭侧边栏
var html=HtmlService.createHtmlOutput(“google.script.host.close();”);//添加
SpreadsheetApp.getUi().showSidebar(html);//已添加
//取消脚本
返回;
}
注:
- 当这个修改后的脚本在主侧栏未打开的情况下运行时,临时侧栏会立即打开。例如,如果您不想这样做,请在打开主侧栏时使用PropertiesService设置主侧栏的存在。这样,当主侧栏未打开时,可以防止打开临时侧栏
如果此解决方案不是您想要的,我很抱歉。我发现此解决方案有效
function example(formObject) {
function onSucces(e) {
google.script.host.close();
}
// Inside the sidebar html
var runner = google.script.run.withSuccessHandler(onSucces)
.formHandlerFunction(formObject);
return;
}
使用withSuccessHand