Save files to Google Drive automatically from Gmail

Share on:

I have been using Gmail since it’s inception when it was still an invite only email service and haven’t looked back. It’s flexibility and speed have made it my personal email account of choice and with Google steadily adding more services to Gmail and their other online collaboration tools it’s always getting better. So I was intrigued when I found out that Google were giving you a way to extend their Google Apps services further with Google App Script. Although it’s primarily aimed towards businesses that are leveraging Google Apps, you can use App Script to automate your own workflows to make your digital life that little bit easier.

For more information on App Script please visit the App Script Developer Site

In this post I will share a quick Google App Script that I wrote to save email bills sent as PDF’s automatically to my Google Drive, in the correct folder and then label the original email with a Gmail label. It’s pretty annoying having to go through your email and click save and then placing the file into the right folder each time, why not just automate it, so it’s in the right folder with the month-year of when it was received and synced to my laptop.

So let’s start by first creating our App Script Project, head over to script.google.com and change the project name to something descriptive, like Save PDF’s from Telstra. Following the Project name, copy and paste the following JavaScript code to the App Script IDE:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function Telstra() {
   
  //Create folder Telstra_Bills, if it doesn't exist
   var folderName = 'Telstra_Invoices'
    
   //Control Folder
   var controlLabel = GmailApp.getUserLabelByName("attachmentsSavedtoDrive");
   var fileLabel = GmailApp.getUserLabelByName("Finance/Telstra");
 
  var folder = DriveApp.getFoldersByName(folderName);
    if (folder.hasNext()) {
    folder = folder.next();
  } else {
    folder = DriveApp.createFolder(folderName);
  }
   
  //search for the following in Gmail to create a list of threads
  var threads = GmailApp.search('from:*@online.telstra.com and has:attachment -label:attachmentsSavedtoDrive'); 
   
  //From Threads, get the first message in the thread, in this case the received message
  var msgs = GmailApp.getMessagesForThreads(threads);
   
  //Loop over each message, saving all attachements in given 
  for (var i = 0 ; i < msgs.length; i++) {
    for (var j = 0; j < msgs[i].length; j++) {
      var attachments = msgs[i][j].getAttachments();
      for (var k = 0; k < attachments.length; k++) {
        var fileName = msgs[i][j].getDate().toString();
        file = folder.createFile(attachments[k].copyBlob());
        file.setName(fileName.match(/\d{4}/) + " " + fileName.match(/\s\w{3}/) + " Telstra Invoices.pdf");
      }
   }
  }
 
  //loop through each thread and apply attachmentsSavedtoDrive, so that these files aren't indexed next time
  for (var x = 0; x < threads.length; x++) {
     controlLabel.addToThread(threads[x]);
     fileLabel.addToThread(threads[x]);
  }
}
Lines 4 – 8 (highlighted) are variables that you will need to change for your own particular bill or purpose.

Continuing on, we now need to save and run the script’s Main function(), when you run the script initially you will be presented with the following dialog box’s asking you to authorise the code to be run against your Google services.

If you allow the script to be run, you should see a new folder created in your Google Drive called Bills with the relevant bills saved and with the correct timestamp of the year-month they were received.

So the last thing you need to do is to add a trigger, the trigger is when you want the script to be run automatically without any user interaction.

Clicking the timepiece icon will present the following dialogue:

I run my script once a week, but you can run it however often you would like.

So there you have it, a script to save email attachments sent via Gmail to Google Drive automatically, hopefully you can use this snippet of code for your own workflows.