Poloniex APi to Google Sheet CSV via Json - json

I've got the following script that pulls keys from the Poloniex JSON output, but doesn't put the actual data that corresponds to the keys into the actual sheet...it only puts the keys as titles at the top of the sheet.
I'm new to API's, and GAS, and coding in general, so I'm sure I'm missing something incredibly obvious, I'd really appreciate it if you could point out what.
Thanks in advance
function Bitcoin_fromPolo_toCSV() {
//Link the script with a spreasdsheet using the identifier found in the spreadsheet url
var ss = SpreadsheetApp.openById('1cubxxxxxxxxxxxxjDqM');
var APIPullSheet = ss.getSheetByName("APIPull");
// Clear Columns A,B,C,D
APIPullSheet.getRange('A2:D19999').clearContent();
var url = "https://poloniex.com/public?command=returnChartData&currencyPair=BTC_ETH&start=1502344800&end=9999999999&period=14400";
//Fetch pulls data from URL
var responseAPI = UrlFetchApp.fetch(url);
//Parse that JSON
var parcedData = JSON.parse(responseAPI.getContentText());
//Break that Parsed data into fields
//Define the 'stats' array, and populate it from the parced data pulled
// for loop iterates over each 'key' in 'parcedData' pushing that data to 'stats'
var stats = [];
stats.push(['date','high', 'low', 'open', 'close', 'volume', 'quoteVolume', 'weightedAverage']);
for(var key in parcedData.stats)
{
stats.push(parcedData.stats[key]);
}
statsRange = APIPullSheet.getRange(1, 1, stats.length, 8);
statsRange.setValues(stats);
}

How about the following modification?
Modification points :
JSON data from URL is as follows.
[
{
"date": 1502352000,
"high": 0.0899,
"low": 0.08754124,
"open": 0.08795499,
"close": 0.08988724,
"volume": 1390.47552953,
"quoteVolume": 15727.49124739,
"weightedAverage": 0.08841051
},
.
.
]
parcedData doesn't have stats as a key.
Flow for creating data :
Outer forEach() retrieves an element from parcedData.
Inner forEach() retrieves each key from stats[0], and retrieves data from the element of parcedData using the key.
Retrieved data is imported to temp which is 1 dimensional array.
The temp is imported to stats which is 2 dimensional array. After this, temp is initialized.
The script reflected this is as follows.
Modified script :
function Bitcoin_fromPolo_toCSV() {
var ss = SpreadsheetApp.openById('1cubxxxxxxxxxxxxjDqM');
var APIPullSheet = ss.getSheetByName("APIPull");
APIPullSheet.getRange('A2:D19999').clearContent();
var url = "https://poloniex.com/public?command=returnChartData&currencyPair=BTC_ETH&start=1502344800&end=9999999999&period=14400";
var responseAPI = UrlFetchApp.fetch(url);
var parcedData = JSON.parse(responseAPI.getContentText());
var stats = [];
stats.push(['date','high', 'low', 'open', 'close', 'volume', 'quoteVolume', 'weightedAverage']);
parcedData.forEach(function(e1){
var temp = [];
stats[0].forEach(function(e2){
temp.push(e1[e2])
});
stats.push(temp);
});
statsRange = APIPullSheet.getRange(1, 1, stats.length, 8);
statsRange.setValues(stats);
}
Result :
If I misunderstand your question, I'm sorry.

Related

Google Apps Script. JSON to Spreadsheet - Result too long (API to Sheet) Work only with 100 reg

Good morning, I have made a Script that connects with an APi, it returns a JSON, I serialize it and complete an array with it .. then I paste it in a Google Spreadsheet, but it only works with 100 records. However if in the same Script, under the JSON my Google Drive makes it complete.
When I check the Logger it tells me the result is too long ...
My code.
function getOT() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('OT');
var url ='https://app.XXXXX.com/api/work_orders';
var authorization=XXXXXXApi(XXX,'app.XXXXXX.com','/api/work_orders','GET','443');
var Options ={
"url": "https://app.XXXXXX.com/api/work_orders",
"method": "GET",
"timeout": 0,
"headers": { "Authorization": "Hawk id=\""+authorization.cID+"\", ts=\""+ authorization.cTS+"\", nonce=\""+authorization.cNONCE+"\", mac=\""+authorization.cHMAC+"\"",
},
};
var response = UrlFetchApp.fetch('https://app.XXXX.com/api/work_orders', Options);
var array = [];
var CC = response.getContentText();
var datos = JSON.parse(CC);
Logger.log (CC)
for (var i = 0; i < datos.data.length ;i++) {
array.push([datos.data[i]['wo_folio']],[datos.data[i]['description']],[datos.data[i]['personnel_description']],[datos.data[i]['items_log_description']])
};
for (var i = 0; i < datos.data.length ;i++){
var startrow=2 + +i;
sheet.getRange(startrow, 1).setValue(datos.data[i]['wo_folio']);
sheet.getRange(startrow, 2).setValue(datos.data[i]['description']);
sheet.getRange(startrow, 3).setValue(datos.data[i]['personnel_description']);
sheet.getRange(startrow, 4).setValue(datos.data[i]['items_log_description']);
}
DriveApp.createFile('XX.csv', CC); //copio el JSON a mi drive
}
The Logging output too large. Truncating output. message is not an error, it's just a notice. It simply doesn't show the entire data, only a subset. This means that the data is actually there but is not shown to you when using a single call. Try logging the output inside a loop and print all the elements.
As for the TypeError: Cannot read property 'wo_folio' of undefined, it seems that this happens because you are modifying increasing the length property of datos.data:
const array = ['a', 'b']
array.length = 3
console.log(array) // outputs ['a', 'b', empty]
Notice that if I try to get the third element of array 2, you'll get undefined. In a similar way, datos.data[i]['wo_folio'] will become undefined['wo_folio'] if iterated outside the original length, and thus the error is thrown. And just so you know, changing an array length via its length property is rarely used.
I have put the size of the data, and I figure 100
var CC = response.getContentText();
var datos = JSON.parse(CC);
var registros=datos.data;
Logger.log(registros.length);
[21-01-03 00:19:21:398 ART] 100.0 .. but i have too many more...
If I run the query from postman, it has more than 300 records .. it only happens when I query it from GAS

Google scipts - cannot convert array to object[][]

I am trying to pull some information off poloniex.com and paste into a range in a google sheet and am running into issues when trying to set the values. I am pretty new at this and cannot tell if the issue is with my understanding of how setValues works or if it has to do with the way I'm pushing data into my prices array.
function processPoloAPI() {
var sheet = SpreadsheetApp.openById('<insert sheet id here>')
var APIPullSheet = sheet.getSheetByName("APIPull");
APIPullSheet.getRange('A2:D19999').clearContent();
var url = "https://poloniex.com/public?command=returnChartData&currencyPair=USDT_BTC&start=1405699200&end=9999999999&period=86400"
var responseAPI = UrlFetchApp.fetch(url)
var parcedData = JSON.parse(responseAPI.getContentText());
var prices = new Array ();
prices.push(['Date', 'High', 'Low', 'Open','Close', 'Volume', 'QuoteVolume', 'WeightedAverage'])
for(var key in parcedData)
{
prices.push(parcedData[key]);
}
var length = prices.length
askRange = APIPullSheet.getRange(1, 1, length, 8);
askRange.setValues(prices);
}
How about the following modification?
Modification points :
Data for setValues() is 2 dimensional array.
When it retrieves values using keys from JSON, the upper/lower cases for keys should be matched to JSON data.
In your script, prices is [[Date, High, Low, Open, Close, Volume, QuoteVolume, WeightedAverage], {date=1424304000, volume=46.27631267, high=244, low=225, weightedAverage=239.62777823, quoteVolume=0.19311748, close=244, open=225},,,. If you want to create data that each row only numbers for the keys at the top row, each number has to be retrieved from JSON data.
The modified script which was reflected in these modification points is as follows.
Modified script :
function processPoloAPI() {
var sheet = SpreadsheetApp.openById('<insert sheet id here>')
var APIPullSheet = sheet.getSheetByName("APIPull");
APIPullSheet.getRange('A2:D19999').clearContent();
var url = "https://poloniex.com/public?command=returnChartData&currencyPair=USDT_BTC&start=1405699200&end=9999999999&period=86400"
var responseAPI = UrlFetchApp.fetch(url)
var parcedData = JSON.parse(responseAPI.getContentText());
var prices = new Array ();
prices.push(['Date', 'High', 'Low', 'Open','Close', 'Volume', 'QuoteVolume', 'WeightedAverage'])
var keys = ['date', 'high', 'low', 'open', 'close', 'volume', 'quoteVolume', 'weightedAverage'];
for (var i in parcedData) {
var temp = [];
for (var j in keys) {
temp.push(parcedData[i][keys[j]]);
}
prices.push(temp);
}
var length = prices.length
askRange = APIPullSheet.getRange(1, 1, length, 8);
askRange.setValues(prices);
}
Result :
If I misunderstand your question, I'm sorry.

Use JIRA webhook data in Google Script Project

I am trying to get the following to work: A JSON is sent to my Google Script - Now I want to get specific values from that JSON Message and store them into specific rows of one specific Google Spreadsheet. So far so good, this is what I have :
function doPost(response) {
var sheets = SpreadsheetApp.openById('MY SHEET ID');
var dataAll = JSON.parse(response.getContentText());
var nR = getNextRow(sheets) + 1;
// RECORD DATA IN SPREADSHEET
sheets.getRangeByName('timestamp').getCell(nR,1).setValue(new Date());
sheets.getRangeByName('ticket_id').getCell(nR,1).setValue(dataAll);
}
function getNextRow(sheets) {
var timestamps = sheets.getRangeByName("timestamp").getValues();
for (i in timestamps) {
if(timestamps[i][0] == "") {
return Number(i);
break;
}}}
It should store the response and put it into a blank cell of the range "timestamp". But nothing happens at this point.
This is the JSON ( Body ) from JIRA:
{"timestamp":1483576902984,"webhookEvent":"jira:issue_created","issue_event_type_name":"issue_created","user":{"self":"https://xxx.atlassian.net/rest/api/2/user?username=admin","name":"admin","key":"admin","emailAddress":"test#mail.at","avatarUrls":{"48x48":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=48","24x24":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=24","16x16":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=16","32x32":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=32"},"displayName":"Max Mustermann [Administrator]","active":true,"timeZone":"Europe/Berlin"},"issue":{"id":"10057","self":"https://xxx.atlassian.net/rest/api/2/issue/10057","key":"TA-58","fields":{"issuetype":{"self":"https://xxx.atlassian.net/rest/api/2/issuetype/10104","id":"10104","description":"A problem which impairs or prevents the functions of the product.","iconUrl":"https://xxx.atlassian.net/secure/viewavatar?size=xsmall&avatarId=10303&avatarType=issuetype","name":"Bug","subtask":false,"avatarId":10303},"timespent":null,"project":{"self":"https://xxx.atlassian.net/rest/api/2/project/10000","id":"10000","key":"TA","name":"Test Area","avatarUrls":{"48x48":"https://xxx.atlassian.net/secure/projectavatar?avatarId=10324","24x24":"https://xxx.atlassian.net/secure/projectavatar?size=small&avatarId=10324","16x16":"https://xxx.atlassian.net/secure/projectavatar?size=xsmall&avatarId=10324","32x32":"https://xxx.atlassian.net/secure/projectavatar?size=medium&avatarId=10324"}},"customfield_10110":null,"fixVersions":[],"customfield_10111":null,"aggregatetimespent":null,"customfield_10112":"Not started","resolution":null,"customfield_10113":null,"customfield_10114":null,"customfield_10104":null,"customfield_10105":null,"customfield_10106":null,"customfield_10107":null,"customfield_10108":null,"customfield_10109":null,"resolutiondate":null,"workratio":-1,"lastViewed":null,"watches":{"self":"https://xxx.atlassian.net/rest/api/2/issue/TA-58/watchers","watchCount":0,"isWatching":false},"created":"2017-01-05T01:41:42.903+0100","priority":{"self":"https://xxx.atlassian.net/rest/api/2/priority/3","iconUrl":"https://xxx.atlassian.net/images/icons/priorities/medium.svg","name":"Medium","id":"3"},"customfield_10100":null,"customfield_10101":null,"customfield_10102":null,"customfield_10103":null,"labels":[],"timeestimate":null,"aggregatetimeoriginalestimate":null,"versions":[],"issuelinks":[],"assignee":null,"updated":"2017-01-05T01:41:42.903+0100","status":{"self":"https://xxx.atlassian.net/rest/api/2/status/10000","description":"","iconUrl":"https://xxx.atlassian.net/","name":"To Do","id":"10000","statusCategory":{"self":"https://xxx.atlassian.net/rest/api/2/statuscategory/2","id":2,"key":"new","colorName":"blue-gray","name":"To Do"}},"components":[],"timeoriginalestimate":null,"description":"super alles neu","timetracking":{},"customfield_10005":null,"attachment":[],"aggregatetimeestimate":null,"summary":"super alles neu","creator":{"self":"https://xxx.atlassian.net/rest/api/2/user?username=admin","name":"admin","key":"admin","emailAddress":"test#mail.at","avatarUrls":{"48x48":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=48","24x24":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=24","16x16":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=16","32x32":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=32"},"displayName":"Max Mustermann [Administrator]","active":true,"timeZone":"Europe/Berlin"},"subtasks":[],"reporter":{"self":"https://xxx.atlassian.net/rest/api/2/user?username=admin","name":"admin","key":"admin","emailAddress":"test#mail.at","avatarUrls":{"48x48":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=48","24x24":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=24","16x16":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=16","32x32":"https://secure.gravatar.com/avatar/3d238d8be45bd26982fa09ae2f891c3f?d=mm&s=32"},"displayName":"Max Mustermann [Administrator]","active":true,"timeZone":"Europe/Berlin"},"customfield_10000":"{}","aggregateprogress":{"progress":0,"total":0},"customfield_10001":null,"customfield_10115":null,"customfield_10116":"0|i0005r:","environment":null,"duedate":null,"progress":{"progress":0,"total":0},"comment":{"comments":[],"maxResults":0,"total":0,"startAt":0},"votes":{"self":"https://xxx.atlassian.net/rest/api/2/issue/TA-58/votes","votes":0,"hasVoted":false},"worklog":{"startAt":0,"maxResults":20,"total":0,"worklogs":[]}}}}
However, I don't want to have the whole JSON in my cell, I only want to have specific obejcts/id from within the JSON. How do I call them ?
After tons of research, this is a solution that works for me (in my case):
function doPost(response) {
var sheets = SpreadsheetApp.openById('SHEET_ID');
// retrieve data from JIRA Payload and store them into "data"
var json = response.postData.contents;
var data = JSON.parse(json);
// index values from "data" and store them into seperate variables
// for example:
var ticket_id = data.issue.key;
var priority_name = data.issue.fields.priority.name;
var summary = data.issue.fields.summary;
This two lines:
var json = response.postData.contents;
var data = JSON.parse(json);
Made it possible to read the body and index all the specific parameters I want.
Here is an example:
/*
* webhookHandler: JIRA webhook callback function
*/
function webhookHandler(response) {
var data = response.getAs("application/json");
//logs out data in dev console
console.log(data);
var spreadsheet = SpreadsheetApp.openById("<spreadsheet id>");
var cellRange = spreadsheet.getRangeByName("<some range name>");
var cell = cellRange.getCell(0 /*row index*/, 0/*column index*/);
cell.setValue(data.ticket_id/*index the JSON object returned by response*/);
}
UrlFetchApp Documentation
SpreadsheetApp Documentation

The coordinates or dimensions of the range are invalid in JSon

I am working on google scripts which can call REST API and get the data in google spreadsheets. But different Json objects work and which I am using now does not work...
At first it was giving me an error as The coordinates or dimensions of the range are invalid. So looked to on at
"The coordinates or dimensions of the range are invalid" - Google Apps Script and JSON Data
And now the result is undefined..
Really appreciate if someone can help
function pullJSON(k) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
var url = "********"; // Paste your JSON URL here
var headers = {
"Content-Type": "application/json",
};
var options = {
"method": "GET",
"headers": headers
};
var response = UrlFetchApp.fetch(url, options); // get feed
var dataAll = JSON.parse(response.getContentText()); //
var dataSet = dataAll;
Logger.log(dataAll.length);
Logger.log(k);
Logger.log(url);
var rows = [],
data;
for (var k = 0; k < Object.keys(dataSet).length; k++) {
data = [Object.keys(dataSet)[k]];
Logger.log(data);
rows.push([data.total1]);} //your JSON entities here
dataRange = sheet.getRange( 1, 1,rows.length,1); // 3 Denotes total number of entites
dataRange.setValues(rows);
}
What you are doing at the moment is calling Object.keys() on dataSet.
This returns an array of strings, e.g. ["status", "data"].
Then you retrieve each of these keys separately and assign them as a one element array to data, so data looks like
["status"], and ["data"].
Since ["total1]" is an array of a string it doesn't have the "total1" attribute, just an element with the value equal to the name.
To get the actual total 1 value from each object within data you can
dataSet.data.forEach(function(x) {
rows.push([x.total1]);
});
instead of the for loop.

"The coordinates or dimensions of the range are invalid"

I am working on a Google Apps Script that links with a REST API and puts the data into a Google Sheet.
I have successfully done this once, but upon accessing some different data I get the error message
"The coordinates or dimensions of the range are invalid"
when they work perfectly fine on my other script. All data accessed is JSON so I am bit confused and is from the same source. The code I am using is:
function stats () {
var logIn = {
"Authorization" : "Basic " + Utilities.base64Encode("XXXX" + ':' + "XXXX")
};
var url = "XXXXX";
var params = {
"method":"GET",
"headers":logIn, };
var response = UrlFetchApp.fetch(url, params);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("XXXX");
var dataAll = JSON.parse(response.getContentText()); //
var dataSet = dataAll;
var rows = [],
data;
for (i = 0; i < dataSet.length; i++) {
data = dataSet[i];
rows.push([XXXX]); //your JSON entities here
}
dataRange = sheet.getRange(1, 1, rows.length, 1);
dataRange.setValues(rows);
}
I have combined pieces of code from around the web and this works on my other script. The error appears on this line:
dataRange = sheet.getRange(1, 1, rows.length, 1);
I believe the issue is with the data I am accessing but I do not know how to alter the script for it to work.
The JSON data that works is shown like:
{
id: XXX,
group: XX,
text: "XXXX?",
creation_date: XXXX,
created_by: "XXXXX",
tags: [
"XXXX"
]
}
And the data that is causing the error is shown as:
{
2016-02-29: {
XXX: 0,
XXX: 0
},
I have had to 'XXXX' out a lot of the private information - apologies. Any help would be appreciated.
Javascript's length property is for indexed arrays and does not apply to Objects so dataSet.length returns undefined and the loop never executes.
To get the length of the object you can use Object.keys(dataSet).length as outlined here.