javascript - html侧边栏数据无法访问
问题描述
我有一个谷歌表,我打开了脚本编辑器并打开了两个文件,一个脚本文件和一个 html 文件。这是我的脚本文件中的内容:
//to load html sidebar
function loadHome() {
const htmlSidebar = HtmlService.createTemplateFromFile("home");
const htmlOutput = htmlSidebar.evaluate();
const ui = SpreadsheetApp.getUi();
ui.showSidebar(htmlOutput);
}
//to create a custom menu
function createMenu(){
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu("AC Paz");
menu.addItem("Side Registration", "loadHome");
menu.addItem("Order Framing", "loadFraming");
menu.addToUi();
}
//to make sure custom menu is loaded on open
function onOpen(){
createMenu();
}
//to access data passed on html input fields
function userClicked(userInfo){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName("Site Information");
ws.appendRow([userInfo.firstName, userInfo.lastName, new Date()])
}
这是我的 html 文件中的内容:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div >
<input id="fn" type="text" >
<label for="fn">First Name</label>
</div>
<div >
<input id="ln" type="text">
<label for="ln">Last Name</label>
</div>
<button id="btn">Add</button>
<script>
document.getElementById("btn").addEventListener("click",doStuff);
function doStuff(){
var userInfo = {}
userInfo.firstName = document.getElementById("fn").value;
userInfo.lastName = document.getElementById("ln").value;
google.script.run.userClicked(userInfo);
document.getElementById("fn").value = "";
document.getElementById("ln").value = "";
}
</script>
这是我的问题:当我在输入字段中键入内容并单击按钮时,没有任何内容传递到电子表格中,但是输入字段中的条目被清除。
解决方案
尝试这个:
HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div >
<input id="fn" type="text" >
<label for="fn">First Name</label>
</div>
<div >
<input id="ln" type="text">
<label for="ln">Last Name</label>
</div>
<button id="btn">Add</button>
<script>
window.onload=function(){
document.getElementById("btn").addEventListener("click",doStuff);
function doStuff(){
var userInfo = {}
userInfo.firstName = document.getElementById("fn").value;
userInfo.lastName = document.getElementById("ln").value;
google.script.run
.withSuccessHandler(function(){
document.getElementById("fn").value = "";
document.getElementById("ln").value = "";
})
.userClicked(userInfo);
}
}
</script>
</body>
</html>
GS:
function userClicked(userInfo){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName("Sheet1");
ws.appendRow([userInfo.firstName, userInfo.lastName, new Date()])
}
function showMySideBar() {
SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('ah1'))
}
推荐阅读
- kivy - Kivy:在没有 kv 的情况下跟随鼠标移动移动图像
- python - 意外的 Python TypeError:使用标量时
- c - 递归函数计算数字字符串的后继
- vue.js - 向 nuxt.config.js 添加函数
- elasticsearch - 带有大写标记的 Elasticsearch word_delimiter 过滤器不匹配
- discord - discord.js 尝试使用 2 个数组创建随机结果命令
- php - 如何使用 INNER JOIN 改进我的 php sub_array 代码以在 DataTables 上显示数据?
- sql - 如何在sql中创建一个可以对当前日期和我的表中的值进行操作的函数
- webgl - Webgl2 maximum size of 3D texture?
- json - 邮递员如何在邮递员中解析具有动态键的嵌套 json 对象?