【问题标题】:Import Report from Sheet to Email using Google Apps Script使用 Google Apps 脚本将报表从工作表导入电子邮件
【发布时间】:2021-02-09 16:54:34
【问题描述】:

我在谷歌表中有定期填充数据的报告。我想使用谷歌应用脚​​本将此谷歌工作表报告发送到特定的电子邮件地址。我尝试了不同的方法将该报告捕获到电子邮件正文中,以便接收者可以在电子邮件中接收他的报告,但找不到任何解决方案以电子邮件正文或图像的形式发送它。这是我的报告表的链接:

https://docs.google.com/spreadsheets/d/11g_a4lgcpBftBxd52nVpGGnlmC78bWeUOg7xk5GejWw/edit?usp=sharing

我有以下代码,它从工作表中提取任何图表并将它们导入邮件。但我希望将整个工作表导出到邮件中,而不仅仅是图表。任何帮助将不胜感激。

function email(sheet,toMail,msg){

var charts = sheet.getCharts();

var slides= SlidesApp.create("sample")
var slide = slides.getSlides()[0]

var emailImages={};
var chartBlobs= new Array(charts.length)
var emailBody="";

for (var i=0;i<charts.length;i++)
  {
   var image=slide.insertSheetsChartAsImage(charts[i]);
   chartBlobs[i]= image.getAs("image/png").setName("chartBlob"+i);
   emailBody =emailBody + "<p align='left'><imag src='cid:chart" +i+"'></p>
   emailImages["chart"+i]=chartBlobs[i];
  }

   MailApp.sendEmail({
             to:toMail,
             subject:"ABC",
             htmlBody:emailBody,
             inlineImages:emailImages});


             DriveApp.getFileById(slides.getId()).setTrashed(true);
}

【问题讨论】:

  • 将工作表转换为 pdf 是否适合您?
  • 这是最后一个选项。但理想的情况是将其放入电子邮件正文中。

标签: excel google-sheets report email-attachments


【解决方案1】:

发出Files: get 请求将返回以下导出链接

对于 Google 文档文件:

{
 "exportLinks": {
  "application/rtf": "https://docs.google.com/feeds/download/documents/export/Export?id=1qvfrIFTYZ3lL0pkP6VTEG_YdThY0UeB-Oyjfl48sL5o&exportFormat=rtf",
  "application/vnd.oasis.opendocument.text": "https://docs.google.com/feeds/download/documents/export/Export?id=1qvfrIFTYZ3lL0pkP6VTEG_YdThY0UeB-Oyjfl48sL5o&exportFormat=odt",
  "text/html": "https://docs.google.com/feeds/download/documents/export/Export?id=1qvfrIFTYZ3lL0pkP6VTEG_YdThY0UeB-Oyjfl48sL5o&exportFormat=html",
  "application/pdf": "https://docs.google.com/feeds/download/documents/export/Export?id=1qvfrIFTYZ3lL0pkP6VTEG_YdThY0UeB-Oyjfl48sL5o&exportFormat=pdf",
  "application/epub+zip": "https://docs.google.com/feeds/download/documents/export/Export?id=1qvfrIFTYZ3lL0pkP6VTEG_YdThY0UeB-Oyjfl48sL5o&exportFormat=epub",
  "application/zip": "https://docs.google.com/feeds/download/documents/export/Export?id=1qvfrIFTYZ3lL0pkP6VTEG_YdThY0UeB-Oyjfl48sL5o&exportFormat=zip",
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "https://docs.google.com/feeds/download/documents/export/Export?id=1qvfrIFTYZ3lL0pkP6VTEG_YdThY0UeB-Oyjfl48sL5o&exportFormat=docx",
  "text/plain": "https://docs.google.com/feeds/download/documents/export/Export?id=1qvfrIFTYZ3lL0pkP6VTEG_YdThY0UeB-Oyjfl48sL5o&exportFormat=txt"
 }
}

对于 Google 表格文件:

{
 "exportLinks": {
  "application/x-vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/spreadsheets/export?id=1wGIfU1L9__Tt1JIxfkCYqUiUH1RSIt3P7KsMsuVUYIc&exportFormat=ods",
  "text/tab-separated-values": "https://docs.google.com/spreadsheets/export?id=1wGIfU1L9__Tt1JIxfkCYqUiUH1RSIt3P7KsMsuVUYIc&exportFormat=tsv",
  "application/pdf": "https://docs.google.com/spreadsheets/export?id=1wGIfU1L9__Tt1JIxfkCYqUiUH1RSIt3P7KsMsuVUYIc&exportFormat=pdf",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "https://docs.google.com/spreadsheets/export?id=1wGIfU1L9__Tt1JIxfkCYqUiUH1RSIt3P7KsMsuVUYIc&exportFormat=xlsx",
  "text/csv": "https://docs.google.com/spreadsheets/export?id=1wGIfU1L9__Tt1JIxfkCYqUiUH1RSIt3P7KsMsuVUYIc&exportFormat=csv",
  "application/zip": "https://docs.google.com/spreadsheets/export?id=1wGIfU1L9__Tt1JIxfkCYqUiUH1RSIt3P7KsMsuVUYIc&exportFormat=zip",
  "application/vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/spreadsheets/export?id=1wGIfU1L9__Tt1JIxfkCYqUiUH1RSIt3P7KsMsuVUYIc&exportFormat=ods"
 }
}

换句话说:没有将 Google 表格文件导出为 HTML 的内置功能

这就是您可以从 UI 以 HTML 格式发送文档文件而不是 Google 表格的原因。

结论:

您需要自己构建一个 html 表格,以将表格内容放入您的电子邮件正文。

Here 是您如何做到这一点的示例。

或者 - 考虑将整个工作表内容转换为图像,因为图像可以嵌入到电子邮件正文中。

【讨论】:

  • 感谢您的回答。它确实帮助我寻找可能的解决方案。经过一番挖掘,我找到了this
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
  • 1970-01-01
  • 2020-12-15
  • 2018-09-08
  • 2016-09-19
  • 1970-01-01
相关资源
最近更新 更多