You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Google Apps Script实现带逻辑的弹窗:无文本框+条件跳过

解决方案

修改要点

  1. 移除第一个弹窗的文本输入框:把原本的ui.prompt替换为ui.alert,alert仅展示YES/NO按钮,不会生成输入框,完全匹配需求。
  2. 控制第二个弹窗的显示逻辑:用变量记录第一个弹窗的选择结果,当用户选NO时,直接跳过第二个弹窗的执行流程。

修改后的完整代码

function TextBox() {
  var ui = SpreadsheetApp.getUi();
  const sheet = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT & WARM CLIENTS');
  sheet.sort(9);

  var today = Utilities.formatDate(new Date(), "GMT-6", "MM/dd/yy");
  var valuesToCopy = sheet.getRange("C5:J5").getDisplayValues().map(r => r.join(' \n '));

  // 第一个弹窗:仅YES/NO按钮,无输入框
  var firstQuestionMsg = valuesToCopy + "\n\n Did we do work for this client today/yesterday? (YES/NO)";
  var firstResponse = ui.alert(firstQuestionMsg, ui.ButtonSet.YES_NO);
  var didProvideService = firstResponse === ui.Button.YES;
  
  if (didProvideService) {
    sheet.getRange("G5").setValue(today);
    // 第二个弹窗:仅当选择YES时显示
    var secondQuestionMsg = valuesToCopy + "\n\n What did we help this client with today?";
    var secondResponse = ui.prompt(secondQuestionMsg, ui.ButtonSet.OK_CANCEL);
    if (secondResponse.getSelectedButton() == ui.Button.OK) {
      var lastActivity = sheet.getRange("H5").getValue();
      sheet.getRange("H5").setValue(lastActivity + " | " + secondResponse.getResponseText());
    }
  } else {
    Logger.log('The user clicked "No" or the dialog\'s close button.');
  }

  // 第三个弹窗:下次跟进时间
  var thirdQuestionMsg = valuesToCopy + "\n\n When should we follow up with this client next? (MM/DD/YY)";
  var thirdResponse = ui.prompt(thirdQuestionMsg, ui.ButtonSet.OK_CANCEL);
  if (thirdResponse.getSelectedButton() == ui.Button.OK) {
    sheet.getRange("I5").setValue(thirdResponse.getResponseText());
  }

  // 第四个弹窗:下次跟进内容
  var fourthQuestionMsg = valuesToCopy + "\n\n Next Follow Up Activity?";
  var fourthResponse = ui.prompt(fourthQuestionMsg, ui.ButtonSet.OK_CANCEL);
  if (fourthResponse.getSelectedButton() == ui.Button.OK) {
    sheet.getRange("J5").setValue(fourthResponse.getResponseText());
  }
}

代码说明

  • 用ui.alert替代ui.prompt实现第一个无输入框的YES/NO弹窗,同时用didProvideService变量存储用户选择状态。
  • 第二个弹窗的逻辑被包裹在didProvideService为true的条件判断中,用户选NO时直接跳过该步骤。
  • 移除了原代码中冗余的数组遍历结构,改为按顺序直接处理弹窗,逻辑更直观易懂。

内容的提问来源于stack exchange,提问作者Ricardo Barrios

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 08:50:27