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

Google Script点击链接按钮后自动关闭UI的实现求助

解决方案

你只需要给链接添加onclick事件,调用Google Apps Script内置的客户端方法google.script.host.close()即可实现点击链接时自动关闭UI对话框。修改后的完整代码如下:

function ServiceSetServiceSheets() {
  var html = '<a href="https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxx" target="_blank" onclick="google.script.host.close();">Open The Service Sheet</a>';
  var anchor = HtmlService.createHtmlOutput(html)
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setHeight(60)
    .setWidth(150);
  SpreadsheetApp.getUi().showModalDialog(anchor, "Click the link to");
}

关键修改说明:

  • 在<a>标签中新增onclick="google.script.host.close();":这个方法是Google Apps Script专门用于关闭当前模态对话框的客户端API,点击链接时会立即触发关闭操作。
  • 修正了原代码中字符串语法错误:原代码里的引号和分号位置有误,调整后确保HTML字符串格式正确。

当用户点击链接时,会先执行关闭对话框的操作,同时链接会在新窗口正常打开,两个操作互不干扰。

内容的提问来源于stack exchange,提问作者Ben C

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 15:25:48