1

Pretty new to all this but have a simple script to pull API info and put into google sheets. I want to pull the top 20 coins but I'm unsure of how to do it as a ??'function'?? to limit the amount of code required presently especially since only 'XXX' is basically changing. Thanks in advance

 var urlBTC='https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&limit=2';
 var responseBTC = UrlFetchApp.fetch(urlBTC,{'muteHttpExceptions': true});
 var jsonBTC = responseBTC.getContentText();
 var parseBTC = JSON.parse(jsonBTC);

 sheetBTC.getRange(3,3).setValue(parseBTC[0][6]);


 var sheetETH = sh.getSheetByName("ETH");
 var urlETH='https://api.binance.com/api/v3/klines?symbol=ETHUSDT&interval=1d&limit=2';
 var responseETH = UrlFetchApp.fetch(urlETH,{'muteHttpExceptions': true});
 var jsonETH = responseETH.getContentText();
 var parseETH = JSON.parse(jsonETH);

 sheetETH.getRange(3,3).setValue(parseETH[0][6]);

}```
1
  • I assume you know how to obtain a list of top 20 coins from the API you are using? If so, you can try looping through the list then use string concatenation to pass into the URL parameters. I will give you a more detailed answer if you need it. Commented Jun 19, 2020 at 2:42

1 Answer 1

1
    var coins = ['ETHUSDT','BTCUSDT']
    function getCoin(){
     coins.forEach(coin => {
       let url = 'https://api.binance.com/api/v3/klines?symbol='+ coin
      + '&interval=1d&limit=2'

      //do the other stuff
      })
     }

Hope this helps set you off in the right direction.

Basically we store the symbols as an array loop through and create a url for that, in the 'do something' put in your other code for handling the req. watch out for rate limits

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

4 Comments

Tried this method but keep getting forEach.coin is not a function. Appreciate the help though. Will keep searching
@Upland is the syntax being followed correctly? just ran a test with the code above and was able to generate the URLs. Just bringing this up because you typed forEach.coin I corrected my mistake of forearch to forEach.
Thank you sir... I was going to start studying Array Loops tonight. Very much appreciated.
If you other questions let me know

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.