Help with Employees call for Google Script: Solved | API Devel…

Help with Employees call for Google Script: Solved

    • HOU5E [2337920]
    • Role: Civilian
    • Level: 15
    • Posts: 1,606
    • Karma: 1,210
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Thread created on 06:22:38 - 04/02/24 (7 months ago)
    |
    Last replied 09:12:02 - 06/02/24 (7 months ago)
    I'm trying to modify this code to work for me:


    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];
    }

    It works in google scripts:



    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:

    /** 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];
    }
    I get an error that saying...

    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!
    Last edited by HOU5E on 09:11:01 - 06/02/24
    • mavri [2402357]
    • Role: Civilian
    • Level: 98
    • Posts: 1,021
    • Karma: 2,334
    • Last Action: 3 minutes
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 10:54:32 - 04/02/24 (7 months ago)
    Post link copied to clipboard Copy post link
    Try logging the JSON you get from Torn. I'm not a company director so can't test myself, but I suspect you're not getting what you're expecting to get.

    mavri buys your stuff :P

    • Kwack [2190604]
    • Role: Civilian
    • Level: 15
    • Posts: 1,865
    • Karma: 2,838
    • Last Action: 5 hours
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 06:45:07 - 05/02/24 (7 months ago)
    Post link copied to clipboard Copy post link
    Why have you got selections on their you don't seem to need as well? I assume you only need company/employees.

    Your response will look like the following:


    So you'd need to something like:
    const employeeData = Object.entries(data["company_employees"]).map(([id, user]) => [id, user.name, user.position, user.days_in_company, .......]);
    To get the data in the format you wanted.
    Then you'd return it with the headers:
    return [["ID", "Name", "Position", "Days in Company", .......], ...employeeData]
    Where ....... is the extra info you'd be missing.

    I haven't tested this so I may have put a typo in it or something, but it would look like this (before adding extra headers)

    Last edited by Kwack on 06:45:31 - 05/02/24
    • Untouchable [1360035]
    • Role: Committee
    • Level: 100
    • Posts: 5,460
    • Karma: 7,457
    • Last Action: 52 minutes
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 23:29:33 - 05/02/24 (7 months ago)
    Post link copied to clipboard Copy post link

    HOU5E [2337920]

    I'm trying to modify this code to work for me:


    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];
    }

    It works in google scripts:



    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:

    /** 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];
    }
    I get an error that saying...

    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!
    This looks like an async/await problem to me, your function isn't async, so when it tries to parse the JSON, it's probably still undefined.

    untouchableforumsoftware.png

    • HOU5E [2337920]
    • Role: Civilian
    • Level: 15
    • Posts: 1,606
    • Karma: 1,210
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 09:06:35 - 06/02/24 (7 months ago)
    Post link copied to clipboard Copy post link
    Figured it 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
    • HOU5E [2337920]
    • Role: Civilian
    • Level: 15
    • Posts: 1,606
    • Karma: 1,210
    • Last Action: 1 hour
      • 0
    • Reason:
      Are you sure you want to report this post to staff?
      Cancel
    Posted on 09:12:02 - 06/02/24 (7 months ago)
    Post link copied to clipboard Copy post link

    Kwack [2190604]

    Why have you got selections on their you don't seem to need as well? I assume you only need company/employees.

    Your response will look like the following:


    So you'd need to something like:
    const employeeData = Object.entries(data["company_employees"]).map(([id, user]) => [id, user.name, user.position, user.days_in_company, .......]);
    To get the data in the format you wanted.
    Then you'd return it with the headers:
    return [["ID", "Name", "Position", "Days in Company", .......], ...employeeData]
    Where ....... is the extra info you'd be missing.

    I haven't tested this so I may have put a typo in it or something, but it would look like this (before adding extra headers)

    Kwack you are dead right! Thanks
Reply
Thread Title: