Skip to content

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

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

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 safely

Follow these steps in order

  1. Label the threads

    In Gmail, apply Save to Drive only to the conversations whose attachments you want copied.

  2. Make the folder

    In Drive, make one folder named Saved email attachments. The script stops instead of guessing if it cannot find it.

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

Service pages

Keep going