Task guide
Save labeled Gmail attachments to Drive
Copy the attachments from chosen Gmail conversations into one Drive folder. You pick the conversations by adding a label to them, and the script saves every attachment it finds there.
1 · Prepare
Put these pieces in place
- Create a Gmail label named Save to Drive and add it only to conversations whose attachments you want to copy. Start with one or two.
- Create a Drive folder named Saved email attachments. If the folder is missing, the script stops with an error.
- Go to script.google.com and click New project, or use a project you already have.
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, compose, send, and permanently delete all your email from Gmail. The code only reads the conversations that carry your label and their attachments.
- See, edit, create, and delete all of your Google Drive files. The code finds one folder by name and adds copies of attachments to it.
Effects of one run
- Saves a copy of every attachment in every message of every labeled conversation into the Drive folder, under its original file name.
- Skips an attachment when a file with the same name is already in the folder, so running the function twice does not make duplicates. Two different attachments with the same name are also treated as one.
- 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
Follow these steps in order
Label one or two conversations
In Gmail, open a conversation that has an attachment, click the label icon above it, and apply Save to Drive. Start with one or two conversations so the first run is easy to check.
Make the folder
In Drive, create one folder named Saved email attachments. Use that exact name, including the capital S and the spaces.
Paste the function and run it
Paste the function over the starter code and save. Choose saveLabeledAttachmentsToDrive in the function dropdown above the code, then click Run. Approve access when Google asks.
Check the folder
Open Saved email attachments in Drive. Each attachment from the labeled conversations is there under its original file name. Label more conversations and run the function again. Files whose name is already in the folder are skipped.
5 · Confirm
How to tell it worked
The Saved email attachments folder holds one file per attachment from the labeled conversations. Running the function again adds only attachments whose file name is not in the folder yet.
If the result is different
Troubleshooting
- The run stops with "Create a Gmail label named Save to Drive before running this function."
- Create the label in Gmail with that exact name, including the capital letters and spaces, or change GMAIL_LABEL in the code to the name of your label.
- The run stops with "Create a Drive folder named Saved email attachments before running this function."
- Create the folder in Drive with that exact name, or change DRIVE_FOLDER_NAME in the code to the name of your folder. If two folders share the name, the script uses the first one Drive returns.
- An attachment did not appear in the folder.
- Look in the folder for a file with the same name. The script skips an attachment when a file with that name is already there. Rename the file in Drive if you want to keep both versions, then run the function again.
- The run stops with "An attachment has no file name. Give it a name before copying it to Drive."
- Download that attachment from the message in Gmail and save it to the folder yourself. Gmail returned no name for it, so the script cannot create the file.
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
Make a function run on its own once a day. Run the setup function once, and from then on Apps Script calls the reminder function each day around the hour you chose.
- Task guide Send an email for each row in a Google Sheet
Send one email to each person listed on a Customers tab. The script marks each row Sent as it goes, so a later run sends only to rows that are not marked yet.
- Object reference Gmail thread
A GmailThread is one Gmail conversation. Get 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 kind of project affects how your code opens files, who can see the code, 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 PDFs to a folder you choose.