【发布时间】:2022-01-21 09:01:59
【问题描述】:
我在从 Google 表格的 html 边栏中调用服务器端 Apps 脚本函数时遇到问题。
我已经用下面的简单示例代码复制了我的问题。
应该怎么做
单击该按钮应在我的 Code.gs 脚本中调用 alert 并向用户显示警报。
实际发生的情况
点击按钮显示错误:
We're sorry, a server error occurred while reading from storage. Error code PERMISSION_DENIED.
在 alert() 的 Apps 脚本仪表板的“执行”部分中没有任何条目,因此该函数似乎从未被调用过?
代码.gs:
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Matthew')
.addItem('Show Sidebar', 'sidebar')
.addToUi();
};
function sidebar() {
const html = HtmlService.createHtmlOutputFromFile('test.html')
.setTitle('Matthew Experiment')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
function alert() {
SpreadsheetApp.getUi().alert("alert!");
}
test.html(以及正文)
<body>
<div class="container">
<button class="btn btn-success" onclick="doSomething()">Click Me!</button>
</div>
<script>
function doSomething() {
google.script.run.withFailureHandler(function(error) {
console.log("error")
console.log(error.message)
}).withSuccessHandler(function(result) {
console.log("done!")
}).alert();
}
</script>
</body>
我已经确认客户端代码正确地使用了服务器端函数名称,它只是调用了不起作用的函数。
我的代码似乎与所有入门指南相同,但我没有看到其他人发布有关此问题的帖子。希望这里有人比我对这个问题有更深入的了解。
更新,更奇怪
我打开了一个私人窗口,它工作,所以我想,也许这是一个引起问题的扩展程序?但即使禁用所有扩展也不能在主窗口中修复它。我已经注销/登录到我的谷歌帐户,但如果不打开私人浏览器窗口,它仍然无法正常工作。
这个问题在 chrome/edge/firefox 中是一致的。
【问题讨论】:
-
根据 issuetracker 的说法,这仅在 chrome 中发生,当全局登录用户与脚本和电子表格的所有者不同时。
-
我既是脚本的所有者,也是电子表格的所有者,在 FF 中也是如此。 :-(
-
哦,是的,我的主要用户是我的 gmail 帐户,这是我的辅助帐户
-
@TheMaster 我会深入研究这个问题,你可能已经想通了
标签: javascript google-apps-script google-sheets web-applications