0

I am working on getting total time entries from via external API. The function needs to get user data which includes id saved in an array. This ID needs to be called by the second function to get hours for a data range and total them up and also save them in an array. The Systems API is in https://github.com/10Kft/10kft-api

This my first function to get user data.

function get_users() {
    // function loops through 10000ft  paginations and scrapes user data to array
    var userarray = [];        
    var lastpage  = false;
    var page      = 1;

    do{        
        // gets 10kft data
        var users = read10k_users(page);            
        // writes data from current page to array

        for (i in users.data) {
            var rec     = {};
            // pushing of mandatory data
            rec.id      = users.data[i].id;
            rec.display_name = users.data[i].display_name;
            rec.email   = users.data[i].email;                
            userarray.push(rec);

        }

        Logger.log(userarray)
        // checks if this is the last page (indicated by paging next page link beeing null
        if (users.paging.next != null) {
            lastpage = false;
            var page = page + 1;
        } else {
            lastpage = true;
        }

    }while (lastpage == false);

    return (userarray);

}

My second function needs to reference id saved in the array in order to return total hours for specific users and this is where am lost.

function get_timedata(id) {
    // function loops through 10000ft  paginations and scrapes time entries to array
    var timearray = [];        
    var lastpage  = false;
    var page      = 1;

    do{

        // gets time data from  10000ft 
        var timedata = read10k_timedata(from_dt,to_dt,); 

        // writes data from current page to array

        for (i in timedata.data) {
            var rec       = {};
            // collection of time data from time entries endpoint
            rec.id        = timedata.data[i].id;
            rec.user      = timedata.data[i].user_id;
            rec.hours_inc = timedata.data[i].hours;                 

            // pushing  incurred hours      
            if (rec.hours_inc >= 0 ) {
                timearray.push(rec);                
            }
            //Logger.log(timearray)
        }

        // checks if this is the last page (indicated by paging next page link beeing null
        if (timedata.paging.next != null) {
            lastpage = false;
            var page = page + 1;
        } else {
            lastpage = true;
        }

    }while (lastpage == false);        
    return (timearray);

}

Any help would be much appreciated. Thanks in advance

2
  • 1
    You either need to call one of the functions from the other, passing the array to it OR record the array somewhere (Google Sheet for example) then grab it from there at the start of the 2nd script. Commented Jan 24, 2020 at 8:57
  • @ross, Thanks for this. I am trying to avoid using G-sheet in this case. Is there an alternative to Gsheet ? Commented Jan 24, 2020 at 11:41

1 Answer 1

2

If I understand you correctly, you want to:

  • Store an array of JavaScript objects returned by a certain function.
  • Use this stored array in a function that you will use in the future.

If that's correct, you can use PropertiesService to store your data. This tool allows scripts to store simple data in key-value pairs.

Now, you can only store strings and you want to store an array of JS objects. So in order to store this to the script property, you would first have to use JSON.stringify(), which converts a JavaScript object or value to a JSON string.

Later, when you want to use the data, you would just have to use JSON.parse() to convert the string back to an array of objects.

Workflow:

So summing this up, in get_users convert userarray to a JSON string and store it as a script property via setProperty(key, value), like this:

var value = JSON.stringify(userarray);
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty("your-key", value);

Then, whenever you want to retrieve that data (in get_timedata or in another function that will call this one), retrieve the property via getProperty(key), and convert it to an array of objects, like this:

var scriptProperties = PropertiesService.getScriptProperties();
var value = scriptProperties.getProperty("your-key");
var object = JSON.parse(value);

Now you can access the different ids in the array as you would with the original array (e.g. object[0]["id"]).

Reference:

I hope this is of any help.

Sign up to request clarification or add additional context in comments.

1 Comment

But, I found out that the string has a limitation of size. If the array is around 80,000 rows and 10 columns, it gives an error "String too large". Can the array be defined as a global variable in the calling function, and can it be used in another function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.