使用Google Script将文本更改为粗体或添加图像

时间:2017-06-06 15:49:49

标签: javascript text google-apps-script macros

这是我到目前为止的代码,效果很好!我只是想知道如何将文本更改为粗体并在可能的情况下添加图像。对于它所说的增长,发光,适应。我希望以粗体为例。

       function sendEmails() {
  var allData,counter,emailAddress,fourRowsOfData,i,lastRow,
      message,messageStart,messageEnd,numRows,
      row,sheet,startRow,studentName,subject;

  //USER INPUT
  startRow = 2;  // First row of data to process
  numRows = 4;   // Number of rows to process
  subject = "Camp Pursuit: Report Card ";
  messageStart = "Dear Parents,"+'\n' +'\n' +
  "Every week at Camp Pursuit, we ask the teachers to make observations about how the kiddos did." +'\n' +
  "We know that one week isn't enough to truly know a child, so we call this our " +"Glows, Grows, & Apropos." +'\n' +
    "Meaning..." + '\n' +'\n' +
    "Glows: Positive things the teacher noticed" +'\n' +
    "Grows: Areas for continued growth" +'\n' +
    "Apropos: Ways to continue your son/daughter's enrichment" +'\n' +
    "We hope you appreciate seeing what the teachers saw last week, and perhaps you can use some of"+'\n' +
    "the recommendations for further enrichment this summer.  Feel free to let us know your thoughts on the camp through our anonymous online survey."+'\n' +
     "Your feedback helps us improve the experience for all kiddos! "+'\n' + 
"Survey Link: https://docs.google.com/forms/d/1g4LvA9a8sdKECr1yvp5uOoPnvKACFm3HvbTBfvQGRyo/viewform?usp=send_form" +'\n' +
"We look forward to seeing your child at our programs throughout the year!" +'\n' +
"Sincerely, "+'\n' +
"The Camp Pursuit Team"
+'\n';
  //END USER INPUT

  sheet = SpreadsheetApp.getActiveSheet();
  lastRow = sheet.getLastRow();

  allData = sheet.getRange(startRow, 1, lastRow-startRow, 10);// Get All data first

  counter = 0;

  while (counter < lastRow-startRow) {
    Logger.log('counter: ' + counter)

    fourRowsOfData = sheet.getRange(startRow + counter, 1, numRows, 6).getValues();
    emailAddress = fourRowsOfData[0][0];  // First column of first row
    Logger.log('emailAddress: ' + emailAddress)

    studentName = fourRowsOfData[0][1];

    messageEnd = "";//Reset on every outer loop

    for (i = 0; i < numRows; i++) {//loop numRows times - once for each row
        row = fourRowsOfData[i];

        messageEnd = messageEnd + '\n' +
            "Class: " + row[2] + '\n' +'\n' +
              "Glows: " + row[3]+ '\n' +
              "Grows: " + row[4] +'\n' +
                "Apropos: " + row[5] +'\n';  
    }

    message = messageStart + "\n" + studentName + "\n" + messageEnd;

    MailApp.sendEmail(emailAddress, subject, message);//Send the email outside of inner loop    
    counter+=numRows;//Increment counter by number of rows to process
  }
}

2 个答案:

答案 0 :(得分:3)

根据官方MailApp文档,要发送HTML正文,您需要在htmlBody方法中使用MailApp.sendEmail()参数。看看下面的例子(从官方文档页面复制)

要使文字加粗,请使用<b></b>标记

例如:“此文本将以粗体显示

要在电子邮件中显示图像,请使用<img src="hostname.com/image.png">

MailApp.sendEmail({
     to: "recipient@example.com",
     subject: "Logos",
     htmlBody: "inline Google Logo<img src='URL_PATH'> images! <br>" +
               "inline YouTube Logo <img src='URL_PATH'><br>" +
               "<b>Using bold tag</b> ",
     inlineImages:
       {
         googleLogo: googleLogoBlob,
         youtubeLogo: youtubeLogoBlob
       }
   });

希望这有帮助。

答案 1 :(得分:1)

在消息字符串中使用标准html标记,然后在sendEmail()方法中将空字符串“”或“null”作为body参数传递。最后,将您的消息字符串分配给'options'参数的'htmlBody'属性。使用'br'标记表示换行符。实施例

function sendEmail() {

var body = "<b>Hello </b> world. <i><Hello /i> world. <br /> New line";

MailApp.sendEmail("anton.dementyev@gmail.com", "test", "", {htmlBody: body});

}

请参阅https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)

中的“高级参数”部分