超过Google Adwords的最长执行时间

这是以下问题的延伸。 我无法使用它,因为Adwords中不支持PropertiesServiceScriptApp ,并且在适应AdWords的问题上我没有发现任何相关的内容。

我有一个Adwords脚本不断得到Exceeded maximum execution time的错误。 对于MCC帐户,我认为最长执行时间为30分钟。 有谁知道是否有办法延长这个时间限制? 或者,有没有办法再次调用AdWords脚本,并从停止的地方继续? 排队? 我可以在Google-Apps-Script中使用MCCScript吗?

这是我迄今为止所做的...

function timeDrivenTrigger(myFunct) {
    var REASONABLE_TIME_TO_WAIT = 4*6000;
    var MAX_RUNNING_TIME = 1*6000;

    var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1sYQ__CM33oL2OLzbScQEdcQ6LvDxJCohP024vdSGfSI/edit#gid=0');
    var sheet = ss.getSheets()[0];
    var cell = sheet.getRange('A1');

    var startTime= (new Date()).getTime();

    myFunct;

    var startRow= cell.getValue() || 1;
    for(var ii = startRow; ii <= 10000; ii++) {
        var currTime = (new Date()).getTime();
        if(currTime - startTime >= MAX_RUNNING_TIME) {
            cell.setValue(ii);
            ScriptApp.newTrigger("runMe")
                .timeBased()
                .at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
                .create();
            break;
        } 
    }
}

与并行执行更新

这是我的整个代码...

//Your build Google-Spreadsheet
var SPREADSHEET_URL = "https://docs.google.com/spreadsheets/d/1k4o_8O_11OvhZRergefWKgXQ8_XxIs7D31-NV9Ove-o/edit#gid=749396300";

//Fetch and convert data in a JSON structure 
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = spreadsheet.getSheetByName('Campaigns');
var data = sheet.getRange("A:C").getValues();
var accountList = accountsListing(data);

accountList= accountList.map(function (el) {
    return el.trim();
});

function main() {

    accountIdList = [];
    accountItr = MccApp.accounts().get();
    while(accountItr.hasNext()) {
      account = accountItr.next();
      if (accountList.indexOf(account.getName()) > -1) {
        accountIdList.push(account.getCustomerId())
      }
    }

    if(accountList.length > 0) {
        var accountSelector = MccApp.accounts()
            .withIds(accountIdList)
            .withLimit(40);

        accountSelector.executeInParallel('adjustCPCmax');
    }

}


String.prototype.format = function () {
  var i = 0, args = arguments;
  return this.replace(/{}/g, function () {
    return typeof args[i] != 'undefined' ? args[i++] : '';
  });
};


function isBlank(line) {
    return line[0].trim() === '' && line[1].trim() === '';
}


function parseData(data) {

    const output = {};
    var currentGroupName = '';

    data.forEach(function(line, index){
        if (isBlank(line) || index === 0){
            return; 
        }

        if (line[0].trim().length > 1) {
            currentGroupName = line[0].trim();
        }

        output[currentGroupName] = output[currentGroupName] || {};

        output[currentGroupName][line[1]] = line[2];
    });

    return output;
}


function accountsListing(data) {
    function isBlank(line){
        return line[0].trim() === '';
    }

    const output = [];

    data.forEach(function(line, index) {
        if (isBlank(line) || index === 0) {
            return;
        }

        output.push(line[0])
    });

    return output
}


function parseKeyword(keyword, keywordId, maxCPC) {

    Logger.log('THE NAME OF THE KEYWORDID IS ' + keywordId + 'n')

    var report = AdWordsApp.report(
               'SELECT Id, Criteria, CampaignName, CpcBid, FirstPageCpc, FirstPositionCpc, TopOfPageCpc, Criteria ' +
               'FROM   KEYWORDS_PERFORMANCE_REPORT ' +
               'WHERE ' + 
               'Id = ' + keywordId);
    var rows = report.rows();
    while(rows.hasNext()) {
        var row = rows.next();
        var keywordIdReport = row['Id'];
        var keywordNameReport = row['Criteria'];
        var campaignName = row['CampaignName'];
        var cpcBid = row['CpcBid'];
        var firstPageCpc = row['FirstPageCpc'];
        var firstPositionCpc = row['FirstPositionCpc'];
        var topOfPageCpc = row['TopOfPageCpc'];

        Logger.log('INFO')
        Logger.log(keyword.getText())
        Logger.log(keywordId)
        Logger.log(keywordNameReport)
        Logger.log(keywordIdReport + 'n')

        if (keywordId === keywordIdReport) {

            if (firstPositionCpc && (firstPositionCpc > 0 && firstPositionCpc <= maxCPC)) {
                var newCPC = firstPositionCpc;
            } else if (topOfPageCpc  && (topOfPageCpc > 0 && topOfPageCpc <= maxCPC)) {
                var newCPC = topOfPageCpc;
            } else if (firstPageCpc && (firstPageCpc > 0 && firstPageCpc <= maxCPC )) {
                var newCPC = firstPageCpc;
            } else {
                var newCPC = minCPC;
            }

            Logger.log('KeywordIdReport :' + keywordIdReport)
            Logger.log('campaignName :' + campaignName)
            Logger.log('CPCbid :' + cpcBid)
            Logger.log('firstPositionCpc : ' + firstPositionCpc)
            Logger.log('topOfPageCpc : ' + topOfPageCpc)
            Logger.log('firstPageCpc : ' + firstPageCpc)
            Logger.log('NewCPC : ' + newCPC + 'n')

            keyword.bidding().setCpc(newCPC)
            break;
        }
    }
}


function getMaxCPC(account, campaign) {
    var cleanData= parseData(data);

    //Formatting account and campaign
    var account = '{}'.format(account.getName())
    var campaign = '{}'.format(campaign.getName())

    Logger.log('Account :' + account)
    Logger.log('Campaign :' + campaign + 'n')

    return cleanData[account][campaign];
}


function adjustCPCmax() {
    //min CPC
    var minCPC = 0.50;

    var account = AdWordsApp.currentAccount();
    Logger.log('=================================' + account.getName() + "=======================================")
    var campaignIterator = AdWordsApp.campaigns().get();
    while (campaignIterator.hasNext()) {
        var campaign = campaignIterator.next();
        try {
            var maxCPC = getMaxCPC(account, campaign)
        }
        catch(e) {
        }

        if (maxCPC) {
            Logger.log('The entrence worked with max CPC : ' + maxCPC + 'n')
            keywordIterator = campaign.keywords().get();

            var startTime= (new Date()).getTime();
            while (keywordIterator.hasNext()) {
                var keyword= keywordIterator.next()
                var keywordId = Number(keyword.getId()).toPrecision()

                parseKeyword(keyword, keywordId, maxCPC);
            }
        }
    }
}

请注意,此代码可以在MCC脚本环境中使用。 它可以用于Google电子表格中的任何帐户。

PS下面的问题给了我一个好主意如何处理,但我想知道你的建议。

链接地址: http://www.djcxy.com/p/32361.html

上一篇: Exceeded maximum execution time in Google Adwords

下一篇: DEVELOPER