ReferenceError:" PropertiesService"未定义

时间:2017-11-14 17:04:48

标签: javascript google-apps-script google-adwords

从以下question开始,我尝试在 Adwords脚本

中使用以下部分代码
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

}

但我从ReferenceError: "PropertiesService" is not defined. (line 170)获得google-apps-script。我怎么能解决这个问题?你能告诉我Adwords是否适合你吗?

已更新

这是我在考虑前一个函数时构建的函数:

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);
}

从该功能,我该怎么做才能解决这个问题?这很奇怪,因为我可以在这个主题上找到documentation

1 个答案:

答案 0 :(得分:1)

您使用的AdWords Script与Google Apps脚本不同,即使它们共享许多API。 Google Apps脚本可以访问Properties Service,而AdWords脚本则无法访问。证据:AdWords脚本参考(screenshot)不包括脚本服务中的属性服务。

您正在使用脚本属性存储一个数字,即当前行号。另一种方法是将其存储在电子表格中,因为AdWords脚本可以访问电子表格服务。所以,在脚本的开头你会有

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

然后在脚本体内,替换

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

通过

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

并替换

scriptProperties.setProperty("start_row", ii);

通过

cell.setValue(ii);

这应该这样做。