title: How can I get the last message in the thread?
body: <p>i use the following code in Google Apps Script and i wanna get last reply every mail list. This code gets every threads first mail instead of last mail.</p>
  
  <pre><code> var myspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
   var mysheet = myspreadsheet.getSheets()[0];
   var start = 0;
   var max = 399;
   var count =0;
   var row = mysheet.getLastRow()+1
   while(count &lt; 7) 
   {
  
     var threads = GmailApp.getInboxThreads(start , max);
     var messages = GmailApp.getMessagesForThreads(threads); 
     var froms = [];
      messages.get
        for(var i = 0; i &lt; threads.length; i++)
      {
         froms.push([messages[i][0].getId(),messages[i][0].getSubject(),messages[i][0].getTo(),messages[i][0].getFrom(),messages[i][0].getCc(),messages[i][0].getDate(),messages[i][0].getReplyTo()]);
      }
  
      mysheet.getRange(mysheet.getLastRow()+1,1,threads.length,7).setValues(froms);
  
      start =  start + 100;
      count++;
      }
  }```
  </code></pre>