为什么我不能从 Google AppScript for Sheets 的侧边栏中调用服务器函数? [英] Why can't I call a server function from the sidebar in Google AppScript for Sheets?

查看:21
本文介绍了为什么我不能从 Google AppScript for Sheets 的侧边栏中调用服务器函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 Google 表格的 html 边栏中调用服务器端 Apps 脚本函数时遇到问题.

我已经用下面的简单示例代码复制了我的问题.

应该怎么做

单击该按钮应在我的 Code.gs 脚本中调用 alert 并向用户显示警报.

实际发生了什么

点击按钮显示错误:

很抱歉,从存储读取时发生服务器错误.错误代码 PERMISSION_DENIED.

Apps 脚本仪表板的执行"部分中没有 alert() 的条目,所以该函数似乎从未被调用过?

代码.gs:

function onOpen() {const ui = SpreadsheetApp.getUi();ui.createMenu('马修').addItem('显示侧边栏', '侧边栏').addToUi();};功能侧边栏(){const html = HtmlService.createHtmlOutputFromFile('test.html').setTitle('马修实验').setWidth(300);SpreadsheetApp.getUi().showSidebar(html);}功能警报(){SpreadsheetApp.getUi().alert("alert!");}

test.html(身体很好)

 <div class="container"><button class="btn btn-success";onclick="doSomething()">点击我!</button>

<脚本>函数 doSomething() {google.script.run.withFailureHandler(function(error) {控制台日志(错误")控制台日志(错误消息)}).withSuccessHandler(function(result) {console.log(完成!")}).警报();}

我已经确认客户端代码正确地使用了服务器端函数名称,它只是调用了不起作用的函数.

我的代码似乎与所有入门指南相同,但我没有看到其他人发布有关此问题的信息.希望这里有人比我更了解这个问题.

更新,更奇怪

我打开了一个私人窗口,它工作,所以我想,也许这是一个导致问题的扩展?但即使禁用所有扩展也不能在主窗口中修复它.我已经注销/登录到我的 Google 帐户,但如果不打开私人浏览器窗口,它仍然无法正常工作.

这个问题在 chrome/edge/firefox 中是一致的.

解决方案

当全局登录用户与脚本所有者不同时会发生这种情况.

注销您的所有 google 帐户,然后使用您用于创建脚本的帐户重新登录.这解决了问题.

I'm having problems calling a server-side Apps Script function from a html sidebar in Google Sheets.

I've replicated my issue with the simple example code below.

What it should do

Clicking the button should call alert in my Code.gs script and display an alert to the user.

What actually happens

Clicking the button shows the error:

We're sorry, a server error occurred while reading from storage. Error code PERMISSION_DENIED.

There are no entries in the 'executions' section of the Apps Script dashboard for alert(), so it seems like the function never even gets called?

Code.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 (well the body)

  <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>

I've confirmed that the client-code is getting properly inflated with server-side function names, it's just calling the function that doesn't work.

My code seems to be the same as all of the getting started guides, but I don't see anyone else posting about this issue. Hoping someone here has deeper knowledge of the problem than I do.

Update, even weirder

I opened a private window and it worked, so I thought, maybe this is an extension causing problems? But even disabling all extensions doesn't fix it in the main window. I've logged out/in to my google account but still can't get it to work without opening a private browser window.

This problem is consistent across chrome/edge/firefox.

解决方案

This happens when the global logged in user is different from the owner of script.

Logout all your google account and then re-login with the account you used to created the script. This fixed the issue.

这篇关于为什么我不能从 Google AppScript for Sheets 的侧边栏中调用服务器函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆