So far i managed to get a connection to a secure JSON. What i dont get to work is the data nice in columns. Plus in the metadata there a nice "labels" but now i get weird column names. Date: Streetname for example.
I have tried a couple of questions on this site. but i dont get it to work.
This is my JSON data (sorry it is in dutch)(so as you can see Project: Zipcode....?):
{
"skip": 0,
"take": 100,
"rows": [
{
"Volgnummer": 1,
"Omschrijving": "Projectnaam",
"Omschrijving_2": "Productnaam",
"Trailercodering": "productnaam-01",
"Omschrijving_3": "Warehouse",
"Datum_laden": "3 juni 2019",
"Tijdstip_laden": "1600 - 1800",
"Datum_aankomst_lossen": "4 juni 2019",
"Tijdstip_lossen": "0800 - 1000",
"Naam": "Transporteur",
"Datum": "Straat"
"Herkomst": huisnummer,
"Navigatie_transport": null,
"Project": "6644 KX",
"Woonplaats": "Ewijk",
"Land": "Nederland"
},
And this is my Google-Script code so far:
function pullJSON1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
var url= "https://the-url.com");
var headers = {
"Authorization": "AfasToken "+
Utilities.base64Encode(<token><version>1</version><data> hier de
token</data></token>)
};
var options = {
"method" : "get",
"headers" : headers
};
var response = UrlFetchApp.fetch(url,options); // get feed
var dataAll = JSON.parse(response.getContentText());
var rows = [Object.keys(dataAll)]; // Retrieve headers.
var temp = [];
for (var i = 0; i < rows[0].length; i++) {
temp.push(dataAll[rows[0][i]]); // Retrieve values.
}
rows.push(temp);
sheet.getRange(1,1,rows.length,rows[0].length).setValues(rows);
// Put values to Spreadsheet.
}
sheet output
(source: imggmi.com)
Can someone help?
I would this rearranged in columns. Also my output in the sheet gives me 1 entry but there a 356 enterys in total.
Great thanks,
Remco
Do you need the "skip", "take" variables? These don't fit with the tabular data, plus they seem to be part of a pagination system (in which case you should probably hide it from the results). Below I provide code to put the actual rows into your sheet:
function pullJSON1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
var url = "https://the-url.com";
var headers = {
"Authorization": "AfasToken " + Utilities.base64Encode("<token><version>1</version><data> hier de token</data></token>");
};
var options = {
"method" : "get",
"headers" : headers
};
var response = UrlFetchApp.fetch(url, options); // get feed
var dataAll = JSON.parse(response.getContentText());
var dataRows = dataAll['rows'];
var rowHeaders = Object.keys(dataRows[0]);
var rows = [rowHeaders]; // Retrieve headers.
for (var i = 0; i < dataRows.length; i++) {
var rowData = [];
for (var j = 0; j < rowHeaders.length; j++) {
rowData.push(dataRows[i][rowHeaders[j]]); // Retrieve values.
}
rows.push(rowData);
}
sheet.getRange(1,1,rows.length,rows[0].length).setValues(rows);
// Put values to Spreadsheet.
}
Related
I'm helping an educational project.
The following is the request:
the words-sentences in 1 row from a google spreadsheets list should be sent automatically to the discord text channal every day. The next day, a line below. When the whole list is finished, it should go back to the beginning and send it again. and write it on a new line after each column.
2nd request: same but this time 2 rows should be sent every day.
Number of columns Generally the same 2 or 3.
this is the code i found works, but that's not what i wanted. this code is for:"a range of cells".
How do I get it to send the next line every day? I will set the code to run once a day with Trigger from the menu. But how will it know which line it sent yesterday, etc.?
Unfortunately, I couldn't do exactly what I wanted. I will be glad if you help
(I'm an IT person, but I don't have any coding knowledge. I understand the code when I see it, but I can't write it.)
enter image description here
function postMessageToDiscord(message) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Loot");
var range = sheet.getRange("A1:C3");
var numRows = sheet.getLastRow()-1; // Number of rows to process
var data = range.getValues();
var result = '';
for (var i = 0; i < data.length; i++) {
var d = data[i];
for (var j = 0; j < d.length; j++) {
result = result.concat(d[j]);
}
}
message = message || result ;
var discordUrl = 'webhook xxx';
var payload = JSON.stringify({content: message});
var params = {
method: "POST",
payload: payload,
muteHttpExceptions: true,
contentType: "application/json"
};
var response = UrlFetchApp.fetch(discordUrl, params);
Logger.log(response.getContentText());
}
Edit:
function postMessageToDiscord(message) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("b1");
var propertyServ = PropertiesService.getScriptProperties();
var properties = propertyServ.getProperties(); //get script properties
var row = 1; // set initial row
var incRow = 4; // how much row
if(Object.getOwnPropertyNames(properties).length != 0){ //this will check if the properties object is not empty
row = parseInt(properties["row"]) + incRow; //increase row
}
var range = sheet.getRange(row, 1, incRow, 6);
var values = range.getValues();
var result = '';
for (var i = 0; i < values.length; i++) {
var d = values[i];
for (var j = 0; j < d.length; j++) {
result = result.concat(d[j]);
}
}
message = message || result ;
var discordUrl = 'https://discord.com/api/webhooks xxx';
var payload = JSON.stringify({content: message});
var params = {
method: "POST",
payload: payload,
muteHttpExceptions: true,
contentType: "application/json"
};
var response = UrlFetchApp.fetch(discordUrl, params);
Logger.log(response.getContentText());
propertyServ.setProperty("row", row); //save the current row of processed line
}
As mentioned by Cooper, you can use the Properties Service of Google Apps Script to save the processed range in your code.
Here I have an example which you can incorporate to your code.
EDIT
Revised code to process 3 rows and print each row per line.
Test Data:
Code:
function propertyServExample() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Loot");
var propertyServ = PropertiesService.getScriptProperties();
var properties = propertyServ.getProperties(); //get script properties
var row = 1; // set initial row
if(Object.getOwnPropertyNames(properties).length != 0){ //this will check if the properties object is not empty
row = parseInt(properties["row"]) + 3;
}
var range = sheet.getRange(row, 1, 3, 2);
var values = range.getValues();
var str = '';
values.forEach(val => {
str = str + val.join('') + '\n';
})
Logger.log(str);
propertyServ.setProperty("row", row); //save the current row of processed line
}
Each run will get the next row.
Run 1:
Run 2:
Run 3:
Reference:
Properties Service
For example, I have this range with 10 Drive hyperlinks to images:
This script already sends only one photo (F1) to a group via Telegram Bot, but I need to iterate for each cell in this range to send every uploaded images (via Google Forms, max. 10), but it could be only one or ten pictures, so I need to stop the iteration if is an empty cell like N1 and O1.
photo_url = "DRIVE_URL";
id = "GROUP_ID";
sendPhoto(id,photo_url)
function sendPhoto(id,photo_url) {
var API_TOKEN = "BOT_TOKEN";
var payload = {
'method': 'sendPhoto',
'chat_id': String(id),
'photo': photo_url,
'caption': "Foto 1"
}
var data = {
"method": "post",
"payload": payload,
'muteHttpExceptions':true,
}
//var response =
UrlFetchApp.fetch('https://api.telegram.org/bot' + API_TOKEN + '/', data);
//Logger.log(response);
}
*Plus, is it the same process to change the name or caption of every image? making this code dynamic:
'caption': "Foto 1"
'caption': "Foto 2"
'caption': "Foto 3" //...etc., until max. 10, sometimes is empty.
*Edited, this is the working code only to find data (URL photos in a Drive) inside a range of 10 cells (F2:O2):
function loopImage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("AWESOME");
var vals =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("AWESOME")
.getRange(2 +":"+ 2)
.getValues()[0],
lastColNum = vals.length
;
while(!vals.pop())
{ --lastColNum; }
var range = sh.getRange(2, 6, 2, lastColNum-5); //The command will automatically adjust the range based on the last column of the F2:O2 range.
var data = range.getValues();
var photoArr = [];
for(var i=0; i<data[0].length; i++){
photoArr.push({"type": "photo", "media": data[0][i], "caption": "Foto "+(i+1)})
}
if(photoArr.length > 0){
sendPhoto(JSON.stringify(photoArr));
}
}
function sendPhoto(photoArray) {
id = "GROUP_TOKEN";
var API_TOKEN = "BOT_API";
var payload = {
'method': 'sendMediaGroup',
'chat_id': String(id),
'media': photoArray,
}
var data = {
"method": "post",
"payload": payload,
"muteHttpExceptions":true,
}
var response = UrlFetchApp.fetch('https://api.telegram.org/bot' + API_TOKEN + '/', data);
Logger.log(response);
}
The script below will loop through the first row of Sheet starting from column F up to the last column with data, creates an array of object of InputMediaPhoto, and send the array to sendPhotoArray function to send it to a Telegram group chat.
Code:
function loopImage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Enter Sheet Name Here");
var range = sh.getRange(1, 6, 1, sh.getLastColumn()-5); //The command will automatically adjust the range based on the last column of the first row.
var data = range.getValues();
var photoArr = [];
for(var i=0; i<data[0].length; i++){
photoArr.push({"type": "photo", "media": data[0][i], "caption": "Foto "+(i+1)})
}
if(photoArr.length > 0){
sendPhoto(JSON.stringify(photoArr));
}
}
function sendPhoto(photoArray) {
id = "Insert Chat ID here";
var API_TOKEN = "Insert API TOKEN HERE";
var payload = {
'method': 'sendMediaGroup',
'chat_id': String(id),
'media': photoArray,
}
var data = {
"method": "post",
"payload": payload,
'muteHttpExceptions':true,
}
var response = UrlFetchApp.fetch('https://api.telegram.org/bot' + API_TOKEN + '/', data);
}
Sample Data:
Output in Telegram:
File Caption:
References:
Telegram sendMediaGroup
Class Range
I would like to know what I need to adjust in the script so that the results appear in the spreadsheet:
TypeError: Cannot read property '0' of undefined (line 7)
function Cartola() {
var url = "https://api.cartolafc.globo.com/mercado/destaques";
var response = UrlFetchApp.fetch(url);
var data = response.getContentText();
var result = JSON.parse(data);
var apelido = result.Atleta[0].apelido;
var foto = result.Atleta[0].foto;
var clube_nome = result.Atleta[0].clube_nome;
var posição = result.Atleta[0].posicao;
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clear()
var headerRow = ['apelido','foto','clube_nome','posição'];
sheet.appendRow(headerRow);
for(var i=0;i<result[0].Atleta;i++){
var row = [result.Atleta[i].apelido,result.Atleta[i].foto,result.Atleta[i].clube_nome,result.Atleta[i].posicao];
SpreadsheetApp.getActiveSheet().appendRow(row);
}
}
data is an array, so you need to access the element first:
var apelido = result[0].Atleta.apelido;
I think you're trying to print every player to spreadsheet, but what you've written is only looking at one of the players. Please first look at the data returned by the API and understand its structure. Looks something like this:
[
{
"Atleta": {
"atleta_id": 68952,
"nome": "Mário Sérgio Santos Costa",
"apelido": "Marinho",
"foto": "https://s.glbimg.com/es/sde/f/2019/05/30/cd8a7f9b0744e105efa0a0c572d37d6f_FORMATO.png",
"preco_editorial": 5
},
"escalacoes": 996124,
"clube": "SAN",
"clube_nome": "Santos",
"clube_id": 277,
"escudo_clube": "https://s.glbimg.com/es/sde/f/organizacoes/2014/04/14/santos_60x60.png",
"posicao": "Atacante",
"posicao_abreviacao": "ATA"
}
]
Once you understand the data, then consider this modified script, which uses batch operations to run much more quickly.
function Cartola() {
var url = 'https://api.cartolafc.globo.com/mercado/destaques';
var response = UrlFetchApp.fetch(url);
var results = JSON.parse(response.getContentText());
var table = [['apelido','foto','clube_nome','posição']];
for (var i = 0; i < results.length; i++) {
var r = results[i];
var apelido = r.Atleta.apelido;
var foto = r.Atleta.foto;
var clube_nome = r.clube_nome;
var posição = r.posicao;
table.push([apelido, foto, clube_nome, posição]);
}
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clear().getRange(1, 1, table.length, table[0].length).setValues(table);
}
I'm sending a request to an API (in a Google Scripts), and I'm getting the response as a JSON text that looks like this:
[{"id":26319355,"name":"1. WAW -FIRST SESION","calendar":"Glovers
Click&Collect","duration":30,"isSeries":false,"slots":90,"slotsAvailable"
:89,"color":"#E3DE7D","price":"0.00","category":"WAW","description":"",
"calendarID":2978881,"serviceGroupID":2978881,"appointmentTypeID":10104780,
"calendarTimezone":"Europe\/Madrid","time":"2019-06-01T12:00:00+0200",
"localeTime":"June 1, 2019 12:00"},
{"id":26466803,"name":"1. WAW -FIRST SESION","calendar":"Glovers
Click&Collect","duration":30,"isSeries":false,"slots":90,"slotsAvailable"
:89,"color":"#E3DE7D","price":"0.00","category":"WAW","description":"",
"calendarID":2978881,"serviceGroupID":2978881,"appointmentTypeID":10104780,
"calendarTimezone":"Europe\/Madrid","time":"2019-06-07T14:00:00+0200",
"localeTime":"June 7, 2019 14:00"},
I want to paste this response as a table in my spreadsheet.
My script actually looks like this (where response is the response I get from the API request):
function CheckAv(row,acuityid,check,url,apiusername,apisecretkey,ss) {
var header = {
"contentType": "application/json",
"headers":{"Authorization" : " Basic " + Utilities.base64Encode(apiusername + ":" + apisecretkey)},
"method" : "GET"
}
muteHttpExceptions: true
var response = UrlFetchApp.fetch(url, header);
var data = JSON.parse(response.getContentText());
var text = response.getResponseCode();
Logger.log(text);
}
I assume it will be really easy but I can't find the solution.
You can cycle through your JSON structure and push each key and value to a specified row using the code below.
json = [{your: "JSON", data: "goes"}, {in : "here"}]
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
var rows = [],
data;
for (i = 0; i < json.length; i++) {
for (j in json[i]) {
dataq = json[i][j];
Logger.log(dataq);
rows.push([j, dataq]);
}
dataRange = sheet.getRange(1, 1, rows.length, 2);
dataRange.setValues(rows);
}
Google Apps Script to pull data from API. I'd like to parse out the information according to the relevant headers.
function Fraud2() {
var ret = "no value";
var response = UrlFetchApp.fetch("https://fraudshield.24metrics.com/api/v1/reports/fraud.json?tracker_id=905&group[]=sub_id&group[]=partner&date_start=2018-01-18&date_end=2018-01-18&timezone=UTC&user_id=XXX&api_token=XXX",{muteHttpExceptions:true})
var user_id = "XXX";
var api_token = "XXX";
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow([response]);
}
The return is pushed into one single cell like so:
{"results":[{"tracker_id":905,"conversion":7883,"click":0,"goal":0,"approved":6511,"rejected":1372,"tracker":"Tatoo
Integration","conversion_rate":"N\/A"},{"tracker_id":906,"conversion":1868,"click":0,"goal":0,"approved":1682,"rejected":186,"tracker":"Aise
Integration","conversion_rate":"N\/A"},{"tracker_id":933,"conversion":413,"click":0,"goal":0,"rejected":290,"approved":123,"tracker":"Tatoo
Invalids Integration","conversion_rate":"N\/A"}]}
I tried this without success.
How can I get the results arranged neatly into columns?
As Hink said, you need to convert the object to an array first.
Hinks solution will work fine but below is a combination of the code you tried to use in your post.
function Fraud2() {
var ss = SpreadsheetApp.getActiveSheet();
var ret = "no value";
var response = UrlFetchApp.fetch("https://fraudshield.24metrics.com/api/v1/reports/fraud.json?tracker_id=905&group[]=sub_id&group[]=partner&date_start=2018-01-18&date_end=2018-01-18&timezone=UTC&user_id=XXX&api_token=XXX",{muteHttpExceptions:true})
var user_id = "XXX";
var api_token = "XXX";
var out=JSON.stringify(response.results);
out=JSON.parse(out)
var title = [];
for (var i in out[0]) {
title.push(i);
}
var res = [];
res.push(title);
for (var i in out) {
var values = [];
for (var j in out[i]) {
values.push(out[i][j]);
}
res.push(values);
}
ss.getRange(1, 1, res.length, res[0].length).setValues(res);
};
You need to convert the response object into an array and append the array:
for (var i = 0; i < response.results.length; i++){
var current = response.results[i];
var myArray = [current.tracker_id, current.conversion, current.click, current.goal, current.approved, current.rejected, current.tracker,current.conversion_rate];
sheet.appendRow(myArray);
}