Skip to content

Instantly share code, notes, and snippets.

@aquaerius
Last active December 5, 2022 19:09
Show Gist options
  • Save aquaerius/cd9f1598bab4ba36231ecbcfe5ffebd1 to your computer and use it in GitHub Desktop.
Save aquaerius/cd9f1598bab4ba36231ecbcfe5ffebd1 to your computer and use it in GitHub Desktop.
let counter = 0;
function myFunction() {
//Find emails from sender no-reply@harborfreight.com or noreply@harborfreight.com that contain receipts attached
let senders = ["noreply@harborfreight.com", "no-reply@harborfreight.com"]
// Save them to a folder in Drive
senders.forEach(
(sender) => { searchMail(sender); }
)
}
function addAttachmentsToFolder(message) {
let folder = DriveApp.getFolderById(<YOUR-DRIVE-FOLDER-ID>);
message.getAttachments().forEach(
(attachment) => {
counter++;
let date = message.getDate();
//Logger.log(date);
let name = date.getFullYear() + "/" +
(date.getMonth() + 1).toString() + "/" +
date.getDate().toString() + " " +
date.getHours().toString() + ":" +
date.getMinutes().toString() + " Receipt";
Logger.log(name);
let file = DriveApp.createFile(attachment.copyBlob()).setName(name)
folder.addFile(file);
}
);
Logger.log(counter);
}
function searchMail(sender) {
let threads = GmailApp.search('receipt has:attachment from:' + sender);
threads.forEach(
thread => {
let messages = thread.getMessages();
messages.forEach(
(message) => {
addAttachmentsToFolder(message);
}
)
}
)
}
@aquaerius
Copy link
Author

Google Apps Script to extract all receipts from a sender (Harbor Freight Tools) and save them to a folder in Google Drive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment