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

Google Apps Script TypeError:response.getSelectedButton不是函数问题

解决Google Apps Script中response.getSelectedButton is not a function错误

嘿,刚接触JS和Google Apps Script遇到这种问题很正常,我来帮你理清楚问题出在哪:

错误原因

你这里的问题是对ui.alert()方法的返回值理解有误。当你调用ui.alert('Change parameters?', ui.ButtonSet.YES_NO)时,这个方法直接返回的就是用户点击的按钮枚举值(比如ui.Button.YES或者ui.Button.NO),而不是一个包含getSelectedButton()方法的对象。

之前你可能混淆了ui.alert()和ui.prompt()的返回值:ui.prompt()返回的是PromptResponse对象,这时候才需要调用getSelectedButton()来获取用户选择;但alert()的返回值更直接,不需要额外调用方法。

修正后的代码

把你的判断逻辑改成直接比较返回值即可:

function Mathdoc() { 
  var ui = DocumentApp.getUi(); 
  var d = new Date(); 
  var s = (d.getDate()) + '/' + (d.getMonth()+1) + '/' + d.getFullYear(); 
  console.log(s); 
  var response = ui.alert( 'Change parameters?', ui.ButtonSet.YES_NO); 
  // 直接比较response和Button枚举值
  if (response == ui.Button.YES) { 
    // 你的其他代码
  } 
}

小补充

如果之后你用到ui.prompt()(比如需要用户输入内容的弹窗),那时候才需要用response.getSelectedButton()来判断用户点了确认还是取消,比如:

var promptResponse = ui.prompt('请输入学科名称');
if (promptResponse.getSelectedButton() == ui.Button.OK) {
  var subject = promptResponse.getResponseText();
  // 处理输入内容
}

内容的提问来源于stack exchange,提问作者Fishy Apprentice

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 15:52:29