I want to pull data from Bittrex API. I get the data but I can't find a way to display them on my spreadsheet. An error keep coming and it makes me mad !
I'm sure it is really simple stuff but I give up :)
Here is the code:
function main() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var balancesSheet = ss.getSheetByName("BITTREX 2");
var balancesRange = balancesSheet.getRange("A1:X");
var balancesListValues = balancesRange.getValues();
// Add those values to the active sheet in the current
// spreadsheet. This overwrites any values that already
// exist there.
balancesSheet.getRange(1, 1, balancesRange.getHeight(), balancesRange.getWidth())
.setValues(balancesListValues);
var apikey = 'My APY Key is here'; // Please input your key.
var apisecret = 'My API Secret is here'; // Please input your secret.
var nonce = Number(new Date().getTime() / 1000).toFixed(0);
var uri = 'https://bittrex.com/api/v1.1/market/getopenorders?apikey=' + apikey + '&nonce=' + nonce;
var sign = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, apisecret);
sign = sign.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2);}).join("");
var params = {
method: "get",
headers: {Apisign: sign}
}
var response = UrlFetchApp.fetch(uri, params);
var getContext= response.getContentText();
var parsedata = JSON.parse(getContext);
//ss.getRange(1,1).setValue(parsedata.foreach)
parsedata.data.forEach(function(result, index) {
balancesListValues.getRange(+3, 1).setValue(result.quantity)
balancesListValues.getRange(+3, 2).setValue(result.price)
})
Logger.log(parsedata.result);
}
And here is the error :
TypeError: Cannot read property 'forEach' of undefined on line 31 (this one: parsedata.data.forEach(function(result, index))
My goal is to transform the raw JSON data in a nice spreadsheet. I'm open to suggestions. I've made that with a lot of Copy/Paste so don't bother the tidyness !
Thank you for your help, NiphtiAe.
EDIT
Awesome ! I ended with that code to get the data updating instead of pilling in rows :
function main() {
// Retrieve values from API.
var apikey = '*********'; // Please input your key.
var apisecret = '**********'; // Please input your secret.
var nonce = Number(new Date().getTime() / 1000).toFixed(0);
var uri = 'https://bittrex.com/api/v1.1/account/getbalances?apikey=' + apikey + '&nonce=' + nonce;
var sign = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, apisecret);
sign = sign.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2);}).join("");
var params = {
method: "get",
headers: {Apisign: sign}
}
var response = UrlFetchApp.fetch(uri, params);
var getContext= response.getContentText();
var parsedata = JSON.parse(getContext);
console.log(getContext) // Here, the response value can be confirmed at the log.
// Use Spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var balancesSheet = ss.getSheetByName("BITTREX 2");
var balancesRange = balancesSheet.getRange("A1:X");
var balancesListValues = balancesRange.getValues();
balancesSheet.getRange(1, 1, balancesRange.getHeight(), balancesRange.getWidth()).setValues(balancesListValues);
var values = parsedata.result.map(({Currency, Available}) => [Currency, Available]);
balancesSheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
Just have to call prices from CMC or Coingecko or else and voilà !
Huge thanks to @Tanaike !!