2
votes

I have documents coming in through E-mail from a scanner.

The scanner CANNOT change the subject. All email subjects from scanner are same "Scan to E-mail Server Job"

However, each file name is unique.

Google threads the messages, and I generally want to keep threading on.

I run a script to extract the PDF and put on drive, and then send the message to the trash.

The problem is.. future scans run the same script on the entire thread, so I end up with numerous copies of the same exact document every time the script is run.

I looked here and elsewhere this: permanently delete only one gmail message from a thread using a google script

    threads[i].moveToTrash();

EXPECTED BEHAVIOR: Don't run the script on messages in the trash. The problem is, the whole thread is labeled trash.

ACTUAL BEHAVIOR: It runs the script on the entire thread.. even the messages in the trash from same sender with same subject.

GOAL: Permanently delete messages so the script doesn't run on prior messages with same subject.

OR change subject to filename after receipt and stop threading on a message after its attachment is extracted

OR add a label that applies to a single message (not a thread) that doesn't clog up my labels/folders.

ALSO, to clarify, I can't just check for the filename being unique... because they are often renamed in the drive.

The last 2 lines below from http://www.googleappsscript.org/home/fetch-gmail-attachment-to-google-drive-using-google-apps-script are not working. The entire thread gets reprocessed... and all attachments get added again.

  var root = DriveApp.getRootFolder();
  for(var i in threads){
    var mesgs = threads[i].getMessages();
    for(var j in mesgs){
      //get attachments
      var attachments = mesgs[j].getAttachments();
      for(var k in attachments){
        var attachment = attachments[k];
        var isImageType = checkIfImage_(attachment);
        if(!isImageType) continue;
        var attachmentBlob = attachment.copyBlob();
        var file = DriveApp.createFile(attachmentBlob);
        parentFolder.addFile(file);
        root.removeFile(file);
      }
    }
    threads[i].addLabel(label);

    //ADDED BELOW TO MOVE TO TRASH
    threads[i].moveToTrash();
    threads[i].removeFromThread();


  }
1
When you want to completely delete a message in the thread without breaking down the thread, the method of Users.messages: delete in Gmail API can be used. Ref But from your question, I'm not sure about the part of your script for using this method. So can you provide your current script? I would like to confirm your script and think of the solution and/or workaround.Tanaike
Hello, I am using this: googleappsscript.org/home/… and github.com/ahochsteger/gmail2gdrive/blob/master/Code.gs The problem is listed in the limitations section. Everything has the same subject and same sender so it gets threaded.B D
Thank you for replying and updating your question. In your script, several threads are retrieved and all messages in each thread are retrieving. But in your question, it seems that the thread is constant. If the existing one thread ID is constant even when new message is received, how about checking only messages without checking all threads using the thread ID, and completely deleting the messages after the attachment files were retrieved?Tanaike
Is the script running on a trigger or are you doing it manually? if on a trigger you could just retrieve the latest message. If you're doing it manually then Tanaike's approach would work best, after looping through the attachments, use the message (mesgs[j]), get its ID and use it with the Users.messages: delete method as suggested above.AMolina

1 Answers

1
votes
  • You want to completely delete the messages in a thread and you want to also permanently delete the thread, after all attachment files were retrieved from all messages in the thread.
  • The subject of thread is always the same.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? In this answer, your script in your question was modified. Please think of this as just one of several answers.

When the thread is deleted using the method of Users.threads: delete, all messages in the thread is also deleted. This is reflected to your script.

In this method, the thread and all messages in the thread are permanently deleted. So please be careful this when you test it.

Modified script:

In this modification, 3 lines were added to your script in your question. Please check the following modified script. Before you run the script, please set the variable of subject which is used for checking the subject.

var subject = "sample subject"; // Added: Please set the subject of the thread.
var root = DriveApp.getRootFolder();
for(var i in threads){
  if (threads[i].getFirstMessageSubject() == subject) { // Added: Here, "subject" is checked.
    var mesgs = threads[i].getMessages();
    for(var j in mesgs){
      //get attachments
      var attachments = mesgs[j].getAttachments();
      for(var k in attachments){
        var attachment = attachments[k];
        var isImageType = checkIfImage_(attachment);
        if(!isImageType) continue;
        var attachmentBlob = attachment.copyBlob();
        var file = DriveApp.createFile(attachmentBlob);
        parentFolder.addFile(file);
        root.removeFile(file);
      }
    }
    Gmail.Users.Threads.remove("me", threads[i].getId()); // Added: Here, the thread and all mesagges in the thread are permanently deleted.
  }
}

Note:

  • Before you use this script, please enable Gmail API at Advanced Google Services.
  • From your question, I could understand that the subject of the first message of the thread is always same. Using this, when subject is the same with the subject of the first message of the thread, all attachment files are retrieved and the thread is permanently deleted.
  • In this method, the thread and all messages in the thread are permanently deleted. So I would like to recommend to test using a sample thread before you run the script for the actual email.
  • If there are several threads which have the same subject at the first message, above modified script is run for them. Also please be careful this.

Reference: