无需重新下载即可下载 gmail 附件的脚本
Script to download gmail attachment without downloading all over again
我创建了一个可以将我的 gmail 附件传输到 gdrive 的脚本,但我必须修改我的脚本,因为它会下载所有 gmail 附件,即使它已经下载了。
我可以知道我的脚本需要修改什么吗?
function saveGmailToGdrive(){
const folderId = "GDRIVE"
const searchItem = "label:MONEY"
const threads = GmailApp.search(searchItem,0,100)
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()
)
})
})
})
}
希望有人能帮帮我谢谢!
那么,如何存储下载附件的邮件ID?这样,在脚本的第 2 个 运行 之后,只能通过搜索邮件 ID 下载来自新邮件的附件。
修改后的脚本:
在使用此脚本之前,请创建一个新的电子表格并将电子表格 ID 设置为 spreadsheetId
。
function saveGmailToGdrive() {
const spreadsheetId = "###"; // Please create new Spreadsheet and put Spreadsheet ID here.
// 1. Retrieve message IDs from Spreadsheet.
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheets()[0];
const lastRow = sheet.getLastRow();
const values = lastRow == 0 ? [] : sheet.getRange(1, 1, lastRow).getValues().map(([a]) => a);
// 2. By checking the message IDs, the attachment files are downloaded.
const folderId = "GDRIVE"; // Please set your folder ID.
const searchItem = "label:MONEY";
const threads = GmailApp.search(searchItem, 0, 100);
const ids = threads.flatMap((thread) => {
const messages = thread.getMessages();
return messages.map((message) => {
const id = message.getId();
if (!values.includes(id)) {
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());
});
}
return [id];
});
});
// 3. Update message IDs on Spreadsheet.
if (ids.length == 0) return;
sheet.clear().getRange(sheet.getLastRow() + 1, 1, ids.length, 1).setValues(ids);
}
- 当此脚本第一次 运行 时,将从所有邮件中下载附件文件。在此 运行 中,消息 ID 存储在电子表格中。第二次运行后,通过搜索邮件ID,下载附件。
参考:
我创建了一个可以将我的 gmail 附件传输到 gdrive 的脚本,但我必须修改我的脚本,因为它会下载所有 gmail 附件,即使它已经下载了。
我可以知道我的脚本需要修改什么吗?
function saveGmailToGdrive(){
const folderId = "GDRIVE"
const searchItem = "label:MONEY"
const threads = GmailApp.search(searchItem,0,100)
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()
)
})
})
})
}
希望有人能帮帮我谢谢!
那么,如何存储下载附件的邮件ID?这样,在脚本的第 2 个 运行 之后,只能通过搜索邮件 ID 下载来自新邮件的附件。
修改后的脚本:
在使用此脚本之前,请创建一个新的电子表格并将电子表格 ID 设置为 spreadsheetId
。
function saveGmailToGdrive() {
const spreadsheetId = "###"; // Please create new Spreadsheet and put Spreadsheet ID here.
// 1. Retrieve message IDs from Spreadsheet.
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheets()[0];
const lastRow = sheet.getLastRow();
const values = lastRow == 0 ? [] : sheet.getRange(1, 1, lastRow).getValues().map(([a]) => a);
// 2. By checking the message IDs, the attachment files are downloaded.
const folderId = "GDRIVE"; // Please set your folder ID.
const searchItem = "label:MONEY";
const threads = GmailApp.search(searchItem, 0, 100);
const ids = threads.flatMap((thread) => {
const messages = thread.getMessages();
return messages.map((message) => {
const id = message.getId();
if (!values.includes(id)) {
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());
});
}
return [id];
});
});
// 3. Update message IDs on Spreadsheet.
if (ids.length == 0) return;
sheet.clear().getRange(sheet.getLastRow() + 1, 1, ids.length, 1).setValues(ids);
}
- 当此脚本第一次 运行 时,将从所有邮件中下载附件文件。在此 运行 中,消息 ID 存储在电子表格中。第二次运行后,通过搜索邮件ID,下载附件。