javascript - google.script.run 错误,我该如何解决?
问题描述
谷歌脚本代码
嗨,伙计们,我在使用基本代码时遇到了困难,一旦我添加了 google.scripts.run,它似乎就停止了工作
您的意见将不胜感激
下面的代码是我的谷歌脚本代码“Code.gs”
function AddRecord(name) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainSheet = ss.getSheetByName("Main");
mainSheet.appendRow([name,new Date()]);
}
function startForm()
{
var form = HtmlService.createHtmlOutputFromFile('AddForm');
SpreadsheetApp.getUi().showModalDialog(form,'Add Record');
}
function addMenu(){
var menu = SpreadsheetApp.getUi().createMenu('Custom');
menu.addItem('Add Record Form', 'startForm');
menu.addToUi();
}
function onOpen(e){
addMenu();
}
下面的代码是我的 HTML “AddForm.HTML”
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function AddRow(){
var name = document.getElementById("name").value;
google.script.run.AddRecord(name);
}
</script>
</head>
<body>
Name: <input type="text" id="name" />
<input type="button" value="Add" onclick="google.script.run.AddRecord(name);" />
</body>
</html>
解决方案
您的代码似乎有点错误。
查看该AddForm
文件,您已经创建了一个名为的函数,该函数AddRow
使用google.script.run
并且这是一个很好的继续操作的方法。但是你从来没有真正使用过这个函数本身。
在按钮中,您有:
<input type="button" value="Add" onclick="google.script.run.AddRecord(name);" />
但该变量name
仅在AddRow
.
将其更改onClick
为实际调用AddRow
,您应该会很好。
修改代码
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function AddRow(){
var name = document.getElementById("name").value;
google.script.run.AddRecord(name);
}
</script>
</head>
<body>
Name: <input type="text" id="name" />
<input type="button" value="Add" onclick="AddRow();" />
</body>
</html>
推荐阅读
- codeigniter - 更新用户配置文件时如何解决此错误?
- tensorflow - 如何将 input_shape 转换为 3 dim “输入具有 3 个维度,但得到的数组具有形状 (35, 33297)”
- firebase - 如何在 Flutter 中缓存 Firebase 数据?
- javascript - 如何在弹出模式中仅显示来自外部 html 文件的一个 div id?
- git - git:如何将一个分支建立在多个本地分支上?
- react-native - 如何使用 react-navigation v3 在 react-native 中的 native-base 中正确实现 Drawer
- swift - Swift 4 - 根据另一个数组从数组中删除项目
- java - iText 7 - 重新生成使用 Adobe 创建的条形码字段
- javascript - 如果 id @clicked 在循环中,则居中地图
- xpages - x pages 问题:计算工作日错误