Come back to the beggining of an iterator

122 views
Skip to first unread message

Alberto Esteves Correia

unread,
Feb 2, 2020, 6:33:32 PM2/2/20
to Google Ads Scripts Forum
Hello,

I have something like this (it's only an example code):


var iterator=AdsApp.display().placements().forDateRange("YESTERDAY").get();

while(iterator.hasNext()){
 
var placement = iterator.next();
}

// At this point, we are at the end of the iterator
// How can I comeback to the beginning of the iterator and run another the next while


while(iterator.hasNext()){
 
var placement = iterator.next();
}
Enter code here...

It's like comeback at the position 0 of an array, but with the iterator. 

Any idea? 

Thanks!

Google Ads Scripts Forum Advisor

unread,
Feb 3, 2020, 1:10:29 AM2/3/20
to adwords...@googlegroups.com
Hi Alberto,

Thanks for posting your concern.

I can see that you have implemented another placement iterator in your code and I can confirm that this is the correct way to return back to the beginning of the iterator and run another loop.

With this, could you confirm if you've encountered any issues with the current implementation? If so, to further investigate, could you provide the details about the issue and provide the customer ID and the name of the script? You could provide the requested information via Reply privately to author option.

Regards,
Ejay
Google Ads Scripts Team

ref:_00D1U1174p._5001UUzvaK:ref

Alberto Esteves Correia

unread,
Feb 5, 2020, 11:58:40 AM2/5/20
to Google Ads Scripts Forum
The iterator is the same in both loops, isn't it? 

While I try to solve this point, in the real code (not this example) I've creadted an array of placements, instead of an iterator. 

Google Ads Scripts Forum Advisor

unread,
Feb 5, 2020, 3:12:27 PM2/5/20
to adwords-scripts+apn2wqdwngphxzdr...@googlegroups.com, adwords-scripts+apn2wqdwngphxzdr...@googlegroups.co, adwords...@googlegroups.com
Hi Alberto,

I can confirm that the way to 'restart' the iterator, is simply to re-declare it, as Ejay asserted above.

Thanks,
Matt

Alberto Esteves Correia

unread,
Feb 22, 2020, 6:12:46 AM2/22/20
to Google Ads Scripts Forum
Hi Matt,

Sorry for my insistence, but I just didn't find the solution. If I give you this code: 


function main(){

var campaignIterator = AdsApp.campaigns.get();

 
while (campaignIterator.hasNext()){
   
var campaign = campaignIterator.next();
   
Logger.log(campaign.getName());
 
}

 
// What must I do to restart the iterator here?

  while (campaignIterator.hasNext()){
   
var campaign = campaignIterator.next();
   
Logger.log(campaign.getName());
 
}
}




Thank you in advance! 

Google Ads Scripts Forum Advisor

unread,
Feb 24, 2020, 2:09:35 AM2/24/20
to adwords-scripts+apn2wqdwngphxzdr...@googlegroups.com, adwords-scripts+apn2wqdwngphxzdr...@googlegroups.co, adwords...@googlegroups.com
Hi Alberto,

I work with Matt and Ejay and allow me to assist you. Regarding your concern, you can declare a new iterator as you may see in the below sample code :

function main(){

var campaignIterator = AdsApp.campaigns().get();


  while (campaignIterator.hasNext()){
    var campaign = campaignIterator.next();
    Logger.log(campaign.getName());
  }

  // What must I do to restart the iterator here?
  Logger.log("restart iterator");
  var campaignIterator = AdsApp.campaigns().get();


  while (campaignIterator.hasNext()){
    var campaign = campaignIterator.next();
    Logger.log(campaign.getName());
  }
}

Doing so should "restart" the iterator. But what it actually does is it creates a new instance of the iterator and starts again from the 0 index. Let me know if this is what you're looking for.

Thanks,
Peter

Alberto Esteves Correia

unread,
Feb 24, 2020, 5:44:54 PM2/24/20
to Google Ads Scripts Forum
Thank you very much for your response. 

I wanted to do that but without doubling the processing time. In very large iterators, this can be a problem and exceed the maximum of 30 minutes (or 60min with pararell execution)

It's like going back to the 0 position of an array, and not creating a new one. 


Thanks!
Message has been deleted

Yoann Berthome

unread,
Feb 24, 2020, 9:35:07 PM2/24/20
to Google Ads Scripts Forum
function main(){

var campaignIterator = AdsApp.campaigns.get();
var campaigns = [];

while (campaignIterator.hasNext()){
var campaign = campaignIterator.next();
    campaigns.push(campaign)

Logger.log(campaign.getName());
}

// What must I do to restart the iterator here?

  for(var i=0;i<campaigns.length;i++)
var campaign = campaigns[i];
Logger.log(campaign.getName());
}
}

store the previous values of each next in an Array
actually you answered your own question already without noticing ?

Google Ads Scripts Forum Advisor

unread,
Feb 25, 2020, 2:39:55 PM2/25/20
to adwords-scripts+apn2wqczxyxhcjoq...@googlegroups.com, adwords...@googlegroups.com
Hi Alberto,

Defining an iterator will not take a significant amount of time. For large iterators, iterating over the entities in the iterator is what will take the vast majority of time. 

At the moment, there is no method that will reset the iterator back to the first member in the list. 

Thanks,
Matt 

Alberto Esteves Correia

unread,
Feb 25, 2020, 4:41:38 PM2/25/20
to Google Ads Scripts Forum
It is understood! 

Thanks to all of you for your responses! 

Yoann Berthome

unread,
Feb 25, 2020, 8:08:32 PM2/25/20
to Google Ads Scripts Forum
Curiosity

if we need to optimize in such a way, what method would be the best?

to while .... next()

or store in an array and for each it ?

Google Ads Scripts Forum Advisor

unread,
Feb 25, 2020, 9:26:57 PM2/25/20
to adwords-scripts+apn2wqczxyxhcjoq...@googlegroups.com, adwords...@googlegroups.com
Hi Yoann,

As Matt pointed out, large iterators may take some time to process.

Using an array, however, may allow users to more flexibly manage members through the use of indexes.

Thanks,
Peter
Reply all
Reply to author
Forward
0 new messages