Skip to content

Latest commit

 

History

History
29 lines (23 loc) · 823 Bytes

Save_Attachments_To_Drive.md

File metadata and controls

29 lines (23 loc) · 823 Bytes

TSCodeTalk > Gmail > Save Gmail Attachments to Google Drive

This script saves Gmail attachments to Google Drive.


function saveAttachmentsToGoogleDrive() { 
  const driveFolder = "Gmail Attachments";
  const threads = GmailApp.search("has:attachment newer_than:1d"); 
  const folder = DriveApp.getFoldersByName(driveFolder);
  
  if (folder.hasNext()) {
    folder = folder.next();
  } else {
    folder = DriveApp.createFolder(driveFolder);
  }
  
  threads.forEach(thread => {
    thread.getMessages().forEach(message {
      const desc = message.getSubject() + " #" + message.getId();
      message.getAttachments().forEach(att => {
        folder.createFile(att).setDescription(desc);
      });  // end attachments.forEach()
    }); // end messages.forEach()
  }); // end threads.forEach()
}