Google Script - Get Data From Gmail Into Sheet
I have been looking around here on SO and on Google, but I cannot find anything that is working. So when I am running my code below, I am getting the result on the image. I wan
Solution 1:
So I managed to solve it, by finding the max index in the array. I have commented the code, so it can help others. Thanks, all.
function myFunction() {
// Use sheetvarss= SpreadsheetApp.getActiveSheet();
// Gmail queryvarquery="label:support -label:trash -label:support-done -from:me";
// Search in Gmail, bind to arrayvarthreads= GmailApp.search(query);
// Loop through query resultsfor (vari=0; i < threads.length; i++)
{
// Get messages in thread, add to arrayvarmessages= threads[i].getMessages();
// Used to find max index in arrayvarmax= messages[0];
varmaxIndex=0;
// Loop through array to find maxIndexD = most recent mailfor (varj=0; j < messages.length; j++) {
if (messages[j] > max) {
maxIndex = j;
max = messages[j];
}
}
// Find datavarmId= messages[maxIndex].getId() // ID used to create mail linkvarfrom= messages[maxIndex].getFrom();
varcc= messages[maxIndex].getCc();
vartime= threads[i].getLastMessageDate()
varsub= messages[maxIndex].getSubject();
// Write data to sheet
ss.appendRow([from, cc, time, sub, 'https://mail.google.com/mail/u/0/#inbox/'+mId])
}
}
Solution 2:
What do you mean by recent?
How about adding newer_than:3d to the search? You can test the search in your own Gmail account and when it's doing what you want then just paste it into the query.
Post a Comment for "Google Script - Get Data From Gmail Into Sheet"