如何使取消按钮在 Google App Script 浏览器输入框中起作用

问题描述 投票:0回答:3
function sendEmails() {
  const sheet = getSpreadsheetApp()
  const sheetData = getSpreadsheetData(sheet).getValues();
  const subjectline = "Weekly Breakdown for the week of - "
  var subjectDate = Browser.inputBox('Please enter the date range for e-mail subject.', Browser.Buttons.OK_CANCEL);
    if(Browser.Buttons.CANCEL = true) {
        Browser.msgBox('The operation has been cancelled')
        return;
      }

  const subject = subjectline + subjectDate

在下面的代码中,即使我通过在输入框中输入详细信息来单击“确定”,它也会给我相同的消息框并停止运行脚本。

javascript google-apps-script google-sheets comparison-operators
3个回答
0
投票

Browser.inputBox 返回一个文本字符串。要查找是否按下了 Cancel 按钮,请使用以下命令:

    if (subjectDate === 'cancel') {

使用 Ui.prompt() 可以更好地检测输入的响应以及按下的按钮。


0
投票

这个:

if(Browser.Buttons.CANCEL = true) {
    Browser.msgBox('The operation has been cancelled')
    return;
  }

应该是:

if(Browser.Buttons.CANCEL == true) {
    Browser.msgBox('The operation has been cancelled')
    return;
  }

= is an assignment

== is a comparison

并尝试这个:

function sendEmails() {
  const ss = SpreadsheetApp.getActive();
  const sheet = ss.getSheetByName("Your Sheet Name")
  const sheetData = sh.getDataRange().getValues();
  const subjectline = "Weekly Breakdown for the week of - "
  var subjectDate = Browser.inputBox('Please enter the date range for e-mail subject.', Browser.Buttons.OK_CANCEL);
    if(Browser.Buttons.CANCEL == true) {
        Browser.msgBox('The operation has been cancelled')
        return;
      }

  const subject = subjectline + subjectDate
}

0
投票

不确定这何时成为一种选择,但这现在对我有用:

var ui = SpreadsheetApp.getUi();
var name = ui.prompt('Name:', ui.ButtonSet.OK_CANCEL).getResponseText();

if(ui.Button.CANCEL) {
  return;
}
© www.soinside.com 2019 - 2024. All rights reserved.