应用脚本 - 发送电子邮件 - 以电子邮件方式设置内容粗体 - 以粗体格式化Array.toString的结果

时间:2017-06-06 18:20:19

标签: google-apps-script gmail google-form text-formatting bold

我的代码每次提交Google表单时都会发送电子邮件通知。它有效,但我希望收集的表单数据在电子邮件中以粗体显示。

for(var i in headers) 
    message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";  


function sendFormByEmail(e) 
{    
  var s = SpreadsheetApp.getActiveSheet();
  var headers = s.getRange(1, 1, 1, s.getLastColumn()).getValues()[0];    
  var message = "";
  var data = s.getRange(1, 1, 1, s.getLastColumn()).getValues()[0];
  var subject = "";

  //Get active Spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  //Get Sheet called 'Form responses 1'
  var fr1 = ss.getSheetByName("Form responses 1");

  //Get all emails from 'Emails' tab
  var emails = ss.getSheetByName("Emails");
  var numRows = emails.getLastRow();
  var emailTo = emails.getRange(2, 2, numRows, 1).getValues();

  // The variable e holds all the form values in an array.
  // Loop through the array and append values to the body.

  for(var i in headers) 
    message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";     

  // Insert variables from the spreadsheet into the subject.
  // In this case, I wanted the part number as part of the
  // email subject. These are the 3rd and 16th columns in my form.
  // This creates an email subject like "Parts Inquiry Submission: 729302"
  subject += e.namedValues[headers[1]].toString();

  // Send the email
  MailApp.sendEmail(emailTo, subject, message); 

  // Based off of a script originally posted by Amit Agarwal - www.labnol.org
  // Credit to Henrique Abreu for fixing the sort order
}

2 个答案:

答案 0 :(得分:1)

首先,您必须在htmlBody的第4个参数中使用高级参数sendEmail(),并且需要使body参数和空字符串。

  

sendEmail(收件人,主题,正文,选项)

代码:

message = "<strong>" + message + "</strong>";//Wrap message in bold tags

var options = {
  "htmlBody":message
}

// Send the email
MailApp.sendEmail(emailTo, subject, "",options);//Make email body empty string

答案 1 :(得分:0)

谢谢大家的帮助。我最后改了一下。这是我最终使用的。

function sendFormByEmail(e) 
{    

  var message = "";
  var subject = ""; 
  var emailTo = "";
  
  //Gets the active Spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  
  //Get Sheet called 'Form responses 1'
  var fr1 = ss.getSheetByName("Form responses 1");
  var headers = fr1.getRange(1,1,1,fr1.getLastColumn()).getValues()[0];  
  
  //Get all emails from 'Emails' tab
  var emails = ss.getSheetByName("Emails");
  var emailTo = emails.getRange(2, 2, emails.getLastRow()-1, 1).getValues().toString();

  // For each column in the spreadsheet, add the column name and it's new entry as a line in the email
  message = "<html>";
  for(var i in headers)
    message += headers[i] + ': <b>'+ e.namedValues[headers[i]].toString() + "</b><br><br>"; 
  message += "</html>";
  
  // Set the subject as the Item Number
  subject = e.namedValues[headers[1]].toString();

  // Send the email
  MailApp.sendEmail({
    to: emailTo,
    subject: subject,
    htmlBody: message
  });
  
}