【发布时间】:2018-10-22 16:10:58
【问题描述】:
我浏览了很多关于我的问题的文章,但我仍然无法解决我的问题。我正在尝试在侧边栏中创建一个表单,其中条目将与其他(隐藏)字符串连接以“生成”一个完整的 URL,然后将该 URL 发布到活动工作表中的列中。我能够显示侧边栏和表单,但在那之后我似乎无法进行任何其他工作。当我点击提交时,侧边栏变为空白,没有任何反应。
这是我的 Code.gs:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Custom Menu')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Form')
.setTitle('Link Generator')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function processForm(form) {
var result = "https://somelink.com/checkout/term?key="+form.partkey+"&metadata=%7B%22morelink%22%3A"+form.cid+"%7D&p_sheet="+form.psheet;
SpreadsheetApp.getActiveSheet().getActiveCell().setValue(result);
Logger.log(form)
}
这是我的 Form.html:
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<script>
function handleFormSubmit(formObject) {
google.script.run.processForm(
{
partkey: formObject.getElementById("partkey").value,
cid: formObject.getElementById("cid").value,
psheet: formObject.getElementById("psheet").value
});
}
</script>
<form id="myForm" onsubmit="handleFormSubmit(this)">
Part Key: <input type="text" name="partkey" id="partkey" required><br/>
CID: <input type="text" name="cid" id="cid" required><br/>
PSheet: <input type="text" name="psheet" id="psheet" required><br/>
<input type="submit" value="Submit" />
</form>
<div id="output"></div>
<p>
<input type="button" value="Close" onclick="google.script.host.close()" />
请注意,我是 JavaScript 和所有编码方面的新手,真的。
【问题讨论】:
-
@I'-'对不起,我不明白。我会将 google.script.run.processForm(formObject) 放在哪里? preventDefault() 是做什么的?对不起,我是菜鸟!
-
@I'-'我搞定了!谢谢!
标签: javascript html forms google-apps-script google-sheets