ReferenceError: “PropertiesService” is not defined

心不动则不痛 提交于 2019-12-11 16:16:17

问题


From the following question, I try to use part of the following code in an Adwords script

function runMe() {
  var startTime= (new Date()).getTime();

  //do some work here

  var scriptProperties = PropertiesService.getScriptProperties();
  var startRow= scriptProperties.getProperty('start_row');
  for(var ii = startRow; ii <= size; ii++) {
    var currTime = (new Date()).getTime();
    if(currTime - startTime >= MAX_RUNNING_TIME) {
      scriptProperties.setProperty("start_row", ii);
      ScriptApp.newTrigger("runMe")
               .timeBased()
               .at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
               .create();
      break;
    } else {
      doSomeWork();
    }
  }

  //do some more work here

}

but I got ReferenceError: "PropertiesService" is not defined. (line 170) from google-apps-script. How could I fix that issue? Can you tell me if it works for you in Adwords?

UPDATED

Here is the function I build in considering the previous function :

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

    var accountIterator = MccApp.accounts().get();
    var mccAccount = AdWordsApp.currentAccount();
    while(accountIterator.hasNext()) { 
        var account = accountIterator.next();
        Logger.log('============================================== ' + account.getName() + ' ====================================================')
        MccApp.select(account)

        var campaignIterator = AdWordsApp.campaigns().get();
        while (campaignIterator.hasNext()) {
            var campaign = campaignIterator.next();
            try {
                var maxCPC = getMaxCPC(account, campaign)
            }
            catch(e) {
            }

            if (maxCPC) {
                var startTime= (new Date()).getTime();

                Logger.log('The entrence worked with max CPC : ' + maxCPC + '\n')
                keywordIterator = campaign.keywords().get();
                while (keywordIterator.hasNext()) {
                    var keyword= keywordIterator.next()
                    var keywordId = Number(keyword.getId()).toPrecision()

                    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;
                        }
                    }
                    var REASONABLE_TIME_TO_WAIT = 4*6000;
                    var MAX_RUNNING_TIME = 1*6000;
                    try {
                        var scriptProperties = PropertiesService.getScriptProperties();
                    }
                    catch(e){
                        Logger.log(e)
                    }
                    var startRow= scriptProperties.getProperty('start_row');
                    for(var ii = startRow; ii <= size; ii++) {
                        var currTime = (new Date()).getTime();
                        if(currTime - startTime >= MAX_RUNNING_TIME) {
                            scriptProperties.setProperty("start_row", ii);
                            ScriptApp.newTrigger("runMe")
                                .timeBased()
                                .at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
                                .create();
                            break;
                        }
                    }
                }
            }
        }
    }
    MccApp.select(mccAccount);
}

From that function, what could I do to fix that problem? It is bizarre because I could find documentation on the subject.


回答1:


You are using AdWords Script which is different from Google Apps Script even though they share many APIs. Google Apps Script has access to Properties Service, AdWords Script does not. Evidence: the AdWords Script reference (screenshot) does not include Properties Service among Script Services.

You are using script properties to store one number, the current row number. An alternative is to store it in a spreadsheet, since AdWords Script has access to Spreadsheet service. So, at the beginning of the script you would have

var ss = SpreadsheetApp.openByUrl('...'); // url of some spreadsheet
var sheet = ss.getSheets()[0];    // first sheet in it
var cell = sheet.getRange("A1");  // storing the number in A1 cell

and then within the body of the script, replace

var startRow = scriptProperties.getProperty('start_row');

by

var startRow = cell.getValue() || 1;  // 1 by default 

and also replace

scriptProperties.setProperty("start_row", ii);

by

cell.setValue(ii);

This should do it.



来源:https://stackoverflow.com/questions/47291439/referenceerror-propertiesservice-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!