Basic Gmail script not working but no error?

307 views
Skip to first unread message

Adam Krakowsky

unread,
Feb 21, 2023, 9:55:50 AM2/21/23
to Google Apps Script Community
Hi all - I have a very basic script that seems to run, but doesn’t seem to actually take effect and do what it’s supposed to do.  Wondering if the community can help me.  

The script is supposed to look at unread Gmail emails that are in the Promotions and Social folders, and if they’re older than 1 week (but less than 3 months, just to keep it to a less intensive search), then it marks the email as read.  It’s supposed to run 1x a week.  

However, it doesn’t even work on demand.  

Script is below.  Any advice?




function markSocialPromotionsAsRead() {
var threads = GmailApp.search('category:social OR category:promotions is:unread older_than:7d newer_than:90d');
var now = new Date();
var oneWeekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
var threeMonthsAgo = new Date(now.getTime() - 90 * 24 * 60 * 60 * 1000);
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var lastMessageDate = thread.getLastMessageDate();
if (lastMessageDate.getTime() >= oneWeekAgo.getTime() && lastMessageDate.getTime() < threeMonthsAgo.getTime()) {
thread.markRead();
}
}
}




Thank you!!!

Adam




Brett Grear

unread,
Feb 22, 2023, 3:48:09 PM2/22/23
to Google Apps Script Community
Your comparison statements are backwards.
Should be lastMessageDate.getTime() <= oneWeekAgo.getTime() && lastMessageDate.getTime() > threeMonthsAgo.getTime()

Adam Krakowsky

unread,
Feb 26, 2023, 3:52:33 AM2/26/23
to Google Apps Script Community
@Brett I really appreciate the reply!  I made that switch and the script just timed out.  

I don’t actually follow why you suggest that change, but thought I’d try it anyway.  I was looking to have mail older than 1 week (and less than 3 months, just to narrow the search) in promotion or social marked as read.  The way I read your suggested change makes it seem like the script would look for mail that is less than 1 week old and also greater than 3 month old, which is impossible. 

Thoughts?  

My original script runs with no error, but doesn’t do anything.  Can’t figure out why.  

Adam

Brett Grear

unread,
Feb 26, 2023, 5:30:01 AM2/26/23
to google-apps-sc...@googlegroups.com
Because time counts up so a message received more than a week ago will have a smaller unix time and a fixed point exactly one week ago.

What do you mean timed out. What was the script run time?

Put a console.log() message above your thread.markread() so you can see if it's finding anything.




--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/dNB-gYpgIBM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/bca15440-3f07-41ae-8e96-2babae284e3dn%40googlegroups.com.

Adam Krakowsky

unread,
Feb 26, 2023, 9:57:12 AM2/26/23
to Google Apps Script Community
99DCEE1D-0FE1-40AA-B9C0-410E8504C2E3.jpeg

Adam Krakowsky

unread,
Feb 26, 2023, 9:58:41 AM2/26/23
to Google Apps Script Community
@Brett Just posted a full screenshot of the error I get.  When I try to debug with your suggested edits, that times out too.   

Thoughts?

Adam


On Sunday, February 26, 2023 at 4:30:01 AM UTC-6 Brett Grear wrote:

Adam Krakowsky

unread,
Mar 1, 2023, 2:15:43 PM3/1/23
to Google Apps Script Community
@Brett any thoughts?  Would love to get this working!

Adam

Adam Krakowsky

unread,
Mar 1, 2023, 2:25:00 PM3/1/23
to Google Apps Script Community
Actually it may be working now, standby.   

Brett Grear

unread,
Mar 1, 2023, 5:22:29 PM3/1/23
to Google Apps Script Community
Add console.log(thread) above the line where it says thread.markRead().  This will at least give you some idea of if the statement on line 10 is returning true at all.  Scripts do have a limit on how long they can run for.  Maybe you've had too many messages in the last 3 months to do them all.
Reply all
Reply to author
Forward
0 new messages