-1

Using Google Apps Script, is there a more efficient way to traverse my Gmail - picking out 'non-starred' emails that have a particular label assigned to them and then download the attachments to Google Drive?

My code works, but typically 'times out' after processing about 25 image attachments (using non-paid Gmail account)

The piece of code that does the work is as follows:

    // Loop through the messages for each thread
    for (var i = 0 ; i < messages.length; i++) {
      for (var j = 0; j < messages[i].length; j++) {
        var CurrentMsg = messages[i][j];
        
          if (!CurrentMsg.isStarred()){
            var att  = CurrentMsg.getAttachments();
            // If there were no attachments, create a 'dummy text file' to notify the user that there were no attachments for that email.
            var MsgSubject = CurrentMsg.getSubject();
            if (att.length == 0){
              var file = folder.createFile(MsgSubject,'There were no attachments',MimeType.PLAIN_TEXT);
            }
            else{  
              for (var k = 0; k < att.length; k++){
                var file = folder.createFile(att[k].copyBlob().getAs('image/jpeg').setName(MsgSubject));
              }
             }
            CurrentMsg.star();
          }
      }
    }  

Any tips gratefully received!

2 Answers2

0

If you want to look through certain emails only, like those that are not starred in your case, consider using the search() method. This will help you avoid looping over threads and messages you don't need.

If you need to bypass your maximum execution time, check out this answer and this article (also linked in the answer).

Dmitry Kostyuk
  • 1,354
  • 1
  • 5
  • 21
  • Hi Dmitry - thank you very much for taking the time to respond. I've never tried posting here so this is all a bit new to me and I should have provided a more specific question. I was aware of some of those articles to work around the timeout, but I was more interested to know if the method of creating a file for the attachment (using createFile method) was the most efficient way or if there was a better way to get the attachment 'saved' to google drive – Andy H. Nov 13 '21 at 18:50
0

I would recommend limiting results via a query then using the foreach function to go through messages:

// limit the list of messages to iterate through
var query = 'has:attachment label:particular'; 

var results = Gmail.Users.Messages.list(userId, {q: query});
results.messages.forEach(function (m) {
    var msg = GmailApp.getMessageById(m.id);
    msg.getAttachments().forEach(function (a) {
        var fileName = a.getName();
        fileName = saveAttachmentToFolder(folder, a, fileName, msg.getDate(), input.tz);
    });
});

The code snippet above is based on a Gmail add-on that I created, specifically for saving attachments to labeled folders in Drive: https://github.com/ellaqezi/archiveByLabel/blob/main/Code.gs#L24

In the label field, you can define nested directories to create in Drive e.g. foo/bar.

In the query field, you can copy the parameters as you would use them in Gmail's search bar.

ellaqezi
  • 41
  • 7