Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spike on automatically saving Google Apps invoice attachment from Gmail into Google Drive using Google App Script
function myFunction() {
var threads = GmailApp.search("label:inbox subject:'Google Enterprise: Your invoice is attached'");
for (var threadIndex = 0 ; threadIndex < threads.length; threadIndex++) {
var messages = threads[threadIndex].getMessages();
if (messages.length == 0) {
Logger.log('No messages found in thread.');
return;
}
var message = messages[0];
var attachments = message.getAttachments();
if (attachments.length == 0) {
Logger.log('No attachments found in message.');
return;
}
var attachment = attachments[0];
var datestamp = new Date().toISOString().substring(0,10);
var filename = datestamp + ' Google Apps - Invoice ' + attachment.getName();
var folders = DriveApp.getFoldersByName('Invoices and receipts');
if (!folders.hasNext()) {
return;
}
var folder = folders.next();
Logger.log('Saving attachment ' + attachment.getName() + ' to Drive with name ' + filename + '.');
folder.createFile(attachment.setName(filename));
Logger.log('Done.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.