Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created May 2, 2015 14:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save floehopper/b13fd71e9aa7946692bf to your computer and use it in GitHub Desktop.
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