Google Apps Script的google.script.run在Safari中无法运行的问题
Google Apps Script对话框在Safari 17.4.1中调用服务器函数报错
我在Google表格中用Apps Script开发了一个对话框,打开功能正常,但点击Go按钮时,本该触发handleGo函数并显示"GO-ing!"提示。这个功能在Chrome里完全正常,但在Safari 17.4.1中会弹出错误:
Error: ScriptError: We're sorry, a server error occurred. Please wait a bit and try again.
我查过JavaScript控制台和Apps Script执行日志,都没有额外的错误信息。已经试过关闭Safari的跨域限制和跨站跟踪防护,也确认登录的是正确的谷歌账号。
我的代码
Code.gs
function openDialog() { var html = HtmlService.createHtmlOutputFromFile('Dialog.html').setWidth(300).setHeight(150); SpreadsheetApp.getUi().showModalDialog(html, 'Test Dialog'); } function handleGo() { SpreadsheetApp.getActiveSpreadsheet().toast("GO-ing!") }
Dialog.html
<!DOCTYPE html> <html> <head> <base target="_top"> <script> function go() { google.script.run .withSuccessHandler(function() { alert("Function executed successfully"); }) .withFailureHandler(function(error) { alert("Error: " + error); }) .handleGo(); } </script> </head> <body> <button onclick="go()">Go</button> </body> </html>
解决方法尝试
- 给服务器函数添加冗余参数
Safari部分版本对无参数的google.script.run调用存在兼容问题,给函数加个空参数就能规避:
- 修改Dialog.html中的
go函数:
function go() { google.script.run .withSuccessHandler(function() { alert("Function executed successfully"); }) .withFailureHandler(function(error) { alert("Error: " + error); }) .handleGo({}); // 传入空对象作为冗余参数 }
- 同步修改Code.gs中的
handleGo函数:
function handleGo(dummyParam) { SpreadsheetApp.getActiveSpreadsheet().toast("GO-ing!") }
用隐私浏览模式测试
浏览器缓存或旧会话数据可能干扰请求,打开Safari隐私窗口,重新登录谷歌账号后测试功能。更新Safari到最新版本
苹果后续的小版本更新可能修复了这个特定兼容问题,检查并安装系统推送的Safari更新。更换HTML加载方式
尝试用模板加载替代直接加载HTML文件,有时候能绕过浏览器的兼容性限制:
修改Code.gs中的openDialog函数:
function openDialog() { var template = HtmlService.createTemplateFromFile('Dialog.html'); var html = template.evaluate().setWidth(300).setHeight(150); SpreadsheetApp.getUi().showModalDialog(html, 'Test Dialog'); }
内容的提问来源于stack exchange,提问作者Scott Brave
相关产品推荐
相关产品推荐

