Task guide
Save labeled Gmail attachments to Drive
Choose the email threads yourself by adding a Gmail label, then copy their attachments into an existing Drive folder.
1 · Prepare
Put these pieces in place
- Create a Gmail label named Save to Drive and add it only to threads whose attachments you want to copy.
- Create a Drive folder named Saved email attachments.
- Use attachment names that are distinct when the files have different contents.
Change these values
- Gmail label
Save to DriveOnly threads carrying this label are considered.
- Drive folder name
Saved email attachmentsThe folder must already exist and is where copies are saved.
2 · Review before running
What the script can access and change
Permissions you approve
- Read Gmail threads and their attachments.
- Create file copies in the chosen Google Drive folder.
Effects of one run
- Copies attachments into the Drive folder.
- Does not delete emails, remove labels, or change the original attachments.
3 · Copy
Paste this into the Apps Script editor
Create or open a script file, replace the example values marked above, then save the project.
const GMAIL_LABEL = 'Save to Drive'; // Only threads carrying this label are considered.
const DRIVE_FOLDER_NAME = 'Saved email attachments'; // The folder must already exist and is where copies are saved.
function saveLabeledAttachmentsToDrive() {
const label = GmailApp.getUserLabelByName(GMAIL_LABEL);
if (!label) throw new Error('Create a Gmail label named ' + GMAIL_LABEL + ' before running this function.');
const folders = DriveApp.getFoldersByName(DRIVE_FOLDER_NAME);
if (!folders.hasNext()) throw new Error('Create a Drive folder named ' + DRIVE_FOLDER_NAME + ' before running this function.');
const folder = folders.next();
label.getThreads().forEach((thread) => {
thread.getMessages().forEach((message) => {
message.getAttachments().forEach((attachment) => {
const attachmentName = attachment.getName();
if (attachmentName === null) throw new Error('An attachment has no file name. Give it a name before copying it to Drive.');
if (!folder.getFilesByName(attachmentName).hasNext()) {
folder.createFile(attachment.copyBlob());
}
});
});
});
} 4 · Run safely
Follow these steps in order
Label the threads
In Gmail, apply Save to Drive only to the conversations whose attachments you want copied.
Make the folder
In Drive, make one folder named Saved email attachments. The script stops instead of guessing if it cannot find it.
Run the function
Paste the function into Apps Script, save it, and run saveLabeledAttachmentsToDrive.
5 · Confirm
How to tell it worked
Attachments from the labeled threads appear in the Drive folder. Files with a name already in that folder are skipped.
If the result is different
Troubleshooting
- The script cannot find the label or folder.
- Create it with the exact name shown in the editable values, including spaces and capitalization.
- A new attachment did not copy.
- Check whether the folder already has a file with the same name. Rename one file if both versions should be kept.
Technical details
Names used in this script
Open these details when you want to adapt the example or look up an exact service or method name.
Show Apps Script symbols and service pages
Apps Script symbols
Service pages
Keep going
Related tasks, basics, and apps
- Task guide Run an Apps Script function every day
Run the setup function once, then Apps Script calls the reminder function each day around your chosen hour.
- Task guide Send an email for each row in a Google Sheet
Use one row per person, then let the script send only the rows that have not already been marked as sent.
- Object reference Gmail thread
A GmailThread holds one Gmail conversation. Reach threads with GmailApp.search() or label.getThreads(), then call getMessages() or addLabel().
- Object reference Drive folder
A DriveApp.Folder is a container in Google Drive. Find one by name with DriveApp.getFoldersByName(name) or by ID with DriveApp.getFolderById(id).
- Basics Apps Script permissions and authorization
Before a script can read your sheet or send your mail, you give it permission to access a list of OAuth scopes. This page walks through each authorization screen, explains @OnlyCurrentDoc, and shows how to revoke access.
- Basics Bound scripts and standalone scripts
Every Apps Script project is either attached to one Google file or standing on its own. The choice decides how your code reaches your data, who can see it, and where you find it later.
- Google app Gmail
GmailApp scripts can send messages, search threads, apply labels, and read attachments after you approve Gmail access for the project.
- Google app Google Drive
DriveApp lets a script open files by ID, find a named folder with getFoldersByName, and save copies or PDF exports in one known place.