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();
}
mesgs[j]
), get its ID and use it with theUsers.messages: delete
method as suggested above. – AMolina