Reputation: 45
So I understand that to put a variable in my HTML template I need to do
var html = HtmlService.createTemplateFromFile('Index')
html.data = data
html = html.evaluate();
and then call it in the HTML with
<?= data ?>
But then if I include a script tag snippet with (in the Code.gs file)
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
and then include my script snippet in the HTML with
<?!= include('DataHandler') ?>
How do I call that data
variable in the DataHandler script?
Upvotes: 1
Views: 232
Reputation: 50890
Change the include
function to evaluate data:
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('DataHandler', {key:data, value: [1,2,3]}) ?>
Upvotes: 3
Reputation: 64140
I don't really use templates much but this worked for me:
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:
function launchmydialog() {
let ui=HtmlService.createTemplateFromFile('ah1');
ui.data="Hello Work";
SpreadsheetApp.getUi().showModelessDialog(ui.evaluate(),'Title');
}
function writeData(data) {
SpreadsheetApp.getUi().alert(data);
}
Upvotes: 0