Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

When using Google forms, how do I get the person's name so I can use it in the confirmation email the user receives after submitting the form

I have a google forms with this field: Firstname

I have managed to set up the confirmation email so that the user receives an email upon submitting the form. However, I want to personalise the email and address it to the person by using their name.

Here is my appscript:

function sendEmail(e) {
  //response
  //getRespondentEmail()
  var html=HtmlService.createTemplateFromFile("email.html");
  var htmlText = html.evaluate().getContent();
  var emailTo = e.response.getRespondentEmail();
  var subject = "Welcome to MyAPP";
  var textBody = "Welcome to MyAPP";
  var options = { htmlBody: htmlText };

  if(emailTo !== undefined){

  GmailApp.sendEmail(emailTo,subject,textBody,options)}
}

here is the email.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    
    <p>Welcome <NAME SHOULD GO HERE> to my APP<p>
    
  </body>
</html>

Can someone please tell me how I can add the value from the Firstname field

I tried adding this to the app script but it says error name is not defined:

 var name = response.getValuesFromForm().Firstname();
question from:https://stackoverflow.com/questions/66055960/when-using-google-forms-how-do-i-get-the-persons-name-so-i-can-use-it-in-the-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Look at the Form submit event object

https://developers.google.com/apps-script/guides/triggers/events

Namely, namedValues, which returns an object like this:

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

Note that all the values get returned in an array, even if it's just one value. To get the information from this object, you can do the following:

let values = e.namedValues
let name = values.Firstname[0] // assigning first item (only item) of Firstname array to name
let email = values.email[0] // assigning first item (only item) of email array to email

These properties Firstname and email may be different depending on how you named them in your form.

It seems you were trying to call the properties as if they were functions - values.Firstname() - which will not work, unless you can find that function defined in the documentation. Or maybe these are functions that are defined elsewhere in your script.

References


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...