【问题标题】:Google App Scripts - Function calls with google.script.run always failingGoogle App Scripts - 使用 google.script.run 的函数调用总是失败
【发布时间】:2021-03-27 18:25:55
【问题描述】:

我试图简单地在侧边栏上获取一个按钮,调用我的 Code.gs 文件中的函数。希望我只是在做一些愚蠢的事情,但情况就是这样。

这是我的 Code.gs:

function onOpen() 
{
   var testMenu = SpreadsheetApp.getUi() 
      .createMenu('Custom Menu');

      testMenu.addItem('Show UI in sidebar', 'showSidebar');
      testMenu.addToUi();
};

function showSidebar(){
  var html = HtmlService.createTemplateFromFile('Page');
  SpreadsheetApp.getUi().showSidebar(html.evaluate());
}

function doWorkInAppsScriptFile()
{
    var foo = 'bar';
}

还有我的 Page.html:

<h2>google script run test</h2>
<div style="width:98%;margin:10px">
    <div style="width:98%;margin:10px;padding:10px;text-align:center">
        <input type="button"  id="saveButton" onclick="onSave()" value="Save" style="background:DarkGreen;color:White;font-weight:bold;margin:10px"/>
    </div>
</div>

<script type="text/javascript">

function onSave(){          
  google.script.run.doWorkInAppsScriptFile();     

}

</script>

使用此代码,我拉起电子表格中的侧边栏并单击按钮。然后,当我在项目编辑器中执行时,它显示了函数 doWorkInAppsScriptFile 的执行,但说它失败了。据我所知,它没有给我任何日志输出,但我可能找错了地方。我想也许我的函数中有一些我不完全理解的代码,所以我将它切换为只设置一个变量,但每次它仍然失败。

它显示执行的事实使我认为我的 Page.html 没有任何问题。 google.scripts.run 是否需要返回值?

我不知道我做错了什么,希望这里有人能指出一些明显的事情。

编辑:嗯,我想通了。当您有多个使用 google 登录的帐户时,看起来 google 脚本有点麻烦。当我没有对工作表和项目具有权限的用户设置为 google 中的默认帐户时,脚本将失败。

似乎在从侧边栏 html 到服务器代码的翻译中,它出于某种原因想要使用默认的 google 用户。 以前有人经历过吗?我可能会提出一个新问题,因为这似乎是一个新问题。

【问题讨论】:

  • 对我来说运行正常。
  • 奇怪,也许我有一些权限都搞砸了。

标签: google-apps-script


【解决方案1】:

我是这样运行它的:

function testhtml() {
const html='<h2>google script run test</h2><div style="width:98%;margin:10px"><div style="width:98%;margin:10px;padding:10px;text-align:center"><input type="button"  id="saveButton" onclick="onSave()" value="Save" style="background:DarkGreen;color:White;font-weight:bold;margin:10px"/></div></div><script type="text/javascript">function onSave(){google.script.run.testoe()}</script>';
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),'test');
}

function testoe() {
  Logger.log('do nothing');
}

日志: 2021 年 3 月 27 日 12:41:46 PM 信息什么也不做

【讨论】:

  • 试过了,结果一样。
  • 就像我说的那样对我有用。我建议删除电子表格并重新开始
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多