【问题标题】:How to pull data from additional pages through pagination如何通过分页从附加页面中拉取数据
【发布时间】:2022-12-10 12:41:25
【问题描述】:

我成功返回了第一页数据,并获得了 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);
  }

分页的示例响应如下所示;

"pagination": {
        "count_per_page": 20,
        "total_count": 1,
        "current_page": 1,
        "total_pages": 2,
        "_links": {
            "next": {
                "href": "/v2/organizations?page=2"
            }
        }
    }

【问题讨论】:

  • API 是否返回下一页标记,或者 current_page 是否像您的示例中那样被更新
  • 您好库珀,感谢您的回复。我不相信 API 会返回下一页令牌。它只是更新当前页面。谢谢。

标签: javascript google-apps-script urlfetch


【解决方案1】:

不知道你的实际反应是什么样子很难,但我认为这会做你想要的:

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,
    ]);
  });
}

【讨论】:

  • 感谢您回答我的问题。不幸的是,我收到一个 TypeError:无法读取未定义的属性“next”。
  • 当将端点更改为第 2 页和 console.log 对象键时,我得到以下信息:{ count_per_page: 20, total_count: 12309, current_page: 2, total_pages: 616, _links: { previous: { href: '/v2/organizations ?page=1' }, next: { href: '/v2/organizations?page=3' } } }
【解决方案2】:

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

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)
    });
       
 }

【讨论】:

    猜你喜欢
    • 2014-08-27
    • 2015-09-04
    • 2020-11-12
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多