简体   繁体   中英

Extract all messages in thread from gmail into a spreadsheet

I am trying to utilise this code to extract job applicant data from my inbox and populate google sheets. This is easy enough but problem is the applications are first processed by our website plugin and then sent my inbox. This changes the sender to the same address. Google addes each recipient to the same thread which then prevents the code from extracting the information from each application that arrives.

At present, the first of each message is being retrieved. I know i need to implement a loop to iterate across the threads but I have had no luck at this to date.

Crediting my original source of code here here

This is the code so far:

This function is delivering the filter, parsed content to the active sheet.

function gather() {
    var messages = getGmail();
    var curSheet = SpreadsheetApp.getActive();
    messages.forEach(message => {curSheet.appendRow(parseEmail(message))});
}

Its somewhere in one of these functions I believe need to incorporate a loop for gathering all the messages within the thread

    function getGmail() {
        const query = "from:noreply-careers@smooth.ie ";
    
        let threads = GmailApp.search(query);
    
        //let label = GmailApp.getUserLabelByName("done");
        //if (!label) {label = GmailApp.createLabel("done")}
    
        let messages = [];
    
        threads.forEach(thread => {
            messages.push(thread.getMessages()[0].getPlainBody());
            
        });
    
        return messages;
    }

Specifically, I have been focusing on this to include a loop through the threads an messages:

    threads.forEach(thread => {
        messages.push(thread.getMessages()[0].getPlainBody());

I did try someting like this but i could not get it working.

function getGmails_(query) {
    var emails = [];
    var threads = GmailApp.search(query);
    for (var i in threads) {
        var msgs = threads[i].getMessages();
        for (var j in msgs) {
            emails.push([msgs[j].getPlainBody().replace(/\n*\s.+:/,',')
            .replace(/\n*.+:/g,',')
            .replace(/^,/,'')
            .replace(/\n/g,'')
            .split(',')
          ]);
        
        }
    } 
  
    return result;
}

The remaining code is running some regexp to extract the applicant answers from the message body and encapsulate in an index for delivery to the columns in sheets.

function parseEmail(message){
    var parsed = message.replace(/,/g,'')
        .replace(/\n*.+:/g,',')
        .replace(/^,/,'')
        .replace(/\n/g,'')
        .split(',');

    var result = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22].map(index => parsed[index]);

    return result;
    
}

If you just want to retrieve the plain body of all messages in all threads in a single array, you could do this:

  • For each message returned in GmailThread.getMessages() , use map to return an array with corresponding plain bodies for each of those messages.
  • Use push to add the current thread messages plain body to the main messages array.

Code snippet:

threads.forEach(thread => {
  const threadMessages = thread.getMessages().map(message=>message.getPlainBody());
  messages.push(...threadMessages);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM