A Google Apps Script that automatically extracts PDF attachments from Gmail messages, saves them to Google Drive, and logs relevant data in a Google Sheet.
- Search Gmail for emails with a specific label.
- Extract PDF attachments and save them to a designated Google Drive folder.
- Append metadata to a Google Sheet:
- Iterative ID
- File name
- Email date
- Subject
- Sender
- Drive link
- Automatically increments ID for new files.
- Removes the label from processed emails.
- Gmail Search: The script searches Gmail using a label query defined in
SCRIPT_CONFIG.GMAIL_SEARCH_QUERY. - PDF Extraction: All PDF attachments in the matched emails are saved to a Google Drive folder specified in
SCRIPT_CONFIG.DRIVE_FOLDER_ID. - Logging: Metadata of each PDF is appended to the sheet defined in
SCRIPT_CONFIG.SPREADSHEET_IDandSCRIPT_CONFIG.SHEET_NAME. - Label Management: After processing, the script removes the label from the email to avoid duplicate processing.
- Create a Google Sheet to log the PDF data.
- Create a Google Drive folder to store the PDFs.
- Set up the script:
- Open Google Apps Script.
- Copy the script into a new project.
- Update
SCRIPT_CONFIGwith your:- Gmail search query (
GMAIL_SEARCH_QUERY) - Drive folder ID (
DRIVE_FOLDER_ID) - Spreadsheet ID (
SPREADSHEET_ID) - Sheet name (
SHEET_NAME) - File prefix (
FILE_PREFIX)
- Gmail search query (
- Run the script and authorize permissions for Gmail, Drive, and Sheets.
const SCRIPT_CONFIG = {
TARGET_MAILBOX_EMAIL: "me",
GMAIL_SEARCH_QUERY: "label:Anhang_speichern",
DRIVE_FOLDER_ID: "YOUR_DRIVE_FOLDER_ID",
SPREADSHEET_ID: "YOUR_SPREADSHEET_ID",
SHEET_NAME: "Posteingangsbuch",
FILE_PREFIX: "PE_",
};