Google Apps Script Real World Use Case
If you’re like me and occasionally receive scanned documents or attachments that need to be stored safely, automating this process can save time and reduce manual effort. In this guide, I’ll walk you through how to automatically save attachments from specific Gmail emails directly to a Google Drive folder using Google Apps Script.
Why Automate This?
Manually saving attachments can be tedious and error-prone. By leveraging Google Apps Script, we can automate this task, ensuring that any relevant emails are processed without missing a beat.
Other Use Cases
This automation can be extended to various other scenarios, such as organizing invoices and receipts for financial tracking, archiving reports for business analytics, or managing client files for seamless collaboration. For example, you can automatically save resumes in a recruitment folder, back up critical legal documents, or even store course materials received via email. The flexibility of Google Apps Script allows you to tailor this solution for any recurring email-to-storage workflow, making it a powerful tool for both personal and professional use.
The Setup
For this example, we’ll automate saving any email attachments from a specific sender into a designated Google Drive folder. In my case, I frequently receive scans from <my-id>@print.epsonconnect.com
, and I want to store these in a folder called “Scanned” in Google Drive.
Step 1: Create a Gmail Filter
First, let’s create a filter in Gmail so that all emails from this sender are labeled “Scanned”:
- Open Gmail and click the search bar.
- Type
from:<my-id>@print.epsonconnect.com
and hit Enter. - Click the dropdown arrow next to the search bar and choose “Create filter.”
- In the filter options, select “Apply the label” and create a new label named “Scanned.”
- Save the filter.
Now, any email from this address will automatically be labeled “Scanned.”
Step 2: Write the Google Apps Script
Next, we’ll write the script that will look for emails with this label and save their attachments to a Google Drive folder named “Scanned”:
- Go to Google Apps Script or open Google Drive and click
New
→More
→Google Apps Script
. - Replace the default code with the following:
function saveGmailAttachmentsToDrive() {
var label = GmailApp.getUserLabelByName("Scanned");
var threads = label.getThreads();
var folder = DriveApp.getFoldersByName("Scanned").next();
if (threads.length === 0) {
Logger.log("No emails found with the 'Scanned' label.");
}
threads.forEach(function(thread) {
var messages = thread.getMessages();
messages.forEach(function(message) {
var attachments = message.getAttachments();
attachments.forEach(function(attachment) {
folder.createFile(attachment);
});
});
thread.removeLabel(label); // Remove the label after processing
});
}
How the Script Works:
- Label and Folder Setup: The script first identifies the Gmail label (“Scanned”) and the Google Drive folder (“Scanned”).
- Processing Emails: The script loops through all emails with this label, extracts the attachments, and saves them to the Google Drive folder.
- Optional Label Removal: After processing each email, the script removes the “Scanned” label so that it doesn’t reprocess the same emails next time.
Step 3: Set Up a Trigger
To run this script automatically:
- In the Apps Script editor, go to
Edit
→Current Project’s Triggers
. - Click “Add Trigger” and set the script to run periodically (e.g., every hour).
Now the script will automatically check for any new emails with the “Scanned” label and save their attachments to Google Drive.
Bonus: Handling No Emails
If no emails are found with the “Scanned” label, the script will simply log a message and exit. You can view these logs by going to View
→ Logs
in the Apps Script editor.
Conclusion
With this simple setup, you can automate the process of saving email attachments to Google Drive, helping you stay organized without manual effort. This solution is scalable and can be customized for other use cases—like archiving invoices, reports, or any recurring documents you receive via email.