0

I'm trying to unstar a message in my Apps Script project, and it's not unstarring. What am I missing?

My code:

var label = GmailApp.getUserLabelByName("Newly Received Test Request");
var threads = label.getThreads();
GmailApp.refreshThreads(threads);
for (var i in threads) {
  GmailApp.refreshThread(threads[i]);
  var messages = threads[i].getMessages();
  GmailApp.refreshMessages(messages);
  for (var j in messages) {
    GmailApp.refreshMessage(messages[j]);
    if (messages[j].isStarred()) {
      messages[j].unstar();
      Logger.log(messages[i].getPlainBody());
      GmailApp.refreshMessage(messages[j]);
    }
  }
  GmailApp.refreshThread(threads[i]);
  //threads[i].removeLabel(label);
}
GmailApp.refreshThreads(threads);
return;

I went a little overboard with the refreshing, just in case. Still, when I run this, the starred messages in this label stay starred.

Edit: FWIW, the lag time between Apps Script unstarring a message and the Gmail UI reflecting that change seems to be getting faster over the last couple of days.

xd1936
  • 1,038
  • 2
  • 9
  • 27
  • What does your execution transcript and log say after running? Also, on line 9, you call `refreshMessage(messages[i])` (the thread) instead of calling the current message with iterator `[j]`. – Brian Aug 24 '18 at 20:06
  • Ooo, nice catch. Execution transcript inexplicably says `[18-08-24 13:08:57:927 PDT] GmailMessage.unstar() [0.161 seconds]` runs the first time, then doesn't run the second time... so I don't enter the `if... isStarred()` in the code. BUT, the message is still starred in the Gmail UI. – xd1936 Aug 24 '18 at 20:10
  • Why not remove the refreshes to see if that's the problem? Most of those methods refresh the thread on their own. – Brian Aug 24 '18 at 20:17
  • I should have mentioned that I added the refreshes during troubleshooting. I have also tried it without any of them, no luck. Other SO posts mentioned that refreshes were mandatory so I thought I'd give it a try `¯\_(ツ)_/¯`. – xd1936 Aug 24 '18 at 20:34
  • According to the docs, `GmailMessages` is an array, so you should use an indexed loop rather than a for...in loop. Loop `messages` with a declared variable: `for(var j=0; j – Brian Aug 24 '18 at 20:39
  • Thanks for the idea! I was under the impression that a `for... each` loop would be functionally identical to a `for... in` loop in this case, since an Array is an object in JS? Anyway, I switched to `var i = 0; i < messages.length; i++` in this case, and no luck :( – xd1936 Aug 24 '18 at 21:02
  • From the linked thread: OP has filed a bug report on the Issue Tracker: https://issuetracker.google.com/issues/77320923 – tehhowch Aug 24 '18 at 22:45
  • Is this information useful for your situation? At old gmail, the star mark is not removed, even when ``unstar()`` is run while it confirms that ``isStarred()`` is false. But at new gmail which was updated recently, the star mark is removed, when ``unstar()`` is run. If you are required to use the old gmail, I also think that it's a bug. If you can use new gmail, the bug has been fixed. – Tanaike Aug 25 '18 at 07:03
  • Interesting. I'm using the new Gmail, but it looks like this bug is exactly the bug that I'm running into. – xd1936 Aug 25 '18 at 16:35
  • After I run this code (or the `Array.forEach` code in the link above), the starred emails no longer show up in `is:starred` searches on the web or `.isStarred()` code... But, they're still starred in the Gmail UI when I find them in lists of messages. So strange. – xd1936 Aug 25 '18 at 16:45
  • I'm really sorry for the inconvenience. In my environment, I confirmed that when I used new Gmail, ``unstar()`` worked. But when I used the old one, it didn't work. So I have been using new one after new Gmail was released. But in your environment, the bug might not be removed. If it's so, I think that there is a value to report this bug. – Tanaike Aug 26 '18 at 00:41

0 Answers0