【问题标题】:Attach a Google Docs file in an email with Google App Scripts使用 Google App Scripts 在电子邮件中附加 Google Docs 文件
【发布时间】:2021-12-13 22:01:25
【问题描述】:

我正在尝试使用 Google App Scripts 将 Google Docs 文件附加到电子邮件中。 我有一个包含文件的文件夹,我的想法是从那里获取文件并使用保存在谷歌表格中的电子邮件地址通过电子邮件发送。

我创建并使用了这个:

function emailDocTestasDocx() {
  var id = '1egjazIip6zuPneKh2sVFTvbK06asd4ZgAvrgjw';// an example of Google doc
  var url = 'https://docs.google.com/document/d/1egjaasdasdeKh2sVFTvbK06z4JbPqnm4ZgAvrgjw';
  var doc = UrlFetchApp.fetch(url);
  var me = Session.getEffectiveUser().getEmail();
  MailApp.sendEmail(me, 'test', 'see attachment', {attachments:[doc]});
}

它可以工作,但最后的附件是一个 html,它将我重定向到文件夹内的文档。我不想要那个。我想发送附件,但我还没有找到方法。

有什么想法吗?谢谢!

【问题讨论】:

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


【解决方案1】:

正如 @Karan 在 cmets 上提到的,Google Docs 不是您可以在云之外打开的东西(它总是通过 docs.google 打开。 com 网站),因此您不能将它们作为文件附加到邮件中。您可以将文档附加为 PDF 或先将文档文件导出为扩展名为 .docx 的文档,然后将文件保存到您的 Google 云端硬盘:

将 Google doc 文件作为 PDF 发送的示例调整脚本:

function emailDocTestasDocx() {
  var id = '1egjazIip6zuPneKh2sVFTvbK06asd4ZgAvrgjw';// an example of Google doc
  var url = 'https://docs.google.com/document/d/1egjaasdasdeKh2sVFTvbK06z4JbPqnm4ZgAvrgjw';
  var doc = DriveApp.getFileById(id); //used ID to get the file saved in your Drive using DriveApp.getFileById() method
  var me = Session.getEffectiveUser().getEmail();
  MailApp.sendEmail(me, 'test', 'see attachment', {attachments:[doc]}); //attach the file via email
}

样本结果

您也可以查看这些类似帖子的答案:

【讨论】:

    猜你喜欢
    • 2021-05-31
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多