Remove multiple labels - need help to customize a script

56 views
Skip to first unread message

Tamara Strelnykova

unread,
May 5, 2019, 5:39:57 AM5/5/19
to Google Apps Script Community
Hi everyone!

I really need your help to customize a script.
I've been using a poplular script to remove 1 label for the optimization process and it works perfectly. The problem is now I have 12 labels and I want one script to remove all of them. 
My labels are ['ROI -100', 'ROI -20-0', 'ROI -75-50', 'ROI -99-75', 'ROI 0-50', 'ROI 200-300', 'ROI 300-400', 'ROI 400-600', 'ROI 600-800', 'ROI 800-1000', 'ROI-50-20', 'ROI 0-50'].

The perfect script would remove all labels that contain word 'ROI'.

The script I'm using:

function main() {
    
    var DEL_LABEL = 'ROI 0-50';
    
    var keywordIterator = AdsApp.keywords()
    .withCondition("LabelNames CONTAINS_ANY ['ROI 0-50']")
    .get();
    while (keywordIterator.hasNext()){
      var keyword = keywordIterator.next()
      var labelIterator = keyword.labels().get()
      while (labelIterator.hasNext()){
        var label = labelIterator.next()
        var labelname = label.getName()
        Logger.log("labelname: "+labelname)
        if (labelname = DEL_LABEL) {
          keyword.removeLabel(DEL_LABEL)
        }
      }
    }
    
     }


I would be grateful for any suggestions!

cwlind

unread,
May 6, 2019, 12:37:18 PM5/6/19
to Google Apps Script Community
Would something like this work?
function main() {
   
   
var DEL_LABEL = 'ROI';  //changed from ROI 0-50
   
   
var keywordIterator = AdsApp.keywords()
   
.withCondition("LabelNames CONTAINS_ANY ['ROI']") //changed from ROI 0-50
   
.get();

   
while (keywordIterator.hasNext()){
     
var keyword = keywordIterator.next()
     
var labelIterator = keyword.labels().get()
     
while (labelIterator.hasNext()){
       
var label = labelIterator.next()
       
var labelname = label.getName()
       
Logger.log("labelname: "+labelname)

       
if (labelname.startsWith(DEL_LABEL)) {  //if the label starts with ROI, remove it
          keyword
.removeLabel(labelname)
       
}
     
}
   
}
   
     
}
The above will *should* remove all labels starting with 'ROI'. I say *should* because I have not tested this.

Tamara Strelnykova

unread,
May 7, 2019, 5:30:52 AM5/7/19
to google-apps-sc...@googlegroups.com
Thanks for your suggestion! I've tried it, but unfortunately it runs with no changes :(

cwlind

unread,
May 7, 2019, 7:50:16 AM5/7/19
to google-apps-sc...@googlegroups.com
Ok, I wasn't sure. What if iterate over each Label instead? Something like:

function main() {

    var DEL_LABEL = ['ROI -100', 'ROI -20-0', 'ROI -75-50', 'ROI -99-75', 'ROI 0-50', 'ROI 200-300', 'ROI 300-400', 'ROI 400-600', 'ROI 600-800', 'ROI 800-1000', 'ROI-50-20', 'ROI 0-50'];

    for (var i = 0; i < DEL_LABEL.length; i++) {

        var keywordIterator = AdsApp.keywords()
            .withCondition("LabelNames CONTAINS_ANY ['" + DEL_LABEL[i] + "']")

            .get();
        while (keywordIterator.hasNext()) {
            var keyword = keywordIterator.next();
            var labelIterator = keyword.labels().get();
            while (labelIterator.hasNext()) {
                var label = labelIterator.next();
                var labelname = label.getName();
                Logger.log("labelname: " + labelname);
                if (labelname == DEL_LABEL[i]) { //remember to use 2 == to check equality
                    keyword.removeLabel(DEL_LABEL[i]);
                }
            }
        }
    }
}


Tamara Strelnykova

unread,
May 7, 2019, 8:14:28 AM5/7/19
to Google Apps Script Community
It worked! Thank you so much for your help!
Reply all
Reply to author
Forward
0 new messages