Archive Gmail attachments to Google Drive with a Sheets log

Auto-file every Gmail attachment into Google Drive folders by sender domain and month, and log each file to a Google Sheets tracker so nothing gets lost.

Deterministic Code
GmailGoogle DriveGoogle SheetsPersonal ProductivityOperationsDocument ProcessingData SyncEmail Automation

Build a code workflow that archives every Gmail attachment I receive into a tidy Google Drive folder structure and logs it in a Google Sheets tracker, so I can find any attachment later without searching my inbox.

Trigger: poll Gmail for new emails using the new_email event on my connected Gmail account. For each new message, only continue if the message has at least one non-inline attachment.

For every triggering message, use Gmail Get a Message (full format) to load the message and walk its parts. Treat a part as an attachment when it has a filename and an attachmentId, and skip parts that are inline (Content-Disposition inline) so signature logos and embedded images are ignored.

For each real attachment, call Gmail Get Attachment with the messageId and attachmentId to download the base64url-encoded bytes. Decode them in memory so they can be uploaded to Drive.

Derive the destination Drive folder path as senderDomain/YYYY-MM, where senderDomain is parsed from the From header (everything after the @, lowercased) and YYYY-MM is the year and month from the message internalDate in my local timezone. Under a configurable root folder ID, ensure both the domain folder and the year-month subfolder exist by listing children first and calling Google Drive Create Folder only when missing. Cache folder IDs across attachments in the same run so we do not recreate them.

Upload each attachment into the resolved year-month folder. If the file size is 5MB or less, use Google Drive Upload File (Multipart). If it is larger than 5MB, use Google Drive Upload File (Resumable). Keep the original filename, set the correct MIME type from the Gmail part headers, and capture the returned Drive file ID and webViewLink.

After each successful upload, append one row to a configurable Google Sheets tracker using Google Sheets Append Values. Columns, in order: received timestamp (ISO 8601, from internalDate), sender (full From address), subject, filename, file size in bytes, Drive file URL (webViewLink), and Gmail message link (https://mail.google.com/mail/u/0/#inbox/<messageId>). One row per attachment, so a single email with three attachments produces three rows.

Inputs the workflow should expose: the Gmail account to monitor, the Drive root folder ID where domain folders live, and the Google Sheets spreadsheet ID plus tab name for the tracker. Optionally allow a Gmail query filter (for example has:attachment -label:archived) to narrow which messages are processed.

Make this fully deterministic with no LLM calls. Handle errors per attachment so one failed upload does not block the rest of the message, and log enough context (messageId, filename) to retry.

Additional information

What does this prompt do?
  • Polls Gmail for new emails and processes any message that has one or more attachments.
  • Builds a tidy Google Drive folder path for each file, organized by sender domain and year-month, creating folders the first time they are needed.
  • Uploads each attachment to Drive using a multipart upload for files under 5MB and a resumable upload for anything larger.
  • Appends one row per attachment to a Google Sheets tracker with the timestamp, sender, subject, filename, file size, Drive link, and a link back to the original Gmail message.
What do I need to use this?
  • A connected Gmail account that can read messages and download attachments.
  • A connected Google Drive account with permission to create folders and upload files in your target Drive.
  • A connected Google Sheets account and a spreadsheet to use as the attachment tracker.
  • A root Drive folder ID (where domain folders should be created) and the spreadsheet ID and tab name for the log.
How can I customize it?
  • Change the folder naming scheme, for example sender domain plus year-quarter, sender email address, or a flat date-only structure.
  • Filter which messages are archived by adding rules on Gmail label, sender, subject, or attachment file type.
  • Adjust the Sheets tracker columns, for example add Gmail labels, attachment MIME type, or a checksum for deduplication.

Frequently asked questions

How does the workflow decide which Drive folder to use?
It parses the From address on each Gmail message to get the sender domain, then nests a year-month folder underneath. So an attachment from jane@acme.com received in March 2026 goes to acme.com/2026-03 inside your chosen root folder.
What happens with very large attachments?
Anything 5MB or smaller uses Google Drive's multipart upload for speed. Anything larger automatically switches to a resumable upload so big files do not fail mid-transfer.
Will it skip inline images and signature logos?
Yes. The workflow walks the Gmail message parts and only downloads parts that are real attachments, ignoring inline images embedded in the email body.
Can I find the original email later?
Every row in the Google Sheets log includes a direct link back to the source Gmail message, alongside the Drive file URL, so you can jump from the spreadsheet to either the file or the email it came from.
Is this a deterministic workflow or does it use an AI model?
It is fully deterministic. Every step is a documented Gmail, Drive, or Sheets operation, so it runs as a code workflow with no LLM calls in the path.

Stop hunting for that attachment from last quarter.

Connect Gmail, Google Drive, and Google Sheets once, and Geni files every new attachment for you with a searchable log.