在此先感谢您的帮助。
我正在尝试使用其Analytics(分析)API(https://developers.hubspot.com/docs/methods/analytics/get-analytics-data-breakdowns)将Hubspot帐户中的数据导入Google表格
我已经在Google App脚本中编写了以下脚本:
var url = API_URL + "/analytics/v2/reports/totals/summarize/daily?&start=20181201&end=20181219";
var response = UrlFetchApp.fetch(url, headers);
var json = response.getContentText();
var dataALL = JSON.parse(json);
var dataSet = dataALL;
Logger.log(dataALL);
var rows = [],
data;
for (i = 0; i < dataSet.length; i++) {
data = dataSet[i];
rows.push(data.visits, data.leads);
}
Logger.log(rows)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
dataRange = sheet.getRange(1, 1, rows.length, 2);
dataRange.setValues([rows])
但是当我尝试Logger.log(rows)
时,它变成空的。当我尝试Logger.log(dataSet.length)
时,它将返回0.0
所以我收集到将JSON数据放入数组时出错。我已经做了一些研究工作,但是找不到适合我特定情况的解决方案。
我要写入电子表格的JSON具有以下格式:
{2018-12-03 = [
{
contactsPerPageview=0.15384615384615385,
rawViews=143,
subscribers=1,
contactToCustomerRate=0.045454545454545456,
privacyConsentDeclines=11,
customersPerPageview=0.006993006993006993,
sessionToContactRate=0.2037037037037037,
pageviewsPerSession=1.3240740740740742,
opportunities=3,
visits=108,
visitors=98,
submissionsPerPageview=0.027972027972027972,
submissions=4,
leads=17,
privacyConsentApproves=9,
customers=1,
contacts=22,
newVisitorSessionRate=0.9074074074074074
}
],
2018-12-14 = [
{
contactsPerPageview=0.06722689075630252,
rawViews=238,
subscribers=4,
privacyConsentDeclines=14,
sessionToContactRate=0.08290155440414508,
pageviewsPerSession=1.233160621761658,
opportunities=6,
visits=193,
visitors=182,
submissionsPerPageview=0.029411764705882353,
submissions=7,
leads=6,
privacyConsentApproves=12,
contacts=16,
newVisitorSessionRate=0.9430051813471503
}]}
你们能指出我正确的方向吗?
再次感谢
答案 0 :(得分:1)
由于dataSet
是一个对象,它没有.length
属性,因此您的for
循环将检查i
是否小于Undefined
,结果为假。这意味着for循环永远不会运行。
我认为您要实现的目标接近以下目标:
var dataSet = Object.keys(dataALL);
Logger.log(dataALL);
var rows = [], data;
for (i = 0; i < dataSet.length; i++) {
data = dataALL[dataSet[i]];
rows.push(data[0].visits, data[0].leads);
}
Logger.log(rows)
请注意,由于对象中的每个键都包含一个数组,因此我还向[0]
和data.visits
都添加了data.leads
,得到了rows.push(data[0].visits, data[0].leads);
。