简体   繁体   中英

How to capture multi-line output of a helper function?

I have written a small Javascript script for AdWords, which loops through several campaigns and adgroups of multiple accounts and then saves the results.

While the script works perfectly fine, I want to store the complete results once the looping is finished and send it off as an email, and I have a hard time making it work.

The code looks like this:

function main() {
MccApp.accounts().withIds([arr1,arr2,...,arrX])..executeInParallel('abc', 'afterProcessAllClientAccounts');
}

function abc() {
// Select campaigns, adgroups and loop through them...
// Now comes the somewhat critical part:

var n = 0;

while(aditer.hasNext()) {
  n++;
  aditer.next();
}

if(n < 4){

Logger.log('Campaign: ' + ag.getCampaign() + ' | AdGroup: ' + ag.getName() + ' | Ads: ' + n);

}
}

function afterProcessAllClientAccounts(results) {
for (var i = 0; i < results.length; i++) {
var result = results[i].getReturnValue();
Logger.log(result);
}
}

Now I would like to send an email with the multi-line Logger.log() output once the looping is done and I run into problems. AdWords Apps Script has a prebuilt mailing method:

MailApp.sendEmail(recipient, subject, body)

If I put it into the helper function, I get an email every time a loop is finished for one of the array elements, at the same time I did not find a valid way to put it in the main function as the outputing happens in the helper function.

Hence how can I aggregate the output and store it in a variable that is callable from within the main function?


EDIT: I have added the afterProcessAllClientAccounts function to the code. Calling it like this, the final Logger.log() output simply gives me the last specified result of each adGroup loop of the account . Hence for n rows of arr[n] , I get either a proper output or simply undefined . The problem is that the while adGroup loop seems to reset the logger completely. Another solution I tried today was pushing each log result in the if (n < 4) statement to an array as such:

var test = [];

function main() {
//foo
}

function abc(){
//foo
// while adGroup loop:

var n = 0;

while(aditer.hasNext()) {
  n++;
  aditer.next();
}

if(n < 4){

var c = 'Campaign: ' + ag.getCampaign() + ' | AdGroup: ' + ag.getName() + ' | Ads: ' + n;

test.push(c);
//or
test[test.length] = 'Campaign: ' + ag.getCampaign() + ' | AdGroup: ' + ag.getName() + ' | Ads: ' + n;
}

//back in the adGroup loop
Logger.log(test);
}
//end of function
}

Also in this case, the log only contained either an [] or the actual correct output per row if the condition was fulfilled, but it never truly appended the values to test , it rather replaced them, with the very final output being simply an empty array. It may be a very small detail I'm overlooking here and I sat on this with several other more experienced people, none of which found a solution.


EDIT 2: Adapted the code with concatenating all single results into an array. Output returns the correct number of rows (equal to the number of accounts), but the array is always empty.

function afterProcessAllClientAccounts(results) {
for (var i = 0; i <= results.length; i++) {
var result = [];
var s_result = results[i].getReturnValue();
result.concat(s_result);
Logger.log(result);
}
} 

As mentioned in the documentation , you can use the function executeInParallel(functionName, optionalCallbackFunctionName); where the parameter "functionName" is the function that will be excecuted for all the accounts, in your case the function is called 'abc'.

The other parameter "optionalCallbackFunctionName" is the name of the function that will be executed after all the accounts have been processed.

So you can return the same string you used in the Log 'Campaign: ' + ag.getCampaign() + ' | AdGroup: ' + ag.getName() + ' | Ads: ' + n 'Campaign: ' + ag.getCampaign() + ' | AdGroup: ' + ag.getName() + ' | Ads: ' + n

Then the callback function will receive this information as an array and then you can concatenate all the strings into one big string so you now can send just one email with the information of all the different accounts. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM