Google Script 使用 Blob 作为“正文”发送电子邮件

时间:2021-03-11 16:02:00

标签: google-apps-script google-docs google-docs-api

我正在尝试发送一封电子邮件,其中包含来自某个 google 文档的数据作为电子邮件正文,我不想将其作为附件发送,它特别必须以文本形式出现在正文中。 我已经尝试过以下内容:

 var file = DriveApp.getFileById(fileId);
 var fileBlob = file.getBlob();
 var blobToString = fileblob.toString();
 MailApp.sendEmail(email,subject,blobToString);

它会发送一封正文中包含 function () { [native code] } 的电子邮件。

我似乎无法弄清楚如何将 blob 作为文本获取,我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

解决方案:

最简单的方法是使用 DocumentApp 方法获取文档正文并获取文本。

例如,在这个非常简单的 Google 文档中:

enter image description here

您可以使用此脚本来获取文本。将 doc-id 替换为文档 ID。

function getText() {
  var document = DocumentApp.openById("<doc-id>");
  var text = document.getBody().getText();
}

示例结果:(转储到日志中)

enter image description here

参考:

Class Document

Class Body

P.S. 请注意,这是一个非常简单的解决方案。请花时间浏览参考资料,以从其他结构元素(如表格或页眉/页脚)中获取文本信息。