Get Gmail Categories

I used to have working script applications that deleted old sales letters in my Gmail account.

He used the code

var label = GmailApp.getUserLabelByName("Promotions");

to get the label, and then it repeats through label.getThreads()to decide if each thread was old enough to be deleted.

However, now Google has changed Gmail, therefore it is automatically classified in the section Categoriesin the user interface, and not in the list of labels, so the above text now returns null.

How can I fix my code to get a category Promotions? I tried too Categories/Promotions, but it also appears null.

+4
source share
2

Gmail .

, . , Label , script ; -)

  var threads = GmailApp.search('category:promotions');// check the category Gmail added to the thread

+6

script , .

function auto_delete_email(){
  delete_Label ("Cameras",30);
  delete_Label ("Travel",365);
  delete_Category ("Social",90);
  delete_Category ("Finance",365*3);
  delete_Category ("Forums",90);
  delete_Category ("Promos",365*3);
}

function delete_Label(mailLabel,delayDays) {  
  var label = GmailApp.getUserLabelByName(mailLabel);   
  if (!label) {return false;}
  var maxDate = new Date(); 
  maxDate.setDate(maxDate.getDate()-delayDays);    
  var threads = label.getThreads();  
  for (var i = 0; i < threads.length; i++) {  
    if (threads[i].getLastMessageDate()<maxDate){  
      threads[i].moveToTrash();
    } 
  } 

function delete_Category(mailCategory,delayDays) {  
  var maxDate = new Date(); 
  maxDate.setDate(maxDate.getDate()-delayDays);    
  var threads = GmailApp.search('category:' + mailCategory);
  for (var i = 0; i < threads.length; i++) {  
    if (threads[i].getLastMessageDate()<maxDate){  
      threads[i].moveToTrash();
    } 
  } 
} 
0

Source: https://habr.com/ru/post/1524919/


All Articles