【发布时间】: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);
}
【问题讨论】:
-
您是否将命名范围定义为
CurrentCard和CurrentSheet?您确定相应的 getRange 返回的是 Class Range 对象而不是null?您使用的是什么运行时,新的 (V8) 还是旧的 (Rhino)? -
我想是的,是的。如果我在对话框关闭后没有启动它而从编辑器手动运行 addEgg() 函数,该函数将正确执行,并且蛋号会增加。这是 V8。
标签: javascript html google-apps-script google-sheets