如何通过分页从附加页面中拉取数据

How to pull data from additional pages through pagination

我成功返回了第一页数据,并获得了 API 调用中存在的额外数据页数。

这是我尝试拉取额外数据页的代码。

try {
const response = UrlFetchApp.fetch(root + endpoint, params);
const responseCode = response.getResponseCode();
const returnedResults = response.getContentText();
const jsonResults = JSON.parse(returnedResults)
//console.log(responseCode) // *** return 200
//console.log(returnedResults) // *** return successful
//console.log(jsonResults) //*** return successful


Object.keys(jsonResults).forEach(function (key){
  console.log(jsonResults[key]);
  /*
  { count_per_page: 20,
    total_count: 12261,
    current_page: 1,
    total_pages: 614,
    _links: { next: { href: '/v2/organizations?page=2' } } }
  */

  });
} catch (e) {
  console.log('Error: ' + e);
  }

const data = [];

let hasmoreitems = true;
  while (hasmoreitems) {
    
    hasmoreitems = jsonResults.pagination.current_page < jsonResults.pagination.total_pages;
   
  data.forEach(entry => {
    const name = entry['name'];
    const email = entry['email'];
    const mission = entry['mission_statement'];
    const address = entry['address'].country;

  data.push([
    name, email, mission, address]);
  });

  // send data to spreadsheet
  const sheet = SpreadsheetApp.openById('1fM-soJt8rJeIHYZ2vVKyoX9J-LohElqvGB2syhlRk00').getSheets()[1];

  sheet.getRange(sheet.getLastRow()+1, 1, data.length,data[0].length).setValues(data);
  }

分页响应示例如下所示;

“分页”:{ "count_per_page": 20, "total_count": 1, "current_page": 1, "total_pages": 2, “_链接”:{ “下一个”: { "href": "/v2/organizations?page=2" } } }

很难不知道您的实际反应是什么样的,但我认为这会满足您的要求:

let endpoint = "/v2/organizations?page=1";
let all_data = [];
while (endpoint) {
  let response = UrlFetchApp.fetch(root + endpoint, params);
  let data = JSON.parse(response.getContentText());
  endpoint = data._links.next.href;
  data.forEach((entry) => {
    all_data.push([
      entry.name,
      entry.email,
      entry.mission_statement,
      entry.address.country,
    ]);
  });
}

在检查了对象的结构后,我意识到了键的不同级别。下面是工作代码。希望对您有所帮助。

let all_data = [];

  while (endpoint) {
    let response = UrlFetchApp.fetch(root + endpoint, params);
    let data = JSON.parse(response.getContentText());
    let orgs = data.organizations

    Object.keys(data).forEach(function (key) {
      console.log(data[key]);
    });

    endpoint = data.pagination._links.next.href;
    orgs.forEach((entry) => {
      all_data.push([
        entry.name,
        entry.email,
        entry.mission_statement,
        entry.address.country,
      ]);
      //console.log(all_data)
    });
       
 }