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

Google Apps Script模态对话框单用户Chrome环境异常求助

Google Apps Script:Chrome下google.script.run未执行的问题

问题现象

  • 仅在Chrome浏览器中出现异常,Edge浏览器可正常运行
  • 多数账号使用该脚本无问题,但某一用户完成授权流程后,点击按钮能显示模态对话框,服务端函数getDinner()却从未执行(已通过项目执行页面确认),页面仅显示「steak and 」,撤销并重新授权无效

脚本逻辑

  1. 点击按钮触发服务端函数showModal(),生成并显示模态对话框
  2. 客户端页面通过google.script.run调用服务端getDinner(),将返回的potatoes插入页面span元素,形成完整文本「steak and potatoes」

相关代码

Code.gs

function showModal(){
  var templ = HtmlService.createTemplateFromFile('template');
  templ = templ.evaluate().setWidth(1500).setHeight(793);
  SpreadsheetApp.getUi().showModalDialog(templ,'Here\s what I had for Dinner:');
}

function getDinner(){
  return 'potatoes'
}

template.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      google.script.run.withSuccessHandler(showDinner).getDinner();

      function showDinner(dinner){
        document.getElementById("side").innerHTML = dinner
      }

    </script>
  </head>
  <body>
    <p>Steak and <span id="side"></span></p>
  </body>
</html>

排查与解决方法

  • 检查Chrome第三方Cookie设置
    Chrome严格的第三方Cookie限制可能阻断google.script.run请求:

    1. 进入Chrome设置 → 隐私和安全性 → Cookie和其他网站数据
    2. 开启「允许所有Cookie」,或手动将*.google.com、*.googleusercontent.com加入允许列表
    3. 若开启「仅允许第一方Cookie」,需添加Google Apps Script相关域名至例外
  • 禁用Chrome扩展
    广告拦截、隐私保护类扩展可能拦截跨域请求:

    1. 打开Chrome扩展程序页面,暂时禁用所有扩展
    2. 重新测试脚本,恢复正常后逐个排查触发问题的扩展
  • 强制刷新与重置Chrome设置

    1. 按下Ctrl+Shift+R(Windows/Linux)或Cmd+Shift+R(Mac)强制刷新页面,清除缓存
    2. 若无效,进入设置 → 高级 → 重置并清理 → 重置设置为原始默认值
  • 调整客户端调用时机并添加错误处理
    当前脚本在<head>中直接执行请求,可能因DOM未加载完成导致异常,同时添加失败处理便于排查:
    修改template.html的脚本部分:

    <script>
      document.addEventListener('DOMContentLoaded', function() {
        google.script.run
          .withSuccessHandler(showDinner)
          .withFailureHandler(function(err) {
            console.error('请求错误:', err);
            document.getElementById("side").textContent = '加载失败';
          })
          .getDinner();
      });
    
      function showDinner(dinner){
        document.getElementById("side").textContent = dinner;
      }
    </script>
    
  • 排查用户账号权限
    让用户尝试使用其他Google账号登录测试,排除账号本身的权限限制问题(如受限企业账号的执行限制)

内容的提问来源于stack exchange,提问作者Richie Travers

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 02:52:47