ItGo.me Focus on IT Recommend

Home > Removing label from Gmail email after X days using Google Apps Script

Removing label from Gmail email after X days using Google Apps Script

2020腾讯云8月秒杀活动,优惠非常大!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1040

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

I created a Google Apps Script Code.gs as follows to remove the Gmail label from every thread that is older than X days and labeled Y.

function archiveYThreads() {
// Every thread, older than two days, and labeled "Unread Feeds".
var threads = GmailApp.search('label:"Unread Feeds" older_than:2d');
  for (var i = 0; i < threads.length; i++) {
    threads[i].removeLabel("Unread Feeds");
}
}

According to the documentation, the function removeLabel exists. Alternatively, I found some sources that use deleteLabel. However, with both I get the error that both functions do not exist, after having set a time-based trigger:

Can anybody please help me detecting why the function does not work?

Recommend:Send email using GMail API in Google Apps Script

chApp.fetch(RequestUrl,RequestArguments); What is wrong with my syntax answer 1 >>---Accepted---Accepted---Accepted--- I found the solution to my question: var RequestArguments = { headers: {Authorization: 'Bearer ' + token}, method: "post", contentType: "appl

google-apps-script gmail
|
  this question
asked Nov 5 '14 at 6:06 orschiro 2,937 8 27 56

 | 

1 Answers
1

---Accepted---Accepted---Accepted---

You have to supply an object of type GmailLabel as the argument to removeLabel() method. Try this snippet.

function archiveYThreads() {
 var label = GmailApp.getUserLabelByName("Unread Feeds");
 var threads = GmailApp.search('label:"Unread Feeds" older_than:2d');
  for (var i = 0; i < threads.length; i++) {
    threads[i].removeLabel(label);
  }
}

|
  this answer
answered Nov 5 '14 at 7:11 Amit Agarwal 2,757 1 9 16      It does work when supplying the GmailLabel object as an argument. Thank you for your help! –  orschiro Nov 9 '14 at 7:05 1   @orschiro Alternatively, you can get rid of the for loop by using the GmailLabel.removeFromThreads(threads) function once you have the threads array. In the code above simply replacing the for loop with label.removeFromThreads(threads) will work. It will work faster an reduces completely the possibility of your script timing out if you have a very large array of threads –  Chikitulfo Mar 21 '15 at 11:49      @Chikitulfo Thanks for your suggestion. Maybe Amit Agarwal can update his solution? –  orschiro Mar 21 '15 at 13:57

 | 

Recommend:gmail - Using "sent" label in a variable for Google Apps script

by running var labels = GmailApp.getUserLabelByName(name); However I could not find a way to put the "sent" label in a variable. How is this possible I want to remove the sent label from the message automatically after a certain number of

oriUlr:http://stackoverflow.com/questions/26750726/removing-label-from-gmail-email-after-x-days-using-google-apps-script

------splitte line----------------------------