I'm trying to get the BTC-EUR ticker from GDAX site to Google Spreadsheet using a script. I got this code but it doesn't work, always returning me error: The coordinates or dimensions of the range are invalid.
var baseUrl = 'https://api.gdax.com';
var data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GDAX");
function ticker() {
var request = "/products/btc-eur/ticker";
var requestUrl = baseUrl + request;
var response = UrlFetchApp.fetch(requestUrl);
var json = JSON.parse(response.getContentText());
var rows = [],
jsondata;
for (i = 0; i < json.length; i++) {
jsondata = json[i];
rows.push([jsondata.price]);
}
dataRange = data.getRange(14, 1, rows.length, 1);
dataRange.setValues(rows);
}
How about a following modification?
The JSON data from https://api.gdax.com/products/btc-eur/ticker
is as follows. In this data, the value of price
is only one. So when the JSON data is always like this, you can directly retrieve the value using json.price
.
{
"trade_id": 4314549,
"price": "3691.06000000",
"size": "0.00004053",
"bid": "3691",
"ask": "3691.05",
"volume": "945.78845044",
"time": "2017-01-01T00:00:00.000000Z"
}
When this is reflected to your script, the modified script is as follows.
function ticker() {
var baseUrl = 'https://api.gdax.com';
var data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GDAX");
var request = "/products/btc-eur/ticker";
var requestUrl = baseUrl + request;
var response = UrlFetchApp.fetch(requestUrl);
var json = JSON.parse(response.getContentText());
var rows = [[json.price]];
var dataRange = data.getRange(14, 1, 1, 1);
dataRange.setValue(rows);
}
If I misunderstand your question, I'm sorry.