TriggerShelf

Recipe

Save Gmail attachments to Google Drive automatically

Trigger: Time-driven, hourlyServices: GmailApp, DriveAppMock-tested: Nov 2026

Every hour the script finds recent mail with attachments, copies each attachment into one Drive folder with a date prefix, and labels the thread so it is not processed twice.

Set it up

  1. Make a Drive folder and copy its ID from the address bar (the text after /folders/).
  2. Open script.google.com, paste the script, set folderId in GA, and save.
  3. Triggers > Add trigger > saveAttachments > Time-driven > Hour timer > Every hour. Approve the prompts.

The script

/**
 * Save Gmail attachments to one Drive folder, then label the thread.
 * Set folderId, then add a time-driven trigger (every hour) for saveAttachments.
 */
var GA = { query: 'has:attachment -label:saved-to-drive newer_than:7d', folderId: 'PASTE_FOLDER_ID', label: 'saved-to-drive', maxThreads: 20 };

function saveAttachments() {
  var folder = DriveApp.getFolderById(GA.folderId);
  var label = GmailApp.getUserLabelByName(GA.label) || GmailApp.createLabel(GA.label);
  var threads = GmailApp.search(GA.query, 0, GA.maxThreads);
  var saved = 0;
  threads.forEach(function (thread) {
    thread.getMessages().forEach(function (msg) {
      msg.getAttachments({ includeInlineImages: false }).forEach(function (att) {
        var day = Utilities.formatDate(msg.getDate(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
        folder.createFile(att.copyBlob()).setName(day + ' ' + att.getName());
        saved++;
      });
    });
    thread.addLabel(label);
  });
  return saved;
}
Test status. 3 of 3 automated checks pass, run Nov 2026 against simulated Google services. This recipe has not yet been run inside a live Google account. Test it on a copy of your sheet first.

What you should see

FileSaved
2026-11-12 invoice-1043.pdfyes
2026-11-13 timesheet.csvyes

Drive folder after one run. Sample values, not output from a real account.

Common errors and fixes

Exception: No item with the given ID could be found
folderId is wrong, or the account has no access to that folder.
Exceeded maximum execution time
Lower maxThreads to 10. See the fix page for batching.
Attachments are saved again
The label saved-to-drive was removed from the thread, or the search no longer excludes it.

Permissions. Running this script asks you to grant Google permissions for the services listed above, to your own script. TriggerShelf never receives your Google data. See the permissions table. Google will show "Google hasn't verified this app"; that is expected for a script you paste yourself.