我是一个新手,并提出以下Google脚本发送GET请求,然后将回复解析为Google表格。
我只能在第一页上请求50个项目,并且我已经尝试搜索循环页面的方法,直到我获得所请求的所有数据。
如果有人能指出我正确的方向,我相信这很容易吗? : - )
function myData1() {
// Call the horizon API
var url = "https://apc.hypaship.com/api/3.0/Orders.json?datefrom=27-03-2018T00:01";
var headers = {"Content-Type": "application/json",
"remote-user": "Basic ****************************"};
var options = {"method" : "get",
"headers" : headers};
var response = UrlFetchApp.fetch(url,options);
var text = response.getResponseCode();
Logger.log(response);
// Parse the JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
//Loop for data required
for (i = 0; i <= data.Orders.Order.length - 1; i++) {
var row = data.Orders.Order[i];
var customer = row.Collection.CompanyName;
var pcde = row.Collection.PostalCode;
Logger.log(customer);
Logger.log(pcde);
//Export to Google Sheet
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(sheet.getLastRow() + 1,1).setValue([customer]);
sheet.getRange(sheet.getLastRow() + 0,2).setValue([pcde]);}
}
{“Orders”:{“AccountNumber”:“279”,“Messages”:{“Code”:“SUCCESS”,“Description”:“SUCCESS”},“Pagination”:{“TotalPages”:“1 “,”NextPage“:null,”PreviousPage“:null,”ItemsPerPage“:”50“,”ItemsTotal“:”4“},
[18-03-27 16:51:46:895 BST] {“Orders”:{“AccountNumber”:“279”,“Messages”:{“Code”:“SUCCESS”,“Description”:“ SUCCESS“},”分页“:{”TotalPages“:”23“,”NextPage“:”2“,”PreviousPage“:null,”ItemsPerPage“:”50“,”ItemsTotal“:”1121“},
答案 0 :(得分:0)
由于您正在使用的API提供了总页数和下一页,因此您可以使用任何您熟悉的循环结构。请注意,根据这些查询所需的时间以及对其结果所做的任何事情,您可能需要采用可恢复的方法。
通常,我建议使用do while
或显式for
循环方法:
function doWhileExample() {
var url = /* my api url */;
var headers = ...;
var options = ...;
// An array of query parameters that are static for this particular request.
var queryParams = [
"datefrom=" + /* value of this param */,
/* other params */
];
var pageKey = /* the string keyword for specifying the result page for your API. Could be "page". */;
var pageNum; /* could read a stored page number, e.g. from cache / properties service */
// Execute your query / queries.
var data = {}, output = [];
do {
var currentUrl = url + "?" + queryParams.join("&");
if (pageNum) {
currentUrl += "&" + pageKey + "=" + pageNum;
}
var response = UrlFetchApp.fetch(currentUrl, options);
var code = response.getResponseCode();
/* do some error checking on the code e.g. don't try to parse data if the server is dead */
// Store data for batch serialization after requests are done.
// (Assumes you store "rectangular" data.)
data = JSON.parse(response.getContentText());
for (var r = 0, rows = data.Orders.Order.length; r < rows; ++r) {
var row = data.Orders.Order[r];
output.push([row.Some.Data.One, row.Some.Data.Two, row.Some.Other.Data, ... ]);
}
// Update the page number.
pageNum = data.Orders.Pagination.NextPage;
} while (pageNum);
// Serialize all data.
var sheet = SpreadsheetApp.openById("someId").getSheetByName("someName");
if (sheet && output.length && output[0].length) {
sheet.getRange(sheet.getLastRow(), 1).offset(1, 0, output.length, output[0].length).setValues(output);
}
}
正如上面的代码中所暗示的,您需要以某种方式向API网址指明您想要特定网页,例如&page=3
如果在网址中接受这些参数(page
是正确的关键字),则可以正常工作 - 它可能需要在标题等中进行此类请求操作。