用于查看电子邮件并将电子表格附件复制到 Google 云端硬盘的 Google 脚本

发布于 2025-01-10 18:46:22 字数 500 浏览 0 评论 0原文

每个星期一,我都会收到一份发送到我的 Gmail 的报告,其中包含两个 Google 电子表格附件。

我每周都需要打开这些附件并将其复制到我的 Google 云端硬盘中的特定文件夹中。

我想要一个能够

1) 的 Google 脚本。每个星期一查看我的 Gmail 并搜索一封新电子邮件,其主题为:“截至 [日期] 的每小时资历报告和截至 [日期] 的每小时终止报告”| 注意:同一用户每周发送报告

2)。复制这两个附件,然后通过文件夹 ID 将副本移动到 Google 文件夹。

我对 Google 脚本相当陌生,在网上查找时,我还没有找到太多可以帮助我实现这种自动化的东西。有谁对如何解决这个问题有任何想法?谢谢你!

每周电子邮件内容

Every Monday, I get a report sent to my Gmail that has two Google Spreadsheet attachments.

I need to open these attachments and make a copy of them into a specific folder in my Google Drive each week.

I would like a Google Script that will

1). Look through my Gmail each Monday and search for a new email with the subject: "Hourly Seniority Report as of [date] and Hourly Termination Report as of [date]" | Note: The same user sends out the report each week

2). Make a copy of the two attachments and move the copy to a Google Folder via the Folder's ID.

I'm fairly new to Google scripting and looking online, I haven't found much that could help me with this automation. Does anyone have any ideas on how to approach this? Thank you!

Weekly Email Contents

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我要还你自由 2025-01-17 18:46:22

如果文件附加到电子邮件

  • 查找具有特定主题的电子邮件:这可以通过 GmailApp.search() 函数来实现(您可以阅读有关它的更多信息< a href="https://developers.google.com/apps-script/reference/gmail/gmail-app#searchquery" rel="nofollow noreferrer">此处)。请注意,这将返回一个线程列表,每个线程依次包含一个消息列表。只要没有其他具有该主题的电子邮件(似乎是这种情况),抓取第一个线程的第一条消息(我的方法)就可以正常工作。
  • 将附件保存到 Google 云端硬盘。这可以通过 Folder 对象的 createFile() 函数来完成(您可以在此处阅读有关它的更多信息)。您可以使用 getFolderById() 函数获取所需的 Folder 对象(您可以阅读有关它的更多信息 此处)。

function myFunction() {
  var emailSubject = ""; // YOUR EMAIL SUBJECT
  var folderId = ""; //YOUR FOLDER ID (FOUND IN URL)

  var folder = DriveApp.getFolderById(folderId);

  var thread = GmailApp.search(emailSubject)[0];
  var message = GmailApp.getMessagesForThread(thread)[0];
  var attachments = message.getAttachments();
  
  for (var i = 0; i<attachments.length; i++){
    folder.createFile(attachments[i].copyBlob());
  }
}

如果该文件是共享云端硬盘的链接

,在这种情况下,该文件将不会显示为电子邮件的附件,因此此处无法采用之前使用的相同方法。不过,该文件将在您的云端硬盘中显示为与我共享。我在这里使用的方法是收集与您共享的所有文件(使用 searchFiles() 函数,您可以阅读有关它的更多信息此处),然后扫描电子邮件的纯文本以查找该电子邮件中是否提及了每个文件的 ID(意思是它是共享)。

function myFunction() {
  var emailSubject = ""; // YOUR EMAIL SUBJECT
  var folderId = ""; //YOUR FOLDER ID (FOUND IN URL)

  var folder = DriveApp.getFolderById(folderId);

  var thread = GmailApp.search(emailSubject)[0];
  var message = GmailApp.getMessagesForThread(thread)[0];
  
  var files = DriveApp.searchFiles("sharedWithMe = true");

  while (files.hasNext()) {
    var file = files.next();
    if (message.getBody().includes(file.getId())){

      folder.createFile(file.getName(), file.getBlob(), file.getMimeType());
    }
  }

}

当然,您可以将此脚本设为时间限制触发脚本,以便根据您的意愿每天执行一次。在这种情况下,脚本应动态获取执行日期,以每天收集正确的电子邮件。您可以在此处详细了解触发器。

If the file is attached to the email

  • Find an email with an specific subject: This can be achieved with the GmailApp.search() function (you can read more about it here). Note that this will return a list of threads that each one in turn will contain a list of messages. Grabbing the first message of the first thread (my approach) will work fine as long as there are no other emails with that subject (which seems to be the case).
  • Saving an attachment into Google Drive. This can be accomplished with the createFile() function of a Folder object (you can read more about it here). You can get the desired Folder object by using the getFolderById() function (you can read more about it here).

function myFunction() {
  var emailSubject = ""; // YOUR EMAIL SUBJECT
  var folderId = ""; //YOUR FOLDER ID (FOUND IN URL)

  var folder = DriveApp.getFolderById(folderId);

  var thread = GmailApp.search(emailSubject)[0];
  var message = GmailApp.getMessagesForThread(thread)[0];
  var attachments = message.getAttachments();
  
  for (var i = 0; i<attachments.length; i++){
    folder.createFile(attachments[i].copyBlob());
  }
}

If the file is a link to a shared Drive

In this case, the file will not show up as an attachment to the email, so the same method used before cannot be employed here. However, the file will show up as shared with me in your Drive. The approach I used here is to gather all the files that are shared with you (using the searchFiles() function, you can read more about it here) and then scan the email's plain text to find whether or not each file's id was mentioned in that email (meaning it was shared).

function myFunction() {
  var emailSubject = ""; // YOUR EMAIL SUBJECT
  var folderId = ""; //YOUR FOLDER ID (FOUND IN URL)

  var folder = DriveApp.getFolderById(folderId);

  var thread = GmailApp.search(emailSubject)[0];
  var message = GmailApp.getMessagesForThread(thread)[0];
  
  var files = DriveApp.searchFiles("sharedWithMe = true");

  while (files.hasNext()) {
    var file = files.next();
    if (message.getBody().includes(file.getId())){

      folder.createFile(file.getName(), file.getBlob(), file.getMimeType());
    }
  }

}

Of course, you can make this script a Time bound trigger script so that it will execute once a day if you wish so. In that case, the date of the execution should be grabbed dynamically by the script to gather the correct email each day. You can read more about triggers here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文