[英]How to pass variable from Code.gs to included script snippet
So I understand that to put a variable in my HTML template I need to do所以我知道要在我的 HTML 模板中放置一个变量,我需要做
var html = HtmlService.createTemplateFromFile('Index')
html.data = data
html = html.evaluate();
and then call it in the HTML with然后在 HTML 中调用它
<?= data ?>
But then if I include a script tag snippet with (in the Code.gs file)但是,如果我包含一个脚本标记片段(在 Code.gs 文件中)
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
and then include my script snippet in the HTML with然后在 HTML 中包含我的脚本片段
<?!= include('DataHandler') ?>
How do I call that data
variable in the DataHandler script?如何在 DataHandler 脚本中调用该data
变量?
Change the include
function to evaluate data:更改include
function 以评估数据:
function include(filename, ...otherData) {
var html = HtmlService.createTemplateFromFile('Index')
otherData.forEach(obj => html[obj.key] = obj.value)
return html.evaluate();
}
Then use include
like:然后使用include
:
<?!= include('DataHandler', {key:data, value: [1,2,3]}) ?>
I don't really use templates much but this worked for me:我并没有真正使用模板,但这对我有用:
HTML: HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div id="lnk"><?= data?></div>
<script>
var gdata=<?= data ?>
window.onload=function(){
google.script.run.writeData(gdata);
}
console.log("My Code");
</script>
</body>
</html>
GS: GS:
function launchmydialog() {
let ui=HtmlService.createTemplateFromFile('ah1');
ui.data="Hello Work";
SpreadsheetApp.getUi().showModelessDialog(ui.evaluate(),'Title');
}
function writeData(data) {
SpreadsheetApp.getUi().alert(data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.