大家好,
我的Google表格文件在App脚本上有两个功能。这些是使用DoubleClick Campaign Manager v3.3的DFA API参考创建的。
一个用于列表广告系列ID,另一个用于列表ID。 我的问题是,9/10它们不起作用-抛出JSON Parse错误:
语法错误:流的预期结尾为char 2292 (数字可能有所不同)。
我认为我已经做了我需要做的一切。我参考了API文档进行构建。并使用与我其他工作函数相同的逻辑来处理和执行,但存在相同的错误。
已在“资源”>“高级Google服务”下的“脚本编辑器”中启用了该API。并且使用最新版本-v3.3作为v3.2,现在已弃用。
我似乎遇到的问题是,当我为campaignInfo / placementInfo调用变量时。尽管使用变量“ search”和“ searchString”指定了我要搜索的内容,但我似乎获得了1000多个条目,当我尝试将其解析为JSON的变量“ data”时,它在那里崩溃了。另外,这些条目似乎也都与我的查询无关。
对于“ campaignInfo”,我提供了特定的广告系列名称。我也尝试过指定“ advertiserId”,但是同样的问题。
对于“ placementInfo”,我提供展示位置的“种类”,“ campaignId”和“名称”。我也尝试过指定“ advertiserId”,但是同样的问题。
我正在示例表中使用预设值来测试这两者,到目前为止没有任何内容。 如果有人可以告诉我我哪里出问题了,将不胜感激。
广告系列ID代码:-注意:几个月前,这种方法一直有效,现在却没有。我尚未对其进行任何更改。
function listCampaignIDs() { //and placement end dates
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet15');
var profileID = _fetchProfileId();
var campaignNames = sheet.getRange(2, 2, ss.getRange("B1").getDataRegion().getLastRow()).getValues();
var campaignArray = campaignNames.map(function(r) {
return [r]
});
Logger.log(campaignNames.length)
try {
for (i = 2; i <= campaignNames.length; i++) {
if (sheet.getRange(i, 6).isBlank()) {
var search = {
"name": sheet.getRange(i, 2).getValue().toString()
}
Logger.log(search);
var campaignInfo = DoubleClickCampaigns.Campaigns.list(profileID, search).campaigns;
var data = JSON.parse(campaignInfo);
//Logger.log(data);
var campaignID = data["id"];
var campaignEndDate = data["endDate"];
var campaignStartDate = data["startDate"];
sheet.getRange(i, 6).setValue(campaignID);
sheet.getRange(i, 5).setValue(campaignEndDate);
sheet.getRange(i, 4).setValue(campaignStartDate);
}
}
} catch (e) {
Logger.log("Error is: %s", e);
//SpreadsheetApp.getUi().alert(e);
}
}
展示位置ID代码:-注意:此操作从不起作用...尽管遵循的操作步骤与我对广告系列版本执行的操作类似。
function listPlacementIDs() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet15');
var profileID = _fetchProfileId();
var campaignIDs = sheet.getRange(2, 1, ss.getRange("A1").getDataRegion().getLastRow()).getValues();
var campaignIdsArray = campaignIDs.map(function(r) {
return [r]
});
Logger.log(campaignIDs.length)
var placementNames = sheet.getRange(2, 2, ss.getRange("B1").getDataRegion().getLastRow()).getValues();
var placementNamesArray = placementNames.map(function(r) {
return [r]
});
Logger.log(placementNames.length)
try {
for (i = 2; i <= campaignIDs.length; i++) {
var campaignId = sheet.getRange(i, 1).getValue().toString()
Logger.log(campaignId)
var name = sheet.getRange(i, 2).getValue().toString()
Logger.log(name)
if (sheet.getRange(i, 3).isBlank()) {
var searchString = {
"kind": "dfareporting#placement",
"campaignId": campaignId,
"name": name
}
Logger.log(searchString);
var placementInfo = DoubleClickCampaigns.Placements.list(profileID, searchString).placements;
Logger.log(placementInfo)
var data = JSON.parse(placementInfo);
Logger.log(data);
var placementId = data["id"];
sheet.getRange(i, 3).setValue(placementId);
}
}
} catch (e) {
Logger.log("Error is: %s", e);
SpreadsheetApp.getUi().alert(e);
}
}
谢谢您的宝贵时间。我愿意接受任何可能有助于/改善以下内容的建议。
谢谢, 拉吉夫·艾哈迈德(Rajiv Ahmed)
日志已导致错误,因此已清除。
非常感谢@Jescanellas提供解决问题所需的新视角。
function listCampaignIDs() { //and placement end dates
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Campaigns');
var profileID = _fetchProfileId();
var campaignNames = sheet.getRange(2, 2, ss.getRange("B1").getDataRegion().getLastRow()).getValues();
var campaignArray = campaignNames.map(function(r) {
return [r]
});
try {
for (i = 2; i <= campaignNames.length; i++) {
if (sheet.getRange(i, 6).isBlank()) {
var search = {
"searchString": sheet.getRange(i, 2).getValue().toString(),
"advertiserId": sheet.getRange(i, 1).getValue().toString()
}
var campaignInfo = DoubleClickCampaigns.Campaigns.list(profileID, search).campaigns;
Logger.log(campaignInfo);
var data = JSON.parse(campaignInfo);
Logger.log(data);
var campaignID = data["id"];
var campaignEndDate = data["endDate"];
var campaignStartDate = data["startDate"];
sheet.getRange(i, 6).setValue(campaignID);
sheet.getRange(i, 5).setValue(campaignEndDate);
sheet.getRange(i, 4).setValue(campaignStartDate);
}
}
} catch (e) {
Logger.log("Error is: %s", e);
//SpreadsheetApp.getUi().alert(e);
}
}
function listPlacementIds() { //and placement end dates
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet15');
var profileID = _fetchProfileId();
var campaignIDs = sheet.getRange(2, 1, ss.getRange("A1").getDataRegion().getLastRow()).getValues();
var campaignIdsArray = campaignIDs.map(function(r) {
return [r]
});
var placementNames = sheet.getRange(2, 2, ss.getRange("B1").getDataRegion().getLastRow()).getValues();
var placementNamesArray = placementNames.map(function(r) {
return [r]
});
try {
for (i = 2; i <= campaignIDs.length; i++) {
var campaignId = sheet.getRange(i, 1).getValue().toString()
var name = sheet.getRange(i, 2).getValue().toString()
if (sheet.getRange(i, 3).isBlank()) {
var search = {
"kind": "dfareporting#placement",
"campaignId": campaignId,
"searchString": name
}
var placementInfo = DoubleClickCampaigns.Placements.list(profileID, search).placements;
Logger.log(placementInfo)
var data = JSON.parse(placementInfo);
Logger.log(data);
var placementId = data["id"];
sheet.getRange(i, 3).setValue(placementId);
}
}
} catch (e) {
Logger.log("Error is: %s", e);
SpreadsheetApp.getUi().alert(e);
}
}
答案 0 :(得分:1)
非常感谢@Jescanellas提供解决问题所需的新视角。
function listCampaignIDs() { //and placement end dates
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Campaigns');
var profileID = _fetchProfileId();
var campaignNames = sheet.getRange(2, 2, ss.getRange("B1").getDataRegion().getLastRow()).getValues();
var campaignArray = campaignNames.map(function(r) {
return [r]
});
try {
for (i = 2; i <= campaignNames.length; i++) {
if (sheet.getRange(i, 6).isBlank()) {
var search = {
"searchString": sheet.getRange(i, 2).getValue().toString(),
"advertiserId": sheet.getRange(i, 1).getValue().toString()
}
var campaignInfo = DoubleClickCampaigns.Campaigns.list(profileID, search).campaigns;
Logger.log(campaignInfo);
var data = JSON.parse(campaignInfo);
Logger.log(data);
var campaignID = data["id"];
var campaignEndDate = data["endDate"];
var campaignStartDate = data["startDate"];
sheet.getRange(i, 6).setValue(campaignID);
sheet.getRange(i, 5).setValue(campaignEndDate);
sheet.getRange(i, 4).setValue(campaignStartDate);
}
}
} catch (e) {
Logger.log("Error is: %s", e);
//SpreadsheetApp.getUi().alert(e);
}
}
function listPlacementIds() { //and placement end dates
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet15');
var profileID = _fetchProfileId();
var campaignIDs = sheet.getRange(2, 1, ss.getRange("A1").getDataRegion().getLastRow()).getValues();
var campaignIdsArray = campaignIDs.map(function(r) {
return [r]
});
var placementNames = sheet.getRange(2, 2, ss.getRange("B1").getDataRegion().getLastRow()).getValues();
var placementNamesArray = placementNames.map(function(r) {
return [r]
});
try {
for (i = 2; i <= campaignIDs.length; i++) {
var campaignId = sheet.getRange(i, 1).getValue().toString()
var name = sheet.getRange(i, 2).getValue().toString()
if (sheet.getRange(i, 3).isBlank()) {
var search = {
"kind": "dfareporting#placement",
"campaignId": campaignId,
"searchString": name
}
var placementInfo = DoubleClickCampaigns.Placements.list(profileID, search).placements;
Logger.log(placementInfo)
var data = JSON.parse(placementInfo);
Logger.log(data);
var placementId = data["id"];
sheet.getRange(i, 3).setValue(placementId);
}
}
} catch (e) {
Logger.log("Error is: %s", e);
SpreadsheetApp.getUi().alert(e);
}
}