在Google Apps脚本中,如何保存用户输入的换行符? [英] In Google Apps Script, how can I preserve newlines from user input?
问题描述
我有一个TextArea(),用于收集用户输入.
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.
当前,我已将此输入分配给一个变量,然后在MailApp()调用中引用该变量.它不通过此过程保留格式:
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
})
我该怎么做?
推荐答案
您正在发送html电子邮件,因此您需要用换行符< BR>替换新的行字符'\ n'.
You are sending a html email so you need to replace new line characters '\n' with line breaks <BR>.
这是选项
//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.
这篇关于在Google Apps脚本中,如何保存用户输入的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!