【问题标题】:HTML Dialog in Google Sheets google.script.run. fails to run Apps Script functionGoogle 表格 google.script.run 中的 HTML 对话框。无法运行 Apps 脚本功能
【发布时间】:2020-10-26 23:52:30
【问题描述】:

我正在使用 Google 表格与一位年长的朋友进行远程游戏(获得游戏发行商的许可)。

我正在尝试使用带有按钮的 HTML 对话框来触发某些脚本。我已经完成了我可以在文档和 Yagisanatode 的博客中找到的所有示例。除了google.script.run.addEgg() 没有触发 Code.gs 中的addEgg() 函数之外,所有部分都有效。

我将包含相关的代码。在进一步构建之前,我正在努力让一个按钮触发一个脚本。

EggFoodTuck01(),通过单击ObjectOverGrid 触发,成功启动对话框

function EggFoodTuck01(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('Decks'));
  sheet.getRange('CurrentCard').setValue(0); // Record Card Number in 'CurrentCard' Cell (0-14)
  sheet.getRange('CurrentSheet').setValue('Player 1'); // Record Sheet Name in 'CurrentSheet'
  
  // Launch dialog
  var html = HtmlService.createHtmlOutputFromFile("EFTDialog");
  ui.showModalDialog(html, "What would you like to do?");
}

HTML 对话框正确显示。单击 [+ Egg] 按钮启动 actionAddEgg(),因为对话框关闭。无论出于何种原因,addEgg() 没有在我的 Apps 脚本中运行。

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
  </head>
  <body>
  <div>
    <br>
    <br>
    <input type="button" value="+ ????" class="share" onclick="addEgg()" />
    <br>
    <br>
    <input type="button" value="Cancel" class="action" onclick="google.script.host.close()" />
  </div>
    <script>
    function addEgg(){
    google.script.run.addEgg();
    google.script.host.close();
    }
    </script>
  </body>
</html>

这是 Apps 脚本中的 addEgg()。它根本没有运行。 'card' 变量甚至不会记录。

function addEgg(){
  // Get card and sheet data
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('Decks'));
  var card = sheet.getRange('CurrentCard').getValue();
  var sheetname = sheet.getRange('CurrentSheet').getValue();
  var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName(sheetname));
  
  // Get current egg value and +1
  var eggCell = ['F7','M7','T7','AA7','AH7','F16','M16','T16','AA16','AH16','F25','M25','T25','AA25','AH25'];
  var egg = sheet.getRange(eggCell[card]).getValue();
  sheet.getRange(eggCell[card]).setValue(egg + 1);
}

【问题讨论】:

  • 您是否将命名范围定义为CurrentCardCurrentSheet?您确定相应的 getRange 返回的是 Class Range 对象而不是 null?您使用的是什么运行时,新的 (V8) 还是旧的 (Rhino)?
  • 我想是的,是的。如果我在对话框关闭后没有启动它而从编辑器手动运行 addEgg() 函数,该函数将正确执行,并且蛋号会增加。这是 V8。

标签: javascript html google-apps-script google-sheets


【解决方案1】:

您错过了google.script.run 是异步的。这意味着google.script.host.close() 可能在google.script.run.addEgg() 调用被有效执行之前执行。

一种选择是您使用withSuccessHandler 调用google.script.host.close()

您缺少的另一件事是google.script.run 将使用用于登录 Google 服务的默认帐户执行。一种快速的解决方案是与您的默认帐户共享您的电子表格。

相关

【讨论】:

  • 我试着弄乱它,但并没有真正理解它。我确实理解您所说的可能会发生什么,以及为什么会发生,所以我会去阅读更多内容。我从 actionAddEgg() 函数中取出 google.script.host.close() ,看看 addEdd() 函数是否会在没有它的情况下运行。我等了大约 20 秒,然后手动关闭了对话框。没有骰子。 :-(
  • @PatrickLollis 您是否尝试过在禁用所有扩展程序的情况下以隐身模式使用 Chrome?使用旧运行时而不是新运行时怎么样?
  • 我试过了,但是没有用。 &lt;script&gt; function actionAddEgg(){ google.script.run.withSuccessHandler(google.script.host.close).addEgg(); } &lt;/script&gt;
  • 该代码有错误。您是否查看了我在答案中包含的链接?
  • 它在隐身模式下工作! (HTML 中带有脚本的版本)
【解决方案2】:
  • 您不应该对服务器和客户端脚本函数使用相同的函数名 (addEgg)。考虑更改其中一个函数的名称。

  • successHandler 中使用close,因为google.script.run 是异步的。 this answer中提到了这一点

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多