This Google Apps Script project automatically saves PDF attachments from specified Gmail senders to a designated Google Drive folder. It uses a Google Sheet to manage the list of email addresses to monitor.
- Reads email addresses from a specified Google Sheet
- Searches for emails from those addresses with attachments
- Saves PDF attachments to a specified Google Drive folder
- Avoids duplicate files
- Keeps track of the last execution date to avoid processing the same emails twice
Config.gs: Contains configuration constantsMain.gs: The main entry point for the scriptSheetOperations.gs: Handles reading data from Google SheetsGmailOperations.gs: Manages Gmail searches and saving attachments to DriveDateUtils.gs: Utilities for managing the last execution date
This script uses Google Apps Script's Properties Service to store configuration parameters. You need to set these properties before running the script.
- Open your Google Apps Script project in the Google Apps Script editor.
- Click on "Project Settings" (cog icon) in the left sidebar.
- Scroll down to the "Script Properties" section.
- Click "Add script property" and add the following properties:
SHEET_ID: The ID of your Google SheetSHEET_NAME: The name of the sheet containing email addressesCOLUMN_NAME: The name of the column containing email addressesTARGET_FOLDER_ID: The ID of the Google Drive folder where attachments will be saved
- Click "Save" to save your script properties.
SHEET_ID: This is the long string of characters in the URL of your Google Sheet between '/d/' and '/edit'.SHEET_NAME: The name of the specific sheet tab in your Google Sheet (usually visible at the bottom of the sheet).COLUMN_NAME: The header of the column containing the email addresses you want to monitor.TARGET_FOLDER_ID: This is the long string of characters in the URL of your Google Drive folder after 'folders/'.
If any of these properties are not set when the script runs, it will throw an error with a message indicating which property is missing.
- Node.js and npm installed on your machine
- Google account with access to Google Apps Script
-
Install clasp globally:
npm install -g @google/clasp -
Login to your Google account:
clasp login -
Create a new Google Apps Script project:
clasp create --type sheets --title "Gmail to Drive Attachment Saver" -
Clone this repository or create the script files locally.
-
Update the
Config.gsfile with your specific Google Sheet ID and target Google Drive folder ID. -
Push the code to Google Apps Script:
clasp push -
Open the script in the Google Apps Script editor:
clasp open
-
In the Google Apps Script editor, click on the clock icon on the left sidebar to open the Triggers page.
-
Click the "+ Add Trigger" button in the bottom right corner.
-
In the "Choose which function to run" dropdown, select
entryPoint. -
Under "Select event source", choose "Time-driven".
-
Select the type of time-based trigger you want (e.g., "Day timer" to run daily).
-
Choose the time of day you want the script to run.
-
Click "Save" to create the trigger.
- Set up the script properties as described in the Configuration section above.
- Ensure your Google Sheet is set up with a column containing the email addresses you want to monitor.
- The script will automatically run based on the trigger you set up, searching for emails from the specified addresses and saving PDF attachments to the designated Google Drive folder.
- You can also run the
entryPointfunction manually from the Google Apps Script editor to process emails immediately.
- Modify the
SHEET_ID,SHEET_NAME,COLUMN_NAME, andTARGET_FOLDER_IDinConfig.gsto match your specific Google Sheet and Drive folder. - Adjust the search query in
Main.gsif you want to change the criteria for which emails to process. - Modify the file type check in
GmailOperations.gsif you want to save attachments other than PDFs.
- If you encounter a "Configuration parameter X is not set" error, make sure you've correctly set all required script properties as described in the Configuration section.
- If you encounter issues, check the Execution log in the Google Apps Script editor for error messages.
- Ensure you have the necessary permissions for the Google Sheet and Drive folder you're accessing.
- Verify that your Gmail search query is correctly formatted and returning the expected results.
Feel free to fork this project and submit pull requests with any improvements or bug fixes.
This project is licensed under the MIT License.