I want to return a list of all email subjects received at a specified day, at my Gmail account. If there is more than one message with the same subject, I want to have a number of how many there are. This is what I did:
var emailThreads = GmailApp.search("subject:<partial_subject> after:2020/8/30 before:2020/8/31");
var messages = GmailApp.getMessagesForThreads(emailThreads);
This returns "an array of arrays of messages, where each item in the outer array corresponds to a thread and the inner array contains the messages in that thread".
I try to access data I need like this:
for (i = 0; i < messages.length; i++) {
messages[i][0].getSubject() // a subject
messages[i].length // number of messages with the same subject
}
However, this returns more messages than is displayed in Gmail UI.
Here's what I've noticed:
- it only happens to threads with more than one message
- it doesn't happen to all threads with more than one message
- every time a run my script for the same day, the result is the same
- when I run my script for different days, the result is different even though the same emails were received
Considering that this code is very simple and I'm only using methods delivered by google, I assume I must have misunderstood something or Gmail is doing something weird with messages in threads.
Any ideas what could this be and how to work around it?
Edit: I've noticed that there is a "Don't include chats" checkbox in gmail filter, which adds "-in:chats" into search query, but it didn't help either. I also tried adding "in:inbox" to the query, but with no change in the result.