It works in google scripts:function getUsersDemo() {
var url = 'https://jsonplaceholder.typicode.com/users';
var response = UrlFetchApp.fetch(url);
var jsonText = response.getContentText();
var data = JSON.parse(jsonText);
let users = data.map(user => [user.name, user.email]);
return [['Name', 'Email'], ...users];
}
But when I try it to implement it with Torn's API it fails! Which is so frustrating because it's not complicated!
Here is my implementation:
I get an error that saying.../** START of getEmployees function */
function getEmployees() {
/** Pull the API Key from the "TWR_Oil_API_Keys.gs" */
var apiKey = apiKeyTWR2
/** Building the URL */
var urlBase = "https://api.torn.com/company/?selections=applications,detailed,profile,employees,stock,timestamp";
var urlKey = "&key=";
var urlComment = "&comment=gSheetsBullshit";
var url = urlBase + urlComment + urlKey + apiKey;
/** Call URL and parse API data pulled. */
var json = UrlFetchApp.fetch(url).getContentText();
var data = JSON.parse(json);
let users = data.map(user => [user.name, user.position, user.days_in_company, user.wage, user.manual_labor, user.intelligence, user.endurance, user.effectiveness.working_stats, user.effectiveness.settled_in, user.effectiveness.merits, user.effectiveness.director_education, user.effectiveness.addiction, user.effectiveness.total, user.last_action.timestamp]);
return [['Name', 'Position', 'days_in_company', 'Wage', 'Manual Labour', 'Intelligence', 'Endurance', 'Working Stats', 'Settled In', 'Merits', 'Director Education', 'Addiction', 'Total Effectiveness', 'Last Action'], ...users];
}
users:undefined
Well it's not defined any differently in the working script. What the f**k?!
--------------------------------------------------------------------------------------
Edit: Solved!
Figured out it was a data-type issue.
APIs can serve arrays or objects. To spot the difference note if the API call begins with a curly bracket or square bracket to spot the difference.
The example code expected arrays, the map function only works on arrays but the torn API serves objects.
Solution:
https://pastebin.com/raw/eKhCkG0j
Buddy helped me with it and did that little flourish to get the user id too. :D
Looks like Kwack was dead right too! Nice, thanks!