如何插入.html文件从谷歌应用程序脚本HTMLService类获取模板? [英] How to insert into .html file the template obtained from google app script HTMLService class?
问题描述
我有一个按钮,我想通过HtmlService.createTemplateFromFile('HTML_file')方法将其替换为通过Google应用程序脚本加载的其他HTML对象。
按钮onclick函数是在.html文件中的javascript中写入的:
< div id =add_form_button_wraper>
< input type =buttonid =add_formonclick =addForm()value =Add Form>
< / div>
< script>
函数addForm(){
innerHTML = google.script.run.getForm(some_args);
document.getElementById(add_form_button_wraper)。innerHTML =
innerHTML;
}
< / script>
Code.gs代码如下所示:
getForm(arg1,arg2){
//从html文件创建HTML模板。
var template = HtmlService.createTemplateFromFile(HTML_TEMPLATE);
template.arg1 = arg1;
template.arg2 = arg2;
返回template.evaluate()。getContent();
}
单击该按钮后,div的内容将更改为undefined。我也尝试了同样的效果HTMLService.createTemplateFromFile()。getCode()。
如何将从HTML应用程序脚本获取的原始html代码插入div中? div> 一个简单的Web应用程序示例
以下是我如何执行对话和Web应用程序。我知道这并不一定与你的具体情况有关,但希望一个可行的例子可以给你一些关于如何继续进行的想法。
这是一个简单的例子一个标准的Web应用程序表单您可以将其视为对话框或将其作为Web应用程序运行。它将所有的回复存储在表格中。
Code.gs:
<$ c
$ b SpreadsheetApp.getUi()。createMenu('My Tools')
.addItem('Display Dialog','displayDialog')
。 addToUi();
}
函数doGet()
{
var output = HtmlService.createHtmlOutputFromFile('CyclingSurvey');
返回output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
循环Survey.gs:
函数storeCyclingSurveyResults(rA)
{
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('CyclingSurvey');
var ts = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),MM / dd / yyyy HH:mm:ss);
rA.splice(0,0,ts);
sh.appendRow(rA);
返回true;
function displayDialog()
{
var html = HtmlService.createHtmlOutputFromFile('CyclingSurvey')。setWidth(800).setHeight(500);
SpreadsheetApp.getUi()。showModelessDialog(html,'Cycling Survey');
}
循环Survey.html:
<!DOCTYPE html>
< html>
< head>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\"> ;</script>
< script>
函数recordData()
{
var q1 = $('#q1')。val();
var q2 = $('input [name =prefer]:checked')。val();
var checked = [];
$(input [name ='own']:checked)。each(function(){checked.push($(this).val());});
var q3 = checked.join(',');
var q4 = $('#q4')。val();
var rA = [];
rA.push(q1);
rA.push(q2);
rA.push(q3);
rA.push(q4);
google.script.run
.withSuccessHandler(setResponse)
.storeCyclingSurveyResults(rA);
}
函数setResponse()
{
$('#reply')。css('display','inline');
$('#collect')。css('display','none');
}
console.log('script here');
< / script>
< style>
#reply {display:none;}
#collect {display:block;}
< / style>
< / head>
< body>
< div id =reply>感谢您花时间参与我们的调查。您的结果已被记录。< / div>
< div id =collect>
< div style =font-weight:bold>你喜欢骑自行车什么?< / div>
< div style =font-weight:bold>你喜欢哪种类型的自行车?< / div>
< input type =radioname =prefervalue =mountain bike>山地自行车< br />
< input type =radioname =prefervalue =road bikechecked> Road Bike< br />
< input type =radioname =prefervalue =健身自行车>健身自行车< br />
< div style =font-weight:bold>您目前拥有哪些类型的自行车?< / div>
< input type =checkboxname =ownvalue =Mountain Bike>山地自行车< br />
< input type =checkboxname =ownvalue =Road Bikechecked> Road Bike< br />
< input type =checkboxname =ownvalue =All Terrain Bike> All Terain Bike< br />
< input type =checkboxname =ownvalue =健身自行车>健身自行车< br />
< div style =font-weight:bold>你喜欢与其他人一起骑行吗?< / div>
单独< input id =q4type =rangename =ratingmax =10min =0step =1defaultValue =5/>其他< br /> ;
< input type =buttonid =btn1value =SubmitonClick =recordData(); />
< / div>
< / body>
< / html>
I have a button and I would like to replace it with other HTML object loaded by google app script via method HtmlService.createTemplateFromFile('HTML_file').
The button onclick function is written in javascript in .html file:
<div id="add_form_button_wraper">
<input type="button" id="add_form" onclick="addForm()" value="Add Form">
</div>
<script>
function addForm() {
innerHTML = google.script.run.getForm(some_args);
document.getElementById("add_form_button_wraper").innerHTML =
innerHTML;
}
</script>
The Code.gs code looks following:
getForm(arg1, arg2) {
// Creating HTML template from html file.
var template = HtmlService.createTemplateFromFile(HTML_TEMPLATE);
template.arg1 = arg1;
template.arg2 = arg2;
return template.evaluate().getContent();
}
After clicking the button the content of the div is changed to undefined. I tried also HTMLService.createTemplateFromFile().getCode() with the same effect.
How to insert into div the raw html code obtained from app script HTMLService?
A Simple Web App Example
Here's how I do my dialogs and web apps. I know it doesn't necessarily pertain to your particular situation but hopefully a working example can give you some ideas as to how you can proceed.
This is a simple example of a standard web app form. You can view it as a dialog or run it as a webapp. It stores all of the response on a sheet.
Code.gs:
function onOpen()
{
SpreadsheetApp.getUi().createMenu('My Tools')
.addItem('Display Dialog', 'displayDialog')
.addToUi();
}
function doGet()
{
var output=HtmlService.createHtmlOutputFromFile('CyclingSurvey');
return output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
Cycling Survey.gs:
function storeCyclingSurveyResults(rA)
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('CyclingSurvey');
var ts=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:ss");
rA.splice(0,0,ts);
sh.appendRow(rA);
return true;
}
function displayDialog()
{
var html=HtmlService.createHtmlOutputFromFile('CyclingSurvey').setWidth(800).setHeight(500);
SpreadsheetApp.getUi().showModelessDialog(html, 'Cycling Survey');
}
Cycling Survey.html:
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function recordData()
{
var q1=$('#q1').val();
var q2=$('input[name="prefer"]:checked').val();
var checked = [];
$("input[name='own']:checked").each(function (){checked.push($(this).val());});
var q3=checked.join(', ');
var q4=$('#q4').val();
var rA=[];
rA.push(q1);
rA.push(q2);
rA.push(q3);
rA.push(q4);
google.script.run
.withSuccessHandler(setResponse)
.storeCyclingSurveyResults(rA);
}
function setResponse()
{
$('#reply').css('display','inline');
$('#collect').css('display','none');
}
console.log('script here');
</script>
<style>
#reply{display:none;}
#collect{display:block;}
</style>
</head>
<body>
<div id="reply" >Thanks for taking the time to take our survey. Your results have been recorded.</div>
<div id="collect">
<div style="font-weight:bold">What do you like about cycling?</div>
<textarea id="q1" rows="4" cols="50" placeholder="Enter Text Here..."></textarea>
<div style="font-weight:bold">Which type of bike do you prefer?</div>
<input type="radio" name="prefer" value="mountain bike">Mountain Bike<br />
<input type="radio" name="prefer" value="road bike" checked>Road Bike<br />
<input type="radio" name="prefer" value="fitness bike">Fitness Bike<br />
<div style="font-weight:bold">What types of bikes do you currently own?</div>
<input type="checkbox" name="own" value="Mountain Bike">Mountain Bike<br />
<input type="checkbox" name="own" value="Road Bike" checked>Road Bike<br />
<input type="checkbox" name="own" value="All Terrain Bike">All Terain Bike<br />
<input type="checkbox" name="own" value="Fitness Bike">Fitness Bike<br />
<div style="font-weight:bold">Do you prefer riding with others or alone?</div>
Alone<input id="q4" type="range" name="rating" max="10" min="0" step="1" defaultValue="5"/>Others<br />
<input type="button" id="btn1" value="Submit" onClick="recordData();" />
</div>
</body>
</html>
这篇关于如何插入.html文件从谷歌应用程序脚本HTMLService类获取模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!