Google apps script 在GoogleApps脚本中,如何从用户输入中保留换行符?

Google apps script 在GoogleApps脚本中,如何从用户输入中保留换行符?,google-apps-script,return,newline,Google Apps Script,Return,Newline,我有一个TextArea(),从中收集用户输入 我想把这些输入内容重新写回电子邮件,但保留格式很重要。如果有人输入返回或段落来分隔他们的语句,我希望这能反映在输出中 目前,我将此输入分配给一个变量,然后在MailApp()调用中引用该变量。它不会通过此过程保留格式: var user_input=e.parameter.text_area_input; MailApp.sendEmail({ to: "someemail@somedomain.com", subject: "This

我有一个TextArea(),从中收集用户输入

我想把这些输入内容重新写回电子邮件,但保留格式很重要。如果有人输入返回或段落来分隔他们的语句,我希望这能反映在输出中

目前,我将此输入分配给一个变量,然后在MailApp()调用中引用该变量。它不会通过此过程保留格式:

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
})
var user\u input=e.parameter.text\u area\u input;
MailApp.sendmail({
至:someemail@somedomain.com",
主题:“这是一个主题。”,
htmlBody:“这是输入的文本:
”+用户输入 })

怎么做?

您正在发送一封html电子邮件,因此需要用换行符替换新行字符“\n”

以下是选项

//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>')});
//作为文本电子邮件发送
MailApp.sendmail('someemail@somedomain.com',  
“这是一个主题”,
'这是输入的文本:\n'+用户输入);
//以html电子邮件的形式发送
MailApp.sendmail('someemail@somedomain.com',  
“这是一个主题”,
'',{htmlBody:'这是输入的文本:
'+user\u input.replace(/\n/g,
');

我还没有测试代码。它可能有语法错误或键入错误。

您可以使用
\n
来执行此操作,在
JavaScript
中,
\n
意味着
n

运行良好。我知道你在.replace调用中使用的语法是来自linux中的sed或awk的-准确吗?@user2308300是的,这些都是正则表达式,几乎适用于每种语言,只需稍加修改或不做任何修改。显然,HTML去除了很多格式,包括字符串中有多个空格的部分,它将它们减少到1。这里的问题是,我使用htmlBody而不是普通的“body”来发送文本。