如何使用JSON.parse()将数据获取到部件中?

问题描述 投票:0回答:1

我的代码工作正常,有限的数据从API到谷歌表。但是,当我试图运行相同的代码与大数据(如以上10,000记录),它给出了错误(由于时间限制超过),我正在寻找的解决方案是得到的记录在部分,0到2000,然后2001年至4000等,直到最后的记录。

如果有谁能给我一些代码或想法来做这件事,并使这段代码也能用于大记录,请让我知道。

async function pullJSON() {

 var requestOptions = {
    method: 'GET',
    followRedirects: true};

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getSheetByName("Sheet7"); // specific sheet name; alternatively use ss.getActiveSheet()

  var url="https://api.covid19api.com/summary"; // Paste your JSON URL here

  var response = await UrlFetchApp.fetch(url,requestOptions); // get feed
  var text = await response.getContentText();
  var text2 = await text.toString();
  var json = await JSON.parse(text2).Countries;
  var dataSet = json;


  var rows = [],
      data;


  for (i = 0; i < dataSet.length; i++) {
    data = dataSet[i];
    rows.push([data.Country, data.TotalConfirmed]); //your JSON entities here
  }

  dataRange = sheet.getRange(2, 1, rows.length, 2); // last '2' Denotes total number of entites sheet.getR
  dataRange.setValues(rows);

}
javascript google-apps-script urlfetch
1个回答
0
投票

EDIT:

现在,我明白了这个问题,我想我有一个解决方案。首先,你获取国家列表。然后,你单独查询这些国家和存储他们的数据。现在我看到Google Apps Scripts被限制在4-5分钟内,所以这个解决方案可能受到两方面的限制,1)你的网速,2)你的处理能力,因为Google Apps Scripts在你的机器上使用Javascript V8引擎运行。

所以这是我想出的办法。如果有帮助,请告诉我。(我使用node开发的,所以希望我记得把一切调整回你的原始代码)

async function pullJSON() {
  var requestOptions = {
    method: 'GET',
    followRedirects: true};

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getSheetByName("Sheet7"); // specific sheet name; alternatively use ss.getActiveSheet()

  let countriesURL = "https://api.covid19api.com/countries";

  var countriesResponse = await UrlFetchApp.fetch(countriesURL,requestOptions); // get feed
  var countriesText = await countriesResponse.getContentText().toString();
  var countries = await JSON.parse(countriesText);

  for (let i = 0; i < countries.length; i++) {
    var country = countries[i];
    let url = `https://api.covid19api.com/country/${country.Slug}/status/confirmed`;
    var summaryResponse = await UrlFetchApp.fetch(countriesURL,requestOptions); // get feed
    var summaryText = await summaryResponse.getContentText().toString();
    var summary = await JSON.parse(summaryText);

    latestSummary = summary[summary.length - 1]; // Gets only the latest case count
    dataRange = sheet.getRange(2 + i, 1, 1, 2);
    dataRange.setValues([[latestSummary.Country, latestSumary.Cases]]);
  }
}

原创:

试试这样的东西。

// after you fetch and parse all the data...
let interval = 2000; // change this to change how many go at once

for (let i = 0; i < rows.length; i += interval) {
  let tempRows = rows.slice(i, Math.min(i + interval, rows.length + 1));
  dataRange = sheet.getRange(2 + i * interval, 1, 2 + tempRows.length, 2);
  dataRange.setValues(tempRows);
}
© www.soinside.com 2019 - 2024. All rights reserved.