google.script.run.withSuccessHandler仅本人可用,其他用户失效求助
问题原因及解决方案
核心问题
其他用户无法关闭初始Dialog的根本原因是userPromptcustomFunction函数中存在未定义的ui变量,导致脚本执行报错,进而使withSuccessHandler绑定的关闭Dialog逻辑无法触发。你自身能正常运行属于临时上下文巧合,但其他用户执行时,未声明的ui变量会直接抛出错误,脚本执行失败后,withSuccessHandler不会被触发,初始弹窗也就无法关闭。
修复步骤
修正函数内变量缺失问题:
在userPromptcustomFunction内部声明并获取UI实例,避免引用未定义变量。修复后的代码如下:function userPromptcustomFunction(){ var ui = SpreadsheetApp.getUi(); currentMonth = ui.prompt("Choose Month to Perform Operation.", ui.ButtonSet.YES_NO); }也可以直接简化为:
function userPromptcustomFunction(){ currentMonth = SpreadsheetApp.getUi().prompt("Choose Month to Perform Operation.", SpreadsheetApp.getUi().ButtonSet.YES_NO); }优化HTML事件逻辑(可选):
将内联事件改为脚本绑定,同时增加错误处理,方便排查问题:<p> App Modes </p> <p class="body" style="font-family: sans-serif; color:gray; text-align:left"> Options:</p> <body> <p> <input type="button" id="no" value="RUN" /> </p> <script> document.getElementById('no').addEventListener('click', function() { google.script.run .withSuccessHandler(function() { google.script.host.close(); }) .withFailureHandler(function(error) { alert('运行出错:' + error.message); }) .userPromptcustomFunction(); }); </script> </body>检查共享权限:
确认已给其他用户授予表格编辑权限(仅查看权限无法运行脚本);若脚本做了独立部署,需将部署权限调整为对应范围(如组织内任何人、任何人等),绑定在表格内的脚本则只需表格编辑权限即可运行。
额外优化点
- 去掉
StartPopup函数的async关键字(未使用await,无必要保留); - 给
currentMonth添加var或let声明,避免全局变量污染。
内容的提问来源于stack exchange,提问作者Txa
相关产品推荐
相关产品推荐

