In Google Apps Script, how can I preserve newlines from user input?

£可爱£侵袭症+ 提交于 2020-01-11 11:39:10

问题


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:


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.



来源:https://stackoverflow.com/questions/16208927/in-google-apps-script-how-can-i-preserve-newlines-from-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!