我试图重构我编写的Google AdWords脚本的一部分,以便不必重复if语句,我希望我可以通过&的实例动态更新#34;这&#34 ;.但是,我正在接收"无法在对象firstKeywords中找到函数push。 (第34行)"错误。当我简单地插入" firstKeywords.push"而不是" this.keywordsArray.push"该脚本有效。想知道是否有办法让这个"这个"实例被视为一个数组,或者是否还有另一种解决方法?
Error: this.keywordsArray.push
非工作脚本:
var firstKeywords = [];
var secondKeywords = [];
var thirdKeywords = [];
function main() {
function testKeywords(adgr, keywordsArray) {
this.adgr = adgr;
this.keywordsArray = keywordsArray;
}
testKeywords.prototype.move = function () {
var campaignIterator = AdWordsApp.campaigns()
.withCondition("Status = ENABLED")
.withCondition("Name CONTAINS_IGNORE_CASE 'High'")
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var adGroupIterator = campaign.adGroups()
.withCondition("Name CONTAINS_IGNORE_CASE 'Blogs'")
.get();
while (adGroupIterator.hasNext()) {
var adGroup = adGroupIterator.next();
var adGroupName = adGroup.getName();
var keywordIterator = adGroup.keywords()
.withCondition("SystemServingStatus = RARELY_SERVED")
.get();
while (keywordIterator.hasNext()) {
var keyword = keywordIterator.next();
var keywordText = keyword.getText();
var adgroupArray = this.keywordsArray;
if (adGroupName === this.adgr) {
this.keywordsArray.push(keywordText);
keyword.pause();
}
}
}
}
}
var test01 = new testKeywords("General Music Blogs", "firstKeywords");
var test02 = new testKeywords("Hip Hop Music Blogs", "secondKeywords");
var test03 = new testKeywords("Indie Music Blogs", "thirdKeywords");
test01.move();
test02.move();
test03.move();
}
以下脚本的工作方式正确,但重复了if语句。
var firstKeywords = [];
var secondKeywords = [];
var thirdKeywords = [];
function main() {
function testKeywords() {
var campaignIterator = AdWordsApp.campaigns()
.withCondition("Status = ENABLED")
.withCondition("Name CONTAINS_IGNORE_CASE 'High'")
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var adGroupIterator = campaign.adGroups()
.withCondition("Name CONTAINS_IGNORE_CASE 'Blogs'")
.get();
while (adGroupIterator.hasNext()) {
var adGroup = adGroupIterator.next();
var adGroupName = adGroup.getName();
var keywordIterator = adGroup.keywords()
.withCondition("SystemServingStatus = RARELY_SERVED")
.get();
while (keywordIterator.hasNext()) {
var keyword = keywordIterator.next();
var keywordText = keyword.getText()
if (adGroupName === 'General Music Blogs') {
firstKeywords.push(keywordText);
keyword.pause();
}
if (adGroupName === 'Hip Hop Music Blogs') {
secondKeywords.push(keywordText);
keyword.pause();
}
if (adGroupName === 'Indie Music Blogs') {
thirdKeywords.push(keywordText);
keyword.pause();
}
}
}
}
}
}
提前致谢。
答案 0 :(得分:0)
在您的testKeyword中,您传递的是string
文字,而不是object
参考。
所以改变
var test01 = new testKeywords("General Music Blogs", "firstKeywords");
var test02 = new testKeywords("Hip Hop Music Blogs", "secondKeywords");
var test03 = new testKeywords("Indie Music Blogs", "thirdKeywords");
到
var test01 = new testKeywords("General Music Blogs", firstKeywords);
var test02 = new testKeywords("Hip Hop Music Blogs", secondKeywords);
var test03 = new testKeywords("Indie Music Blogs", thirdKeywords);