Skip to content

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

Change these values

Make the code yours before you run it.
Gmail label
Save to Drive

Only threads carrying this label are considered.

Drive folder name
Saved email attachments

The 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.

Code.gs
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

  1. 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.

  2. Make the folder

    In Drive, create one folder named Saved email attachments. Use that exact name, including the capital S and the spaces.

  3. 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.

  4. 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

Service pages

Keep going