循环只是保持返回相同的JSON对象,但是我改变了偏移...有人知道什么是错的吗?
我会写完整个功能以确定,可能不是很干。
function getDeals (api_key) {
//get the right spreadsheet & sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Blad1');
sheet.clear();
sheet.appendRow(["Dealname", "Dealstage", "Close date", "Amount"]);
//set up API variables
var offset = 0;
var queryParams = '&limit=250&offset=' + offset + '&properties=dealname&properties=dealstage&properties=closedate&properties=amount'
var _url = 'https://api.hubapi.com/deals/v1/deal/paged?hapikey=' + api_key;
var options = {
"method": "GET",
"muteHttpExceptions": true
};
//set up functions for call
var apiCall = function(url, endpoint){
var apiResponse = UrlFetchApp.fetch(url + endpoint,options);
var json = JSON.parse(apiResponse);
return json
};
var filterDeals = function (url, unfiltered_deals){
var i = 0;
while (unfiltered_deals.status !== "error" && i < (unfiltered_deals.deals).length) {
if (unfiltered_deals.deals[i].properties.dealstage.value == "closedwon") {
var dealname = unfiltered_deals.deals[i].properties.dealname.value;
var dealstage = unfiltered_deals.deals[i].properties.dealstage.value;
var closedate = new Date((unfiltered_deals.deals[i].properties.closedate.value)*1000);
var amount = unfiltered_deals.deals[i].properties.amount.value;
var deals = [dealname, dealstage, closedate, amount];
// Append Hubspot data to Spreadsheet
sheet.appendRow(deals);
}
i++;
}
if (unfiltered_deals.hasMore) {
offset = unfiltered_deals.offset;
var newDeals = apiCall(url, queryParams);
filterDeals(_url, newDeals, offset, queryParams);
} else {
return
}
}
//Api call to get deals
var dealCall = apiCall(_url, queryParams);
var filteredDeals = filterDeals(_url, dealCall);
};
我用API密钥调用此函数。
答案 0 :(得分:1)
您始终获得相同JSON参数的原因是您永远不会使用新的偏移值更新变量queryParams
。在您的代码段中:
if (unfiltered_deals.hasMore) {
offset = unfiltered_deals.offset;
var newDeals = apiCall(url, queryParams);
filterDeals(_url, newDeals, offset, queryParams);
}
您获得新的偏移值,即offset = unfiltered_deals.offset;
。但您需要通过重新初始化其值来更新您的queryParams
if (unfiltered_deals.hasMore) {
offset = unfiltered_deals.offset;
queryParams = '&limit=250&offset=' + offset + '&properties=dealname&properties=dealstage&properties=closedate&properties=amount'
var newDeals = apiCall(url, queryParams);
filterDeals(_url, newDeals, offset, queryParams);
}