如何优化以下脚本?由于可能存在多个循环,它正在超时。循环按顺序排列:
1-循环访问所有 Google Ads 帐户。
2-循环遍历所有广告组。
3 - 在每个广告组中,检查是否有任何广告处于“已拒绝”状态。如果是,请将数据附加到 Google 表格。
function main() {
var sheetId = 'XXX';
var sheetName = 'XXX';
var spreadsheet = SpreadsheetApp.openById(sheetId);
var sheet = spreadsheet.getSheetByName(sheetName);
if (!sheet) {
sheet = spreadsheet.insertSheet(sheetName);
} else {
// Clear the existing contents
sheet.clearContents();
}
// Adding headers (column names)
var headers = ['Account Name', 'Campaign', 'Ad Group'];
sheet.appendRow(headers);
// Iterating through each client account
var accountIterator = AdsManagerApp.accounts().get();
while (accountIterator.hasNext()) {
var account = accountIterator.next();
AdsManagerApp.select(account);
var adGroupIterator = AdsApp.adGroups()
.withCondition('CampaignStatus = ENABLED')
.withCondition('AdGroupStatus = ENABLED')
.get();
while (adGroupIterator.hasNext()) {
var adGroup = adGroupIterator.next();
// Check if the ad group has any disapproved ads
var disapprovedAdFound = false;
var adIterator = adGroup.ads().get();
while (adIterator.hasNext()) {
var ad = adIterator.next();
if (ad.getPolicyApprovalStatus() === 'DISAPPROVED') {
disapprovedAdFound = true;
break;
}
}
if (disapprovedAdFound) { // Disapproved ads found
// Record the details of the ad group with disapproved ads
sheet.appendRow([
account.getName(),
adGroup.getCampaign().getName(),
adGroup.getName()
]);
}
}
}
}