Home > Net >  Gmail & Google App Scripts - Apply label based on star colors
Gmail & Google App Scripts - Apply label based on star colors

Time:06-22

Currently I am just using the search bar has:red-bang but I would prefer to apply labels to has:red-bang and has:yellow-bang so I can see a count to see how many unread emails are found in that label.

When I use Google's filter options, and I set the rule to has:red-bang, it does not work.

Is is possible to achieve this using Google App Scripts instead?

Logic:

  • When email has:red-bang apply label "High Priority", mark as unread.
  • When email has:yellow-bang apply label "Low Priority", mark as unread.

This way I can open my email each day, and click on High Priority to respond to all those emails first. Then if I have time, then I will click on Low Priority.

CodePudding user response:

UPDATE:

I played around this and was able to find through the Developer Console the following information:

Gmail Superstar labels are formatted in the following:

  • l:^ss_cr = has:red-bang
  • l:^ss_cy = has:yellow-bang
  • l:^ss_cb = has:blue-info

So with the following code sample, I was able to use GmailApp.search() with the following superstar label queries above. This does seem to be undocumented as of the moment.

Here is the code:

    function myFunction() {

  //Gets the label names. Make sure this is already created on Gmail. 
  var rlabel = GmailApp.getUserLabelByName('High Priority');
  var ylabel = GmailApp.getUserLabelByName('Less Priority');

  //Applies the label 'High Priority' whenever messages are found with red-bang
  var redbang = GmailApp.search('l:^ss_cr');
  redbang.forEach(x => x.addLabel(rlabel).markUnread());
  //Applies the label 'Less Priority' whenever messages are found with yellow-bang
  var yellowstar = GmailApp.search('l:^ss_cy');
  yellowstar.forEach(y => y.addLabel(ylabel).markUnread());

  Logger.log(redbang);
  Logger.log(yellowstar);

}
  • Related