我創建了一個腳本,可以將我的 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()
)
})
})
})
}
我希望有人可以幫助我謝謝!
uj5u.com熱心網友回復:
在這種情況下,如何存盤下載附件檔案的訊息 ID?這樣,在第二次運行腳本后,可以通過搜索郵件 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 存盤在電子表格中。第 2 次運行后,通過搜索訊息 ID,下載附件檔案。
參考:
- 包括()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/473287.html
