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?
CodePudding user response:
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,
})
}
}