ReferenceError:"PropertiesService"没有定义 [英] ReferenceError: "PropertiesService" is not defined

查看:68
本文介绍了ReferenceError:"PropertiesService"没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从以下问题中,我尝试使用 AdWords脚本

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

}

但是我从google-apps-script获得了ReferenceError: "PropertiesService" is not defined. (line 170).我该如何解决该问题?您能否在Adwords中告诉我它是否适合您?

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?

这是我在考虑以前的功能时构建的功能:

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.

推荐答案

您正在使用属性服务,AdWords脚本可以不是.证据:AdWords脚本参考(屏幕截图)不包括脚本服务中的属性服务.

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.

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

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

通过

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

并替换

scriptProperties.setProperty("start_row", ii);

通过

cell.setValue(ii);

这应该做到.

这篇关于ReferenceError:"PropertiesService"没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆