以下 Apps 脚本代码获取最旧的电子邮件线程(来自 gmail),我怎样才能让它获取最新的电子邮件?

The Following Apps Script code gets the oldest email the thread (from gmail), how can I make it get the latest one?

下面的代码是我目前用来将 csv 文件导入 google sheet 的代码,一切工作正常,除了代码总是获取线程中最旧的电子邮件,我想要它总是 select 最新的。

function importCSVFromGmail() {

  var threads = GmailApp.search('subject:(Daily Scheduled Report Recruiting Metrics) AND newer_than:3h'); // enter search criteria here
  var message = threads[0].getMessages()[0];
  var attachment = message.getAttachments()[0];


  var sheet = SpreadsheetApp.openById('1mflZmtgChZOFGVdlnyNrlDmVOxKEw6Hk0RJzilTvnMo').getSheetByName('Sheet1');
  var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");

   sheet.clearContents().clearFormats(); // clears target sheet
   sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData); // adds data to the sheet
}

尝试用

替换var message = threads[0].getMessages()[0];
var message = threads[0].getMessages()[Number(threads[0].getMessageCount()-1)];