Reputation: 647
I have this script that pulls dates for unread messages in Gmail (the idea being that I want to get the date of the oldest unread email)
function someFunction () {
var oldSearchQuery = "in:Inbox is:Unread"
var oldThread = GmailApp.search(oldSearchQuery, 0, 500);
for (var inc = 0; inc < oldThread.length; inc++) {
oldThread.forEach(function(messages){
messages.getMessages().forEach(function(msg){
var date = msg.getDate()
Logger.log(date)
});
However, I want to sort the dates to get the oldest (or, if anyone knows of a better way, just get the oldest unread email). I tried to use sort and reverse:
Script as above but with ----
var date = msg.getDate()
date.sort();
date.reverse();
Logger.log(date)
});
But it doesn't like it as the values returned by getDate don't play nicely with sort. Anyone got any ideas?
Edit ----------------------------------------------------------
Updated to show current script and output:
for (var inc = 0; inc < oldThread.length; inc++) {
oldThread.forEach(function(messages) {
messages.getMessages()
.forEach(function(msg) {
var dates = msg.getDate()
var empty = []
empty.push(dates);
var sortedDateList = empty.sort(function(a,b){
return new Date(a) - new Date(b);
});
Logger.log(sortedDateList)
});
Outputs as:
[18-06-21 13:49:42:549 BST] [Wed Jun 20 02:08:24 GMT+01:00 2018]
[18-06-21 13:49:42:729 BST] [Tue Jun 19 16:15:19 GMT+01:00 2018]
[18-06-21 13:49:42:734 BST] [Wed Jun 20 02:08:24 GMT+01:00 2018]
[18-06-21 13:49:42:739 BST] [Tue Jun 19 16:15:19 GMT+01:00 2018]
I would be expecting to see:
[18-06-21 13:49:42:729 BST] [Tue Jun 19 16:15:19 GMT+01:00 2018]
[18-06-21 13:49:42:734 BST] [Wed Jun 20 02:08:24 GMT+01:00 2018]
[18-06-21 13:49:42:739 BST] [Tue Jun 19 16:15:19 GMT+01:00 2018]
[18-06-21 13:49:42:734 BST] [Wed Jun 20 02:08:24 GMT+01:00 2018]
Also, no idea why it is returning 2 lots of dates per mail, looks like its duplicating itself?
Upvotes: 1
Views: 2274
Reputation: 9872
This isn't meant to be a full solution to your question, but instead to demonstrate using Array
functions to get information from messages.
function getThreadStartDate_(thread) {
const messages = thread.getMessages();
var now = new Date();
var start = messages.reduce(function (earliest, msg) {
var msgStart = msg.getDate();
return (msgStart < earliest) ? msgStart : earliest;
}, now);
return start;
}
function getEarliestMatchingThread_(query) {
const threads = GmailApp.search(query);
// Sort descending, so we can call pop() to access the earliest one.
threads.sort(function (a, b) {
return getThreadStartDate_(b) - getThreadStartDate_(a);
});
return threads.pop();
}
Example use of the above:
function foo() {
const oldest = getEarliestMatchingThread_("your query here");
Logger.log(oldest.getFirstMessageSubject());
}
Note that there is an implicit assumption that the query you make is not unmanageably large (if it is, Gmail will throw an error). I also make the assumption that at least one thread matches the query. I recommend not relying on those assumptions :)
References:
Upvotes: 2
Reputation: 3410
You can get the list the dates for unread messages into an array then you can just run sort
method on it. For example,
I have an array of dates like this which is same as msg.getDate() as you have shown in the code. I have changed day, month, year and time to make sure it works for all.
var dateList = [ 'Tue Dec 18 22:14:59 GMT-11:00 2018',
'Wed Jul 18 22:14:59 GMT-11:00 2017',
'Tue Jun 12 02:14:59 GMT-11:00 2018',
'Thu Apr 12 21:14:59 GMT-11:00 2019',
'Thu Jun 14 22:14:00 GMT-11:00 2018',
'Fri Jan 12 22:14:59 GMT-11:00 2018' ]
Now you can apply sort
on this array,
var sortedDateList = dateList.sort(function(a,b){
return new Date(a) - new Date(b);
});
It will give this output:
[Wed Jul 18 22:14:59 GMT-11:00 2017,
Fri Jan 12 22:14:59 GMT-11:00 2018,
Tue Jun 12 02:14:59 GMT-11:00 2018,
Thu Jun 14 22:14:00 GMT-11:00 2018,
Tue Dec 18 22:14:59 GMT-11:00 2018,
Thu Apr 12 21:14:59 GMT-11:00 2019]
This output is in ascending order. Just swap a and b to get the result in descending order. You can see output by Logger.log(sortedDateList);
.
Upvotes: 2