简体   繁体   中英

How to parse API data into google sheets

I have an API response that I'm trying to place in my spreadsheet.

I managed to figure out how to call it using the following code but it all goes to the first cell. How do I make each value go to a different cell?

function callCandles() {
  var response = UrlFetchApp.fetch("https://api-pub.bitfinex.com/v2/candles/trade:1D:tBTCUSD/hist?limit=1000&start=1577841154000&end=1606785154000&sort=-1");
  Logger.log(response.getContentText());

  var fact = response.getContentText();
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1,1).setValue([fact])

}

Try something like this,

 var response = UrlFetchApp.fetch("https://api-pub.bitfinex.com/v2/candles/trade:1D:tBTCUSD/hist?limit=1000&start=1577841154000&end=1606785154000&sort=-1");
 Logger.log(response.getContentText());

 var fact = JSON.parse(response.getContentText());
 var sheet = SpreadsheetApp.getActiveSheet();
 fact.forEach(row => {
   sheet.appendRow(row);
 });

Hope you got some idea.

This is the correct way to set the values in a sheet. Avoid using for loops with appendRow . It can be extremely slow for a relatively small amount of data.

Suggested solution:

function myFunction() {
 var response = UrlFetchApp.fetch("https://api-pub.bitfinex.com/v2/candles/trade:1D:tBTCUSD/hist?limit=1000&start=1577841154000&end=1606785154000&sort=-1");
 var fact = JSON.parse(response.getContentText());
 var sheet = SpreadsheetApp.getActiveSheet();
 sheet.getRange(sheet.getLastRow()+1,1,fact.length,fact[0].length).setValues(fact);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM