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);
}
}
Logger.log(data)afterresultand then check view logs to see what is the output of that object. Maybe it is already a json object and you don't need to parse it. - soMariodatato your question by loggingconsole.log(data)- TheMaster.getContentText()returns only one type:string- TheMasterresponseis always of type:object, specifically of classHttpResponse. There are no variants. More Specifically, When dealing with json,getContentText()is always needed and it returns a json string and you always need to parse it. - TheMaster