0
votes

I copied this function from somewhere on the web. My goal is to import the body of specifically labeled emails in my gmail account to a google spreadsheet. While I'm not completely inept when it comes to coding, I'm not familiar with this stuff.

Some details that may be relevant: Each of these Emails I am trying to import are NOT conversations, they are a single email received, no response and no earlier messages in the thread. I want the entire body of each Email placed in a single cell in the spreadsheet.

As it is, only the subject is being placed in my spreadsheet. How can I get it to bring the body, as well? I feel like it's being placed in the array, but when looping the setValue it gets skipped.

Much love, folks!

Code:

function getMessagesWithLabel() {
 var destArray = new Array();
  var threads = GmailApp.getUserLabelByName('abc').getThreads(1,10);

  for(var n in threads){
        var msg = threads[n].getMessages();
        var destArrayRow = new Array();
        destArrayRow.push('thread has '+threads[n].getMessageCount()+' messages');
          for(var m in msg){
                     destArrayRow.push(msg[m].getSubject());
           }
  destArray.push(destArrayRow);           
        }
Logger.log(destArray);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
if(ss.getLastRow()==0){sh.getRange(1,1).setValue('getMessagesWithLabel() RESULTS')};
sh.getRange(ss.getLastRow()+1,1,destArray.length,destArray[0].length).setValues(destArray)
}
1
I suggest you either edit your question to make it clear what is your actual problem or close it and create a new one with the actual problem you're facing. - Jonathan Drapeau
done. Hopefully this will help me find an answer. - Grouchy

1 Answers

0
votes

You're not employing .getBody() anywhere in your script? In a case like this I'd store each message as an object with subject and body as separate values:

var msg = threads[n].getMessages();
var contentArray = [];

for(var i = 0; i < msg.length; i++){

  var obj = {
    subject: msg[i].getSubject(),
    body: msg[i].getBody()       
  };

  contentArray.push(obj);

}

These values can then be iterated through using the following notation:

console.log(contentArray[0].subject);
console.log(contentArray[0].body);