In Google Script it is recommended to store operations in an array, then calling the methods once all the operations has been constructed, to minimize response time every time a service is called.
For example in AdWords:
Script A
var adGroupBuilder = campaign.newAdGroupBuilder();
for (i = 0; i < 1000; i++) {
var adGroupOperation = adGroupBuilder
.withName(i.toString())
.build();
}
var adGroups = adGroupOperation.getResult(); // method call
Executes in less than a second.
Script B
var adGroupBuilder = campaign.newAdGroupBuilder();
for (i = 0; i < 1000; i++) {
var adGroupOperation = adGroupBuilder
.withName(i.toString())
.build();
var adGroups = adGroupOperation.getResult(); // method call
}
Executes in almost 6 minutes.
In AdWords, an AdGroup
can have several Ads
, and in order to create an Ad
there must be a corresponding AdGroup
.
I have an external API which returns about 15 000 entities in batches of 1000 each, for which I am developing a Google Script that convert these into AdGroups
with corresponding Ads
. Obviously, to make that many individual calls to their services results in a timeout error. So I would like to break those down into 15 calls, with an array of 1000 entities each.
Now, I would like to know if there is possible to add Ads
into the array of operations that is called by the method getResults()
, or if the AdGroup
object needs to be instantiated before I can create an Ad
for it?
Because if not I see no other option than to make an individual call to the AdWords services for every entity that I want to turn into an Ad
, but then the response time would be very high, and a different structure is required.