【问题标题】:need to download the link from mail using app script需要使用应用脚本从邮件中下载链接
【发布时间】:2021-10-08 15:53:05
【问题描述】:

我遇到了应用脚本问题,我正在尝试下载粘贴在内容中的链接以进行驱动。

邮件示例如下

报告下载网址:xxxxxx.xlsx

上面的 xxxxx.xlsx 是我们下载文件的链接。 我试过下面的应用程序脚本代码。其他附件被下载到驱动器,但上面的链接没有被下载。请帮忙。

function saveAttachmentInFolder(){
  const folderId = 'Drive id';
  const searchQuery = 'Report download URL';
  const threads = GmailApp.search(searchQuery, 0,25);
  threads.forEach(thread => {
    const messages = thread.getMessages();
        messages.forEach(message => {
        const attachments = message.getAttachments({
          includeInlineImages: false,
          includeAttachments: true
      });
        attachments.forEach(attachment => {
        Drive.Files.insert(
          {
            title: attachment.getName(),
            mimeType: attachment.getContentType(),
            parents: [{ id: folderId }]
          },
          attachment.copyBlob()
        );
      });
    });
  });
};

【问题讨论】:

  • 该文件是附加到电子邮件本身,还是仅包含一个下载文件的链接?
  • 电子邮件包含下载文件的链接,文件未附加

标签: google-apps-script gmail-api


【解决方案1】:

很明显你不能用getAttachments得到它,因为它不是附件

您可能需要抓取电子邮件的内容。

/*
declare before the loop
const urls = [];
*/

const body = message.getBody();
//const body = message.PlainBody();
// you may need to adjust it in case there is any html code
const match = body.match(/Report download URL:.*(\S+\.xlsx)/);
if (match) { urls.push(match[1]); }

参考资料:

getBody()

getPlainBody()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多