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

爱⌒轻易说出口 提交于 2019-12-24 03:25:41

问题


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:


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.



来源:https://stackoverflow.com/questions/30012268/how-to-check-if-a-gmail-label-has-a-nested-sub-label

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