Google 表格中的 HTML 对话框 google.script.run. 无法运行 Apps Script 功能



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

我正在尝试使用带有按钮的 HTML 对话框来触发一些脚本。我已经完成了我在文档和八木山托德博客上可以找到的所有示例。除了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 对话框显示正确。单击 [+ 蛋] 按钮启动actionAddEgg(),因为对话框关闭。无论出于何种原因,addEgg()都没有在我的应用程序脚本中运行。

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

下面是应用脚本中的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);
}

您缺少异步google.script.run。这意味着google.script.host.close()可能会在调用有效执行之前执行google.script.run.addEgg()

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

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

相关

  • HTML 服务提交表单未调用 google.script.run 函数
  • 您不应该对服务器和客户端脚本函数使用相同的函数名称(addEgg(。请考虑更改函数的名称之一。

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

相关内容

最新更新