How to check if a Gmail label has a nested sub-lab

2019-07-21 02:48发布

Using Google Apps Script, is there a function to check if a Gmail label has nested sub-labels? If a label has one or more sub-labels I want to exclude them from a code sequence.

1条回答
干净又极端
2楼-- · 2019-07-21 03:27

There is no direct method to get labels from a "parent" label but this is quite simple to get using the simple getUserLabels() method documented here and illustrated by the following small code :

function getAllLabels(){
  var results = [];
  var labels = GmailApp.getUserLabels();
  for (var i = 0; i < labels.length; i++) {
    Logger.log("label: " + labels[i].getName());
    results.push(labels[i].getName());
  }
  for (var i = 0; i < results.length; i++) {
    if(results[i].indexOf('/')>0){Logger.log(results[i]+' has a subLabel')};
  }
}

This will show a list of all your labels and from the second loop you'll get a list of all Labels that have at least one sub-Label.

Note that this script has to be improved because it will consider INBOX (for example but not only) as a label which is not really what you want but that part will be easy to manage with a few conditions.

查看更多
登录 后发表回答