I have a google app script read email to google sheet , and it get all unread inbox email by var SEARCH_QUERY = "label:inbox is:unread to:me";
But I just want to get 10 newest message from email [email protected]
, how can i achieve that ?
I have tried var SEARCH_QUERY = "mailfrom:[email protected]"
but it does not work .
CodePudding user response:
The Ten Newest
function tennewest(){
const ts = GmailApp.search("label:inbox is:unread to:me");
let mA = [];
ts.forEach(t => {
t.getMessages().forEach(m => mA.push([{from:m.getFrom(),date:m.getDate(),subject:m.getSubject(),body:m.getBody()}]))
});
mA.sort((a,b) => {
let va = new Date(a.date).valueOf();
let vb = new Date(b.date).valueOf();
return va - vb;
})
return mA.slice(0,10);
}
CodePudding user response:
SUGGESTION
NOTE: Please share a sample script that you're working on in case there's anything else missing on this answer.
You can try this query:
is:unread from:(email_address)
Sample Script
function myFunction() {
// Find unread messages from [email protected]
var SEARCH_QUERY = 'is:unread from:([email protected])';
var threads = GmailApp.search(SEARCH_QUERY);
//Only filter the 10 most recent messages
var last10NewestMessages = threads.filter((_, index) => { return index <= 9 });
//E.g. log result to return the recent 10 messages' date just to confirm that they're the 10 most recent messages
console.log(last10NewestMessages.map(sampleGMessageDate => { return [sampleGMessageDate.getLastMessageDate()] }))
}