The reason why your rows
is empty is because your jsonResponce
is not an array. If you will log jsonResponce.length
it will return null
.
Sample code:
function myFunction() {
var jsonData = [{
"makerCommission": 15,
"takerCommission": 15,
"buyerCommission": 0,
"sellerCommission": 0,
"canTrade": true,
"canWithdraw": true,
"canDeposit": true,
"updateTime": 123456789,
"accountType": "SPOT",
"balances": [
{
"asset": "BTC",
"free": "4723846.89208129",
"locked": "0.00000000"
},
{
"asset": "LTC",
"free": "4763368.68006011",
"locked": "0.00000000"
}
],
"permissions": [
"SPOT"
]
}];
var data = JSON.stringify(jsonData);
var jsonResponce = JSON.parse(data); // To JSON
Logger.log(jsonResponce);
Logger.log(Array.isArray(jsonResponce))
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
Logger.log(jsonResponce.length);
var rows = [],
balanc;
for (i = 0; i < jsonResponce.length; i++) {
balanc = jsonResponce[i];
rows.push([balanc.accountType]);
}
Logger.log(rows); /////////////// return [] why not return SPOT
dataRange = sheet.getRange(1, 1, rows.length, 1);
dataRange.setValues(rows);
}
Modifications done:
- create a json array. (encapsulate original json data with
[]
)
Output:
6:37:31 AM Notice Execution started
6:37:31 AM Info [{canDeposit=true, sellerCommission=0.0, balances=[{locked=0.00000000, asset=BTC, free=4723846.89208129}, {locked=0.00000000, asset=LTC, free=4763368.68006011}], takerCommission=15.0, permissions=[SPOT], canTrade=true, makerCommission=15.0, updateTime=1.23456789E8, buyerCommission=0.0, accountType=SPOT, canWithdraw=true}]
6:37:32 AM Info 1.0
6:37:32 AM Info [[SPOT]]
6:37:34 AM Notice Execution completed
Note:
You can use Array.isArray(), to check if the variable is an array or not then read the data accordingly.
Sample:
if(Array.isArray(jsonResponce)){
for (i = 0; i < jsonResponce.length; i++) {
balanc = jsonResponce[i];
rows.push([balanc.accountType]);
}
}else{
rows.push([jsonResponce.accountType])
}
(UPDATE)
If you want to make the Binance API response into an array, you can append "[" and "]" on your data.
Sample:
var jsonData = {
"makerCommission": 15,
"takerCommission": 15,
"buyerCommission": 0,
"sellerCommission": 0,
"canTrade": true,
"canWithdraw": true,
"canDeposit": true,
"updateTime": 123456789,
"accountType": "SPOT",
"balances": [
{
"asset": "BTC",
"free": "4723846.89208129",
"locked": "0.00000000"
},
{
"asset": "LTC",
"free": "4763368.68006011",
"locked": "0.00000000"
}
],
"permissions": [
"SPOT"
]
};
var data = JSON.stringify(jsonData);
data = "["+data+"]";
var jsonResponce = JSON.parse(data); // To JSON
Logger.log(jsonResponce);
Logger.log(Array.isArray(jsonResponce))
Output:
12:21:48 AM Info [{makerCommission=15.0, canDeposit=true, balances=[{free=4723846.89208129, locked=0.00000000, asset=BTC}, {free=4763368.68006011, asset=LTC, locked=0.00000000}], takerCommission=15.0, updateTime=1.23456789E8, accountType=SPOT, canTrade=true, sellerCommission=0.0, buyerCommission=0.0, canWithdraw=true, permissions=[SPOT]}]
12:21:48 AM Info true