Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Share variable from .gs into .html in Google Apps Script

I'm trying to get respondents to a Google Form survey fill another survey, but some of their responses will be pre-filled based on the first survey. Basically, want all of their responses attached to the unique person, without making them answer same questions again. And it must be two surveys, not a continuation. In this example, I'm only pre-filling their email address, but it may be more fields.

So in Google Apps Script, I've got a Code.gs and and Email1.html scripts as follows:

function confirmationEmail1(e) {
  
  var htmlBody= HtmlService.createHtmlOutputFromFile('Email1').getContent();
  var recipient=e.response.getRespondentEmail();
  if(recipient!==undefined){
    MailApp.sendEmail({
      to:recipient,
      subject: 'Thank you. Please answer another one',
      htmlBody: htmlBody,
    })
  }
}

And the following email body:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p>Thank you for filling the survey</p> 
    <p>Now fill another one. Some answers will be pre-filled based on your past responses<p> 
    <p>To continue click this <a href="https://docs.google.com/forms/d/e/1FAIpQLSfqYWf-V0Jrwh8ld2AjuHtskuEHr1CfQB3_wrLS7grzZ_EajQ/viewform?usp=pp_url&entry.786833434=<?= recipient ?>">Link</a></p>
  </body>
</html>

What am I doing wrong in this place that it does not retrieve the recipient variable from Code.gs but simply pre-fills the text "" in my second survey?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since the htmlBody is a string you can replace any text fragments with another text fragments, with string.replace() method.

Say you can make {RECEPIENT} in the html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p>Thank you for filling the survey</p> 
    <p>Now fill another one. Some answers will be pre-filled based on your past responses<p> 
    <p>To continue click this <a href="https://docs.google.com/forms/d/e/1FAIpQLSfqYWf-V0Jrwh8ld2AjuHtskuEHr1CfQB3_wrLS7grzZ_EajQ/viewform?usp=pp_url&entry.786833434=<?={RECEPIENT}?>">Link</a></p>
  </body>
</html>

And then replace the text {RECEPIENT} it with recepient value in the script:

function confirmationEmail1(e) {
  
  var htmlBody= HtmlService.createHtmlOutputFromFile('Email1').getContent();
  var recipient=e.response.getRespondentEmail();
  if(recipient!==undefined){

    htmlBody = htmlBody.replace('{RECEPIENT}', recipient); // <--- here

    MailApp.sendEmail({
      to:recipient,
      subject: 'Thank you. Please answer another one',
      htmlBody: htmlBody,
    })
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!