How to prevent grouping by subject or date when using getThreads() from a label name in Gmail using App-Script?

Viewed 96

When I am using GmailApp.getUserLabelByName(labelReceived).getThreads() it retrieves all the emails that have the same subject and were received on the same date in and outside the given label.
Is there a solution to get the emails from the only given label without grouping it by subject or a date?
Note: that the conversation view is off in the Gmail settings.

1 Answers

Issue:

  • Currently, It seems it is not possible to get the email messages labeled with a specific label. Only the containing threads labeled with a label is retrievable through Apps script built in libraries.

Solution:

Snippet:

function getLabelledMessages(labelName = 'test') {
  const labelIds = Gmail.Users.Labels.list('me').labels.find(
    labelObj => labelObj.name === labelName
  ).id;
  let messagesList, pageToken;
  do {
    messagesList = Gmail.Users.Messages.list('me', {
      labelIds,
      pageToken,
      maxResults: 100,
    });
    const messages = messagesList.messages;
    if (messages) console.log(JSON.stringify(messages));
  } while ((pageToken = messagesList.nextPageToken) !== undefined);
}
Related