user5245372
user5245372

Reputation: 1

Simple q: how to get data from Google form in email

I'm using a form to make a reservation. My goal is to have an automated email sent to the address entered in the form along with a specific data from the form (reservation date and time).

I'm working off this add-on:

https://developers.google.com/apps-script/quickstart/forms-add-on

The email is working, it will send the .html to the email address in the form after the form is submitted.

Now I'd like to get the 2 data fields from the form into the email (date and time).

Here are the snippets of code that I think are applicable, first the code.gs:

function sendRespondentNotification(response) {
 var form = FormApp.getActiveForm();
 var settings = PropertiesService.getDocumentProperties();
 var emailId = settings.getProperty('respondentEmailItemId');
 var emailItem = form.getItemById(parseInt(emailId));
 var respondentEmail = response.getResponseForItem(emailItem)
   .getResponse();
 if (respondentEmail) {
 var template =
    HtmlService.createTemplateFromFile('RespondentNotification');
 template.paragraphs = settings.getProperty('responseText').split('\n');
 template.notice = NOTICE;
 var message = template.evaluate();
 MailApp.sendEmail(respondentEmail,
    settings.getProperty('responseSubject'),
    message.getContent(), {
      name: form.getTitle(),
        htmlBody: message.getContent()
    });
 }
}

Here is the .html for the email template:

  <html><link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
  <body>    
  <h1>Thank you for booking</h1>
  <p>Below are the details of your reservation:</p>
  <p>Date:</p>
  <p>Time:</p>
  </body>
  </html>

I've been trying something like var date= form.getResponce(date) in the script and <?= date ?> in the .html with no luck.

Can anyone tell me the proper way to do this?

Thanks.

Upvotes: 0

Views: 120

Answers (1)

IDM
IDM

Reputation: 127

To pass the date into the template you have to set it on the variable template as is done for paragraphs and NOTICE

So after template is initialised and before it's evaluated you would put:

template.date = date;

or

template.date = form.getResponse(date);

Then, as you say, in the template (RespondentNotification.html) include the value as

<?= date ?>

ASIDE: You're not using paragraphs or NOTICE in your template so you may as well not set them on the template variable.

Upvotes: 1

Related Questions