Den123
Den123

Reputation: 21

Reading key-value json data into google sheet cells

I am looking to bring in the "bid" values from each "ticker" from this API call https://api.etherdelta.com/returnTicker into Google Sheet cells.

An example cell value will have something like: =crypt("PPT).

Here is the code I have so far, but I am having a hard time figuring out how I can get the data for each ticker (I know I haven't declared "ticker" anywhere in the code).

function crypt(ticker) {

  var url = "https://api.etherdelta.com/returnTicker";

  var response = UrlFetchApp.fetch(url);
  var text = response.getContentText();

  var json = JSON.parse(text);
  var price = json[bid];

  return parseFloat(bid);

}

Upvotes: 2

Views: 975

Answers (1)

Tanaike
Tanaike

Reputation: 201613

How about the following modifications?

Modification points :

  • Each ticker name has a header of ETH_.
  • ETH_ + ticker is a key of the object.
    • When =crypt("PPT") is used, the key is ETH_PPT and "bid" you want is in the value of ETH_PPT.

The modified script which was reflected above is as follows.

Modified script :

function crypt(ticker) {

  var url = "https://api.etherdelta.com/returnTicker";

  var response = UrlFetchApp.fetch(url);
  var text = response.getContentText();

  var json = JSON.parse(text);
  var price = json["ETH_" + ticker].bid; // Modified

  return parseFloat(price); // Modified

}

This modified script retrieves the value of bid for each ticker by putting =crypt("PPT") to a cell in the spreadsheet.

Note :

  • It seems that an error response is sometimes returned from the URL.

If I misunderstand your question, I'm sorry.

Upvotes: 2

Related Questions