1

I'm trying to utilize map function in Google SpreadSheets (Google Script) to get my account coin balances from Bittrex using API. Here is my JSON object:

({success:true,
  message:"",
  result:[
      {Currency:"BTC",
       Balance:0.01,
       Available:0.01,
       Pending:0,
       CryptoAddress:null},
      {Currency:"ETH",     
       Balance:1.0,
       Available:1.0,
       Pending:0,
       CryptoAddress:null}
    ]}
})

Ideally I would like to populate header row automatically, based on Keys in result and underlying rows using data from each object. I saw spme solutions how to do that using for each or more complicated way. But I guess this can be done by just mapping. Here is how I mapped top row, but don't know how to map values:

var headerRow = Object.keys(json.result[0]);

Expected output in Google SpreadSheet is

____________________________________________________________    
| Currency | Balance | Available | Pending | CryptoAddress |
|__________________________________________________________|
| BTC      | 0.01    | 0.01      | 0       | null          | 
| ETH      | 1.0     | 1.0       | 0       | null          |
____________________________________________________________
3
  • 1
    Have you tried anything? Commented Dec 15, 2017 at 13:39
  • Can you show us an example of what you expect the output to look like? Commented Dec 15, 2017 at 13:39
  • If you are trying to access the value , try that json.result[headerRow[0]] to get the first. Commented Dec 15, 2017 at 13:43

3 Answers 3

1

I would do the following. So you have the key and then you can retrieve the value.

Object.keys(json.result[0]).map((val) => { console.log(json.result[0][val])})

Wrap that within a foreach to do the same for each result.

Hope that helps

Sign up to request clarification or add additional context in comments.

1 Comment

Thx for the tip, but Google Apps Script doesn't support "=>", so I had to spend some time to write it other way.
0

Here is fully working example:

function getBalances() {
  //Spreadsheet where you store your API key and secret
  var keys = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Keys");
  // Output empty sheet where balances will be written to
  var ex = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Exchanges");
  var nonce = 1995932972127042 + new Date().getTime(); 

  var key = keys.getRange("B2").getValue();
  var secret = keys.getRange("C2").getValue();

  var baseUrl = 'https://bittrex.com/api/v1.1/';
  var command = "account/getbalances";
  var uri = baseUrl + command + "?apikey=" + key + "&nonce=" + nonce;

  var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,uri,secret);
  signature = signature.map(function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join('')

  var headers = {
    "apisign": signature
  }

  var params = {
    "method": "GET",
    "headers": headers,
  }

  var response = UrlFetchApp.fetch(uri, params);  
  var json = JSON.parse(response.getContentText()).result;    
  var balances = json.map(function(coin) {   
    return ["Bittrex",coin["Currency"],coin["Balance"]]; 
  }).filter(function(coin) { if (coin[2] !== 0) return coin; });

  var headings = ["Exchange","Currency","Balance"];
  balances.unshift(headings);
  ex.getRange(1, 1, balances.length, balances[0].length).setValues(balances);
}

Comments

0

Seems you have a solution, but here is another way.

var row = 1;
json.forEach(function(element,index){
   row = ++row;
   Object.keys(json[index]).forEach(function(e,i)
   {
     ++i;
     if(index == 0) { 
       ex.getRange(1, i).setValue(e);
     }
     ex.getRange(row , i).setValue(element[e]);

  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.