Created
May 2, 2015 14:55
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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