1
votes

I would like to save gmail attachments(.pdf files) from a specific email in a specific Google Drive folder. I also need to rename file with a string composed by some string of the email.

I have developed a simple script using Google Apps Script with some functions.

This is the main function I have wrote:

function GmailToDrive() {
    var query = '';
    query = 'in:inbox from:[email protected] has:nouserlabels ';
    var threads = GmailApp.search(query);
    var label = getGmailLabel_(labelName);
    var parentFolder;
    if (threads.length > 0) {
        parentFolder = getFolder_(folderName);
    }
    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();
            var message_body = mesgs[j].getBody();
            for (var k in attachments) {
                var attachment = attachments[k];
                var isDefinedType = checkIfDefinedType_(attachment);
                if (!isDefinedType) continue;
                var attachmentBlob = attachment.copyBlob();
                var file = DriveApp.createFile(attachmentBlob);
                file.setName(renameFile_(attachment, message_body))
                parentFolder.addFile(file);
                root.removeFile(file);
            }
        }
        threads[i].addLabel(label);
    }
}

The checkIfDefinedType_(attachment) function checks if the attachments is a .pdf file and the renameFile_(attachment, message_body) rename the attachment extracting some string from the email.

The script seems to be correctly developed but sometimes I have two or more same attachments saved in my google drive folder.

1
Welcome. "but sometimes I have two or more same attachments saved in my google drive folder." I do not understand what this means; would you please explain in more detail? Also, the code for GmailToDrive is pretty well known and reliable but I don't recall having seen the extra line of code file.setName(renameFile_(attachment, message_body)); would please provide the code for this function.Tedinoz
The script scans all Gmail items according to parameter of the query and has to save the attachment of no labeled mails to Google Drive. The script runs every 30 minutes and sometimes some attachments are saved more than one time in Google Drive folder.Stefano Ferrario
file.setName is Google App Script function of Class File (developers.google.com/apps-script/reference/drive/file). The parameter of this function is a string obtained by the function renameFile_(attachment, message_body) that extract some string from the email text and concatenates them into a single stringStefano Ferrario
"sometimes some attachments are saved more than one time in Google Drive folder". So what is the problem; that the code should look for duplicates and not process them twice? Perhaps even delete the duplicate?Tedinoz
The script processes all Gmail mails only if they have no user labels and save attachments. I can't understand why the script processes again labeled mails and so saves attachments again. we can consider "duplicate control" the label added to the mal after it's processed. Maybe I have put some conditions not in the right place...Stefano Ferrario

1 Answers

1
votes

Stefano, I had the same issue, if this is the same code as adapted from https://bit.ly/-extractgmail

I removed the line "for (var i in fileTypesToExtract) {" which was causing duplicates for me. It was running the query for each of the file types.