In Google Apps Script, how can I preserve newlines

2019-02-28 10:37发布

I have a TextArea() that I'm collecting user input from.

I'd like to spit this input back out into an email, but it's important to preserve the formatting. If someone puts in returns or paragraphs to separate their statements, I want that to be reflected in the output.

Currently, I have this input assigned to a variable, and then I reference the variable in the MailApp() call. It is not preserving formatting through this process:

var user_input=e.parameter.text_area_input;

MailApp.sendEmail({
  to: "someemail@somedomain.com",
  subject: "This is a subject.",
  htmlBody: "Here is the text that was entered: <BR>" + user_input
})

How can I do that?

1条回答
乱世女痞
2楼-- · 2019-02-28 10:52

You are sending a html email so you need to replace new line characters '\n' with line breaks <BR>.

Here are the options

//Send as text email
MailApp.sendEmail('someemail@somedomain.com',  
                   'This is a subject', 
                   'Here is the text that was entered: \n' + user_input);

//send as html email
MailApp.sendEmail('someemail@somedomain.com',  
                   'This is a subject', 
                   '', {htmlBody: 'Here is the text that was entered: <br>' + user_input.replace(/\n/g, '<br>')});

I have not tested the code. It may have syntax error or typo.

查看更多
登录 后发表回答