Google Apps Script GmailApp - Label threads includ

2019-07-20 18:21发布

I am using the following to retrieve the threads and messages for my label:

var threads = GmailApp.search("label:labelName to:forwarded@email.addr");    
//Same result returned from using
//var threads = GmailApp.getUserLabelByName(labelName).getThreads();

for (var i=0; i<threads.length; i++){    
  var messages = GmailApp.getMessagesForThread(threads[i]);
  for (var j=0; j<messages.length;j++){
    var msg=messages[j].getPlainBody();
    ...
  }
}

I am successfully retrieving data. However, there are isolated incidents where messages sent to an address that is not the one specified in the search and are not associated with the label are clumped into the thread. I can confirm this with "conversation view" in the email account itself.

I am attempting to retrieve JUST the messages that match my search criteria, not the ones that are a member of the thread but not a member of the label. How can I do this reliably?

UPDATE:

So after extensive research, I've concluded this currently isn't possible with the scripts API. However, scripts can now include the full GMail API by enabling it as a resource and then enabling GMail api in your Google Developer center. With the full GMail API the below code can be used to compare message labels, but this is not otherwise possible with the Scripts API. I have filed a feature request here.

Inline with the above code, the below will serve to verify message labels:

var abort=true;
var labelID="permenant_label_id"; //NOT label name.
//Compare message label
var compare=Gmail.Users.Messages.get('me', messages[j].getID());
for (var l=0; l<compare.labelIds.length;l++) {
  //if abort==false then message verified against label.
  abort = (compare.labelIds[l]!=labelID)?true:false;
  if (!abort) break; //stop sorting labels
}   

3条回答
男人必须洒脱
2楼-- · 2019-07-20 18:47

You could use the getTo() function when iterating the messages in a thread. Then check the result of getTo() to be the value you are searching for.

Hope I understood your question and goal correctly.

查看更多
霸刀☆藐视天下
3楼-- · 2019-07-20 18:49

You can try the given methods in Class GmailLabel, such as removeFromThread(thread) or removeFromThreads(threads) wherein these remove label from the given threads and forces the threads to refresh.

More information can also be found in - Create time-based Gmail filters with Google Apps Script

查看更多
够拽才男人
4楼-- · 2019-07-20 19:10

So after extensive research, I've concluded this currently isn't possible with the scripts API. However, scripts can now include the full GMail API by enabling it as a resource and then enabling GMail api in your Google Developer center. With the full GMail API the below code can be used to compare message labels, but this is not otherwise possible with the Scripts API.

Inline with the above code, the below will serve to verify message labels:

var abort=true;
var labelID="permenant_label_id"; //NOT label name.
//Compare message label
var compare=Gmail.Users.Messages.get('me', messages[j].getID());
for (var l=0; l<compare.labelIds.length;l++) {
  //if abort==false then message verified against label.
  abort = (compare.labelIds[l]!=labelID)?true:false;
  if (!abort) break; //stop sorting labels
}   
查看更多
登录 后发表回答