Chris Willson
Chris Willson

Reputation: 1

Google sheets API script

I'm trying to use the following REST API within one of my sheets, and am struggling to return a specific result from the JSON array (first timer here!!)

Here's what I have so far....

function Bittrex_API() {

  // Call the Bittrex API for market rates
  var response = UrlFetchApp.fetch(
      "https://bittrex.com/api/v1.1/public/getmarketsummaries");

  // Parse the JSON reply
  var json = response.getContentText();
  var data = JSON.parse(json);

  Logger.log(data);
}

This returns the full array in the Logs, but how do I get it to pull all information from a specific result in the array?

I can return specific results from the first section, by calling the index position "0" as follows:

function Bittrex_API() {

  // Call the Bittrex API for market rates
  var response = UrlFetchApp.fetch(
      "https://bittrex.com/api/v1.1/public/getmarketsummaries");

  // Parse the JSON reply
  var json = response.getContentText();
  var data = JSON.parse(json);

  Logger.log(data["result"][0]);
}

But how do I specify a different section of the array without using the index number??

i.e. I want to return all information relating to MarketName=BTC-RCN.

Upvotes: 0

Views: 204

Answers (1)

Ed Nelson
Ed Nelson

Reputation: 10259

There is nothing wrong with using the index nunber. Whatever notation you use you will need to find BTC-RCN. You can loop through the index number to find it. This will find it and return the results to Sheet1:

function Bittrex_API() {

  // Call the Bittrex API for market rates
  var response = UrlFetchApp.fetch(
  "https://bittrex.com/api/v1.1/public/getmarketsummaries");

  // Parse the JSON reply
  var json = response.getContentText();
  var data = JSON.parse(json);
  var lgt=data.result.length
  for(i=0;i<lgt;i++){
    var test=data["result"][i]["MarketName"]
      if(test=="BTC-RCN"){
       var num=i;
      }
  }
var MarketName=data["result"][num]["MarketName"]
var High=data["result"][num]["High"]
var Low=data["result"][num]["Low"]
var Volume=data["result"][num]["Volume"]
var Last=data["result"][num]["Last"]
var BaseVolume=data["result"][num]["BaseVolume"]
var TimeStamp=data["result"][num]["TimeStamp"]
var Bid=data["result"][num]["Bid"]
var Ask=data["result"][num]["Ask"]
var OpenBuyOrders=data["result"][num]["OpenBuyOrders"]
var OpenSellOrders=data["result"][num]["OpenSellOrders"]
var PrevDay=data["result"][num]["PrevDay"]
var Created=data["result"][num]["Created"] 

var ss=SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getSheetByName("Sheet1")
s.getRange("A1").setValue("MarketName")
s.getRange("B1").setValue(MarketName)
s.getRange("A2").setValue("High")
s.getRange("B2").setValue(High)
s.getRange("A3").setValue("Low")
s.getRange("B3").setValue(Low)
s.getRange("A4").setValue("Volume")
s.getRange("B4").setValue(Volume)
s.getRange("A5").setValue("Last")
s.getRange("B5").setValue(Last)
s.getRange("A6").setValue("BaseVolume")
s.getRange("B6").setValue(BaseVolume)
s.getRange("A7").setValue("TimeStamp")
s.getRange("B7").setValue(TimeStamp)
s.getRange("A8").setValue("Bid")
s.getRange("B8").setValue(Bid)
s.getRange("A9").setValue("Ask")
s.getRange("B9").setValue(Ask)
s.getRange("A10").setValue("OpenBuyOrders")
s.getRange("B10").setValue(OpenBuyOrders)
s.getRange("A11").setValue("OpenSellOrders")
s.getRange("B11").setValue( OpenSellOrders)
s.getRange("A12").setValue("PrevDay")
s.getRange("B12").setValue(PrevDay)
s.getRange("A13").setValue("Created")
s.getRange("B13").setValue(Created)
}

Upvotes: 1

Related Questions