首页 > 解决方案 > 谷歌脚本的sendEmail(收件人,主题,正文)方法的html正文部分设计

问题描述

如何设计 Google App 脚本的 sendEmail(recipient, subject, body) 正文部分以表格格式发送电子邮件?下面是我的代码,我想用 Html 设计正文部分。请帮忙。

function sendEmails() {

  var app = SpreadsheetApp;
  var targetSheet = app.getActiveSpreadsheet().getSheetByName("Main_Template");

  var i,j; var k=4;
     for(i=2; i<=3; i++){ 
       var infoData = [];
       for(j=2; j<=5; j++){

         var cellValues = targetSheet.getRange(i, j).getValue();
         infoData.push(cellValues);
       }

       var emailSheet = app.getActiveSpreadsheet().getSheetByName("Emails");
       var currentEmail = app.getActiveSheet().activate().getRange(i, 1).getValue(); 
       var subject = app.getActiveSheet().activate().getRange(i, 2).getValue();

       //var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
       //Logger.log("Remaining email quota: " + emailQuotaRemaining);

       MailApp.sendEmail(currentEmail, subject,
                           "\n\n(FY 2019-20) % of NRM Expenditure:  "+ infoData[0]
                         + "\n\n(FY 2019-20) % of Timely MGNREGA wage payment:  " + infoData[1]
                         + "\n\n(FY 2017-18 & Earlier) % of Work Completion:  " + infoData[2]
                         + "\n\n(FY 2018-19) No. of rejected transactions pending regeneration:   " + infoData[3]); 
     }
  }

标签: javascriptgoogle-apps-script

解决方案


您需要使用sendEmail(recipient, subject, body, options)此处的文档)在正文中生成 HTML 表格。

在您的代码中,替换它

   MailApp.sendEmail(currentEmail, subject,
                       "\n\n(FY 2019-20) % of NRM Expenditure:  "+ infoData[0]
                     + "\n\n(FY 2019-20) % of Timely MGNREGA wage payment:  " + infoData[1]
                     + "\n\n(FY 2017-18 & Earlier) % of Work Completion:  " + infoData[2]
                     + "\n\n(FY 2018-19) No. of rejected transactions pending regeneration:   " + infoData[3]); 
 }

有了这个

var body = "<table border = '1'>" +
  "<tr> <td> (FY 2019-20) % of NRM Expenditure:  </td><td>" + infoData[0] + "</td></tr>" +
    "<tr> <td> (FY 2019-20) % of Timely MGNREGA wage payment:  </td><td>" + infoData[1] + "</td></tr>" +
      "<tr> <td> (FY 2017-18 & Earlier) % of Work Completion:  </td><td>" + infoData[2] + "</td></tr>" +
        "<tr> <td> (FY 2018-19) No. of rejected transactions pending regeneration:   </td><td>" + infoData[3] + "</td></tr>" +
          "</table>";

var options = {
  htmlBody : body
}

MailApp.sendEmail(currentEmail, subject, "", options);

要格式化您的表格,请参考此处。玩得开心。


推荐阅读