1

I'm wondering if there is a way in Google script to use a variable to get data and write it on a cell from a nested API call. For example:

var symbol = "BTC";

  var responseAPI = UrlFetchApp.fetch(url+apikey+"&symbol="+symbol+"&convert=EUR");

  var parcedData = JSON.parse(responseAPI.getContentText());

  index.getRange(2,1).setValue([parcedData.data.BTC.symbol]);
  index.getRange(2,2).setValue([parcedData.data.BTC.quote.EUR.price]);

This is the code I use to retrieve BTC price from new Coinmarketcap API (documentation is here https://coinmarketcap.com/api/documentation/v1/#operation/getV1CryptocurrencyQuotesLatest). Since I already declare the BTC symbol in a variable, is there a way to substitute BTC in "setValue"? Something like:

index.getRange(2,1).setValue([parcedData.data.+symbol+.symbol]);
                                 variable here ^^^^^^

This is not working of course, is there a way to make it works? Thank you in advance

1 Answer 1

3

Yes, there is, nothing stops you from accessing a variable property name here, just remember to use bracket notation - parcedData.data[symbol].symbol - so long as the symbol variable is reachable within the scope of setValue(), you should be good to go.

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

1 Comment

You are welcome! Btw, since ES6 came around, you can even use calculated properties (for example, parcedData.data[ doSomething() ], as long as doSomething returns a property name

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.