Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script - Pulling data into a template with parameters

I'm developing a Google Apps Script and I want to pull the URL parameters into a HTML file. For example, if the user goes to my_domain.com/?hello=test the HTML output should be "Hello test". Here is my code.

index.html

<html>
    <body><p>Hello <?= data ?></p></body>
</html>

myCode.gs

function doGet(e) {
    var t = HtmlService.createHtmlOutputFromFile('index');
    t.data = e.parameter.hello;
    return t.evaluate();
}

This results in an error message: "Vous ne pouvez pas ajouter ni modifier de propriétés pour cet objet." (You can not add or modify properties for this object.)

I don't know why. I follow this doc https://developers.google.com/apps-script/html_service They say:

The third way for templates to access data is to add variables to a template directly in a script file, as shown in the example below.

function doGet() {
    var t = HtmlService.createTemplateFromFile('myTemplate');
    t.data = SpreadsheetApp.openById('SPREADSHEET_KEY_GOES_HERE').getRangeByName('dataRange').getValues();
    return t.evaluate();
}
like image 535
tsil Avatar asked Dec 27 '22 20:12

tsil


1 Answers

Your code:

var t = HtmlService.create HtmlOutput FromFile('index');

The example you cited:

var t = HtmlService.create Template FromFile('index');

An HtmlOutput can't have variables added to it like you are attempting. A template can - and the result of evaluating it is an HtmlOutput.

like image 61
Corey G Avatar answered May 21 '23 01:05

Corey G