下面的代码是我目前用来导入csv文件到一个谷歌表,一切都是完美的,除了代码总是获取线程中最古老的电子邮件,我希望它总是选择最新的一个。
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)];