简体   繁体   中英

Apps Script - how to get the correct headers range to set the value to a cell google sheets?

function response() {
  var url = 'https://api.zonda.exchange/rest/trading/ticker/BTC-PLN'
  var response = UrlFetchApp.fetch('https://api.zonda.exchange/rest/trading/ticker/BTC-PLN');
  Logger.log(response.getContentText());
  SpreadsheetApp.getActiveSheet().getRange('D4').setValue("hello");
}

Logs:

{ "status": "Ok", "ticker": { "market": { "code": "BTC-PLN", "first": { "currency": "BTC", "minOffer": "0.0000432", "scale": 8 }, "second": { "currency": "PLN", "minOffer": "5", "scale": 2 }, "amountPrecision": 8, "pricePrecision": 2, "ratePrecision": 2 }, "time": "1663016303747", "highestBid": "104000.28", "lowestAsk": "104232.99", "rate": "104300.04", "previousRate": "104232" } }
function getData() {
  const json = '{ "status": "Ok", "ticker": { "market": { "code": "BTC-PLN", "first": { "currency": "BTC", "minOffer": "0.0000432", "scale": 8 }, "second": { "currency": "PLN", "minOffer": "5", "scale": 2 }, "amountPrecision": 8, "pricePrecision": 2, "ratePrecision": 2 }, "time": "1663016303747", "highestBid": "104000.28", "lowestAsk": "104232.99", "rate": "104300.04", "previousRate": "104232" } }';
  let obj = JSON.parse(json);
  Logger.log(obj.ticker.highestBid);
  Logger.log(obj.ticker.lowestAsk);
}

Execution log
3:14:07 PM  Notice  Execution started
3:14:05 PM  Info    104000.28
3:14:05 PM  Info    104232.99
3:14:08 PM  Notice  Execution completed

function getData() {
  const json = '{ "status": "Ok", "ticker": { "market": { "code": "BTC-PLN", "first": { "currency": "BTC", "minOffer": "0.0000432", "scale": 8 }, "second": { "currency": "PLN", "minOffer": "5", "scale": 2 }, "amountPrecision": 8, "pricePrecision": 2, "ratePrecision": 2 }, "time": "1663016303747", "highestBid": "104000.28", "lowestAsk": "104232.99", "rate": "104300.04", "previousRate": "104232" } }';
  let obj = JSON.parse(json);
  let o = Object.keys(obj.ticker).map(k => {
    if (typeof obj.ticker[k] !== 'object') {
      return [k, obj.ticker[k]];
    } else {
      return ["",""];
    }
  }).filter(e => e[0]);
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  sh.clearContents();
  o.unshift(["Key","Value"]);
  sh.getRange(1,1,o.length,o[0].length).setValues(o);
}

The is what my sheet output looks like

Key Value
time 1663016303747
highestBid 104000.28
lowestAsk 104232.99
rate 104300.04
previousRate 104232

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