Google 脚本和电子邮件

Google Scripting and Email

我有一个带有 table 的 Google 表格,其中预填充了数据,

我想使用脚本

在电子邮件中发送 summary/table 的 'picture'

有没有办法使用脚本预览电子邮件正文中的 table?

感谢您对此的任何帮助

要完成此任务,您可以使用 SheetConverter

  • 按照这些说明将其包含在内 library 直接到您的项目。
  • 但是,如果您遇到任何问题,请先回答此问题 post 将为您提供您正在寻找的解决方案。

导入库后,使用此代码片段在电子邮件正文中发送 table 的“屏幕截图”。您可以在此处指定 table 的范围:

const range = s.getRange('B2:C10');

请同时调整Sheet的名称,在下面的例子中我使用Sheet1.

Code.gs

function sendEmail() {
  
  const s = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const conv = SheetConverter.init(ss.getSpreadsheetTimeZone(),
                    ss.getSpreadsheetLocale());
  const range = s.getRange('B2:C10');
  
  const htmlTable = conv.convertRange2html(range);
  
  const to = 'example@gmail.com';
  
  const body = 
     "Dear Sir or Madam, <br/><br/>" 
     + htmlTable
     + "<br/><br/>End of report." ;
     
  const subject ="This is a test";
     
     MailApp.sendEmail(to, subject, body, {htmlBody: body});
     
}