在Google Apps脚本中,如何保留用户输入的换行符?

Loc*_*ane 1 newline return google-apps-script

我有一个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
})
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Waq*_*mad 6

您正在发送html电子邮件,因此您需要使用换行符替换换行符\n'<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>')});
Run Code Online (Sandbox Code Playgroud)

我没有测试过代码.它可能有语法错误或拼写错误.