Email Attachment Backup with Google Apps Script
A Tutorial & Setup Guide
This guide describes how to set up a Google Apps Script solution that automatically fetches email attachments, renames them based on the email’s subject, and saves them into organized folders in Google Drive. The solution supports both a one‑time run and a daily fetch with deduplication, and it logs all actions in a common log sheet.
Overview
The solution performs the following tasks:
- One-Time Backup:
Fetches all emails (up until tomorrow) that match specified criteria, saves the attachments in a subfolder (named after the sender’s email) inside a parent folder on Google Drive, renames the files based on the email subject, and logs the transaction.
- Daily Backup:
Processes only today’s emails. For each sender, only one email per subject is processed. If no emails are processed, the script logs a “No emails received today” message. Files are renamed by their subject before saving, and every action is recorded in a common log sheet.
- Logging:
All results (from both one‑time and daily runs) are logged in a sheet named "AttachmentLog" with a field for “Run Type.”
- Folder Organization:
Attachments are saved in a common parent folder named "Google Drive Attachments". Under that, subfolders are created for each sender as specified in the configuration.
Prerequisites
Before you begin, make sure you have:
- A Google account.
- A Google Drive folder named "Google Drive Attachments" (create this folder manually in Google Drive).
- A Google Sheet that will serve as the dashboard for:
- Defining email criteria (in a sheet named "Schedule Backup")
- Viewing the log output (in a sheet named "AttachmentLog" — this will be created automatically if not present).
- Basic knowledge of how to open the Apps Script editor from your Google Sheet (via Extensions > Apps Script).
Setup & Installation
Follow these steps to set up the solution in your Google Sheet:
1. Create Your Google Sheet
- Open a new (or existing) Google Sheet.
- Create a sheet named "Schedule Backup".
- Column A: List the sender email addresses.
- Column B: (Optional) Add subject criteria for filtering emails.