Web Gmail shows message as starred even after Apps Script unstars it

扶醉桌前 提交于 2019-12-17 17:14:16

问题


OK, I think web Gmail is being screwy. I run a Google apps script that in part adds my "To-do" label to any thread I manually star, then archives and unstars it. A snippet is below. I'd appreciate any help.

After running the script the thread gets the label and is unstarred, except the star icon next to the thread/message in web Gmail still shows as selected. If I go to Starred messages label/folder nothing is displayed. If I immediately rerun the script it doesn't find any Starred threads. This seems to indicate the script is working OK. The problem seems to be that web Gmail still wants to show it as Starred even though it isn't. Gmail Android app doesn't show the star applied to the thread. Pictures are worth more...

What my inbox looks like after the script runs. Note the star:

Yet no Starred messages:

function addStarred2ToDo() {
  var threads = GmailApp.search('is:starred');
  for (var h in threads) {
    var messages = threads[h].getMessages();
    for (var i in messages) {
      if (messages[i].isStarred()) {
        messages[i].unstar();
      }
    }
  }
}

EDIT: I also tried this and neither produce what is expected.

function thisOne() {
  var threads = GmailApp.search('is:starred');
  for (var h in threads) {
    var messages = threads[h].getMessages();
    for (var i in messages) {
      if (messages[i].isStarred()) {
        messages[i].unstar().refresh();
      }
    }
  }
}

function andThisOne() {
  var threads = GmailApp.search('is:starred');
  var toUnstar = [];
  threads.forEach(function (thread) {
    thread.getMessages().forEach(function (message) {
      if (message.isStarred()) {
        toUnstar.push(message);
      }
    });
  });
  GmailApp.unstarMessages(toUnstar);
}

回答1:


If you refresh gmail and hover over the star, you will see that the popup says it is Not Stared. Also, this seems to be an issue when you select the star from gmail, as stars that are set by my filters work correctly when my script unstars them.




回答2:


This display issue is caused because you do not force Gmail to update the message with a call to refresh() after your call to unstar().

Per documentation of GmailMessage#refresh():

Reloads this message and associated state from Gmail (useful in case the labels, read state, etc., have changed).

messages[i].unstar().refresh();

Should be sufficient to inform Gmail of the new starred status.

Alternately, a batch call to modify the messages will be more efficient in terms of quota usage:

var toUnstar = [];
threads.forEach(function (thread) {
  thread.getMessages().forEach(function (message) {
    if (message.isStarred()) {
      toUnstar.push(message);
      ...
    }
  });
});
GmailApp.unstarMessages(toUnstar);

In my sample I avoid the assumption that iterating an array is safe with for .. in.. and use the more expressive Array.forEach() to indicate that the code is something that we want to apply to every thread and every message in said thread.

Documentation on GmailApp.unstarMessages():

Removes stars from these messages and forces the messages to refresh.




回答3:


I'm having a similar problem. I have enabled a superstar, the green-check. I manually set them.

My script finds the relevant threads using "l:^ss_cg" in a search. It finds the starred email, sends a copy of the email somewhere, and then does the unstar.

Afterward, in the web gmail, if i search for the same message again, it shows up with a green star visually, but if I hover over the star icon, it shows 'not starred'.

However, if I run the script again, the thread is found using the search. It doesn't send another copy of the email, however, because I have a check for ".isStarred()" before it sends a copy of the specific email. I have also been able to reduce the number of threads it double-checks by adding a .hasStarredEmails() check to the thread before it starts looking at individual emails.

Doing a search in the web interface for has:green-check reveals only the emails it should.

There is something about .unStar() that doesn't work properly, I think.

I had considered trying to remove the star at the thread level by removing the ^ss_cg label, but that won't work because there is no way to get a GMailLabel object to send to the function.



来源:https://stackoverflow.com/questions/49540711/web-gmail-shows-message-as-starred-even-after-apps-script-unstars-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!