0

Is it possible to add namedValues in a scriptlet when trying to use HTML template to send mails in App Script? If yes, how please, if no, how can I refer to each question on a Google form using scriptlets so that on form submission, the HTML template will evaluate() each record submitted? Below is the code

function sendEmail(e) {
var named_values = e.namedValues;
var name = named_values["Name"]
var timestamp = named_values["Timestamp"]
var subject = "Outlet "+ timestamp +""

const outlet = HtmlService.createTemplateFromFile('outlet');
outlet.named_values = named_values;
const message =  outlet.evaluate().getContent();

MailApp.sendEmail({
to: '[email protected]',
subject: subject,
htmlBody: message
});
}

...and below is the template code

<!DOCTYPE html>
<html>
<body>
<ul><li><b>Company:</b> <?= name ?></li>
<li><b>Loaction:</b> <?= timestamp ?></li></ul>
</body>
</html> 

I need the script to pick 'Name' and 'Timestamp' from the data submitted last from the Google form and send it to mail using the HTML template. Thanks.

3
  • Please provide an example of what you wish to do Commented Jan 16, 2023 at 23:37
  • What do you mean by "namedValues"? Commented Jan 17, 2023 at 0:05
  • I've added the App Script code and the HTML template code. Commented Jan 17, 2023 at 8:07

1 Answer 1

0

e.namedValues is an object, meaning it has properties, these properties are the form titles and their values are the answers people submits in the form.

The representation of the namedValues object on the google doc is the following:

{
 'First Name': ['Jane'],
 'Timestamp': ['6/7/2015 20:54:13'],
 'Last Name': ['Doe']
}

On your code you need to access the values of e.namedValues + the specific title field you want to get. For example, in order to get the timeStamp, you do it the following way:

let named_values = e.namedValues;
let timeStamp = named_values.Timestamp;

In order to make you script work you should apply this concept to your code. A way of doing this is the following:

function sendEmail(e) {

 let named_values = e.namedValues;
 let name = named_values.Name;
 let timeStamp = named_values.Timestamp;
 let subject = "Outlet "+timeStamp;

 let outlet = HtmlService.createTemplateFromFile('outlet');
 outlet.timeStamp = timeStamp;
 outlet.name = name;
 let message =  outlet.evaluate().getContent();

 MailApp.sendEmail({
  to: '[email protected]',
  subject: subject,
  htmlBody: message
 });
}

And the HTML should look like this:

<!DOCTYPE html>
<html>
 <body>
  <ul><li><b> timeStamp: </b> <?= timeStamp ?></li>
  <li><b> Name: </b> <?= name ?></li></ul>
 </body>
</html> 
Sign up to request clarification or add additional context in comments.

3 Comments

it worked and I appreciate. But added an HTML comment to the code, but the comment was not included in mail received. What can I do about this please?
Hi, is the comment another field from the form? if yes, you can assign it's value to a variable the same way the code assigned values of Name and TimeStamp, then you include that also in the outlet and in the HTML file you create a new <li> section with the comment.
It's something like <!-- comment - - >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.