如何获取线程中的最后一条消息?

时间:2019-08-21 14:39:17

标签: google-apps-script gmail-api spreadsheet

我在Google Apps脚本中使用以下代码,我想在每个邮件列表中得到最后的答复。该代码使每个线程的第一个邮件而不是最后一个邮件。

 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 < 7) 
 {

   var threads = GmailApp.getInboxThreads(start , max);
   var messages = GmailApp.getMessagesForThreads(threads); 
   var froms = [];
    messages.get
      for(var i = 0; i < 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++;
    }
}```

1 个答案:

答案 0 :(得分:1)

此脚本中有很多事情可能需要更改。

因此,以下各行仅限于获取每个线程的最后一条消息。然后将最后一条消息的详细信息推入数组from[]

  for (var i = 0; i < threads.length; i++) { // Loop through the threads

    var thisThread = threads[i]; // Get a speific thread

    var messages = thisThread.getMessages(); // Get the messages in that thread

    var messageCount = thisThread.getMessageCount(); // Count the number of messages in that thread

    var lastMessage = messages[messageCount - 1]; // Get the last message in that thread. The first message is numbered 0.

    froms.push([lastMessage.getId(), lastMessage.getSubject(), lastMessage.getTo(), lastMessage.getFrom()...]);

  }