Google 脚本将附件保存到 GDrive,无需重复

发布于 2025-01-19 00:33:19 字数 747 浏览 2 评论 0原文

使用Google Workspaces,我需要一个脚本来从每日电子邮件中移动附件(始终具有相同的发件人,主题和附件名称)到Google Drive中的文件夹。

我已经尝试了此处找到的几个脚本,所有脚本都将附件移至驱动器 - 但是当我设置定期触发器进行执行时,所有先前和已移动的附件都会再次移动并创建重复。 我该如何执行此操作,而无需删除电子邮件或上一个文件?

下面似乎有一半的工作 - 但是我只需要适用于尚未将附件移动的新电子邮件。如果有一种方法只能指定XLS附件,但我不知道在哪里插入该位,也将很棒

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());
}
}

Using Google Workspaces, I need a script to move an attachment from a daily email - with always the same sender, subject and attachment name - to a folder in Google Drive.

I've tried several of the scripts found on here, which all move the attachment to drive - but when I set the periodic triggers for execution, all prior and already moved attachments are moved again and duplicates are created.
How can i do this without having to delete either the email or the previous file?

Below seems to half work - but I need this to only apply to NEW emails that haven't already had the attachments moved. Would also be great if there was a way to specify only the xls attachment but I don't know where to insert that bit

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());
}
}

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

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

发布评论

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

评论(2

你的往事 2025-01-26 00:33:20

劳伦(Lauren),这是两种方式,具体取决于您的邮箱的使用方式:

  1. 脚本可以在该主题和附件中寻找未读的电子邮件。保存附件,然后将电子邮件标记为读取。这样,您就不会重复。

  2. 如果也“手动”使用了电子邮件框,则使用邮箱的人们可能会标记电子邮件为“阅读”,这是第二种方式。
    一个。在邮箱中创建标签
    b。创建一个将此标签附加到新电子邮件和附件的过滤器。
    c。更改脚本以查找使用该标签的电子邮件。并保存附件后,从电子邮件中删除标签。

Lauren, here are two ways depending on how your mailbox is used:

  1. The script could look for unread emails with that subject and attachment. Save the attachment and then mark the email as read. That way you won't get duplicates.

  2. If the email box is used "manually" as well, i.e. the email might get marked "read" by folks using the mailbox, here is the 2nd way.
    a. create a label in the mailbox
    b. create a filter that attaches this label to new emails with that subject and attachment.
    c. change the script to look for emails with that label. And once the attachment is saved, remove the label from the email.

心的位置 2025-01-26 00:33:20

对于这个来说有点晚了,但这对我来说创造了奇迹:

function myFunction() {
var query = "label:yourLabel";
var folderId = "yourFolderID";

var folder = DriveApp.getFolderById(folderId);
var thread = GmailApp.search(query)[0];
var message = GmailApp.getMessagesForThread(thread)[0];
var attachments = message.getAttachments();

for (var i = 0; i<attachments.length; i++){

folder.createFile(attachments[i].copyBlob());

  }
  
thread.removeLabel(GmailApp.getUserLabelByName("yourLabel"));
thread.addLabel(GmailApp.getUserLabelByName("NewLabel"));

}

所以,本质上,这个是在寻找一个特定的标签(我已经制作了一个过滤器以在我的收件箱中应用),然后在运行循环后将其删除。另外,我添加了一个新标签,以便我可以跟上哪些标签已保存或未保存。

Sort of late for this one, but this has done wonders for me:

function myFunction() {
var query = "label:yourLabel";
var folderId = "yourFolderID";

var folder = DriveApp.getFolderById(folderId);
var thread = GmailApp.search(query)[0];
var message = GmailApp.getMessagesForThread(thread)[0];
var attachments = message.getAttachments();

for (var i = 0; i<attachments.length; i++){

folder.createFile(attachments[i].copyBlob());

  }
  
thread.removeLabel(GmailApp.getUserLabelByName("yourLabel"));
thread.addLabel(GmailApp.getUserLabelByName("NewLabel"));

}

So, in essence this one is looking for a specific label (which I've made a filter to apply within my inbox) and then removes it after running the loop. Also, I add a new label so I can keep up with which has already been saved or not.

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