Warm tip: This article is reproduced from stackoverflow.com, please click
arrays google-apps-script google-sheets loops

How to map and send emails to specific users who don't meet a certain criteria in Javascript/GAS

发布于 2020-04-03 23:37:02

I am new in Javascript and bit by bit I have used resources here on StackOverflow to build on a project that uses external API to get time entries for users from the 10k ft project management system. I have finally have different functions together as follows:

  • Calls for user details which includes user_id
  • Get the time entries and sums up for every user who's approval has a value (pending or approval) in a specific date range. Those without approval will be ignored in the summation and their total entries left at 0.

  • My challenge now is to have only those with 0 as total hours of time entries receive emails to update their time entries. This code doesn't seem to select only those with 0 and send emails specifically to them. I will appreciate any pointers and/or assistance. after sending the email, this should be recorded on Google sheet


var TKF_URL = 'https://api.10000ft.com/api/v1/';
var TKF_AUTH = 'auth'
var TKF_PGSZ = 2500
var from = '2020-01-20'
var to = '2020-01-26'
var options = {
    method: 'get',
    headers: {
        Authorization: 'Bearer ' + TKF_AUTH
    }
};

function getUsers() {
    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 (var 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);
        }

        // 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);


    return (userarray);

}

function read10k_users(page) {

    var endpoint = 'users?';

    var url = TKF_URL + endpoint + 'per_page=' + TKF_PGSZ + '&auth=' + TKF_AUTH + '&page=' + page;
    var response = UrlFetchApp.fetch(url, options);
    var json = JSON.parse(response);

    //Logger.log(json.data)
    return (json);
}


function showTimeData() {

    var users = getUsers()

    var time_array = [];


    for (var i = 0; i < users.length; i++) {

        // Logger.log(users[i].id)
        var url = 'https://api.10000ft.com/api/v1/users/' + users[i].id + '/time_entries?fields=approvals' + '&from=' + from + '&to=' + to + '&auth=' + TKF_AUTH + '&per_page=' + TKF_PGSZ;
        var response = UrlFetchApp.fetch(url, options);
        var info = JSON.parse(response.getContentText());
        var content = info.data;
        var total_hours = 0;




        for (var j = 0; j < content.length; j++) {
        if (content[j].approvals.data.length > 0) {
                total_hours += content[j].hours;

            }
        }
        Logger.log('User name: ' + users[i].display_name + ' ' + 'User id: ' + users[i].id + '  ' + 'total hours: ' + total_hours+ '  ' + 'Email: ' + users[i].email)
    }

}



function sendMail(showTimeData){

     var emailAddress = user.email;
     var message = 'Dear ' + user.display_name + 'Please update your details in the system'
     var subject = ' Reminder';

     MailApp.sendEmail(emailAddress, subject, message);

} 
Questioner
Just
Viewed
116
Just 2020-01-31 19:44

I was able to get a solution for this as follows:

for (var j = 0; j < content.length; j++) {
    if (content[j].approvals.data.length > 0) {
        total_hours += content[j].hours;

    }
}

Logger.log('User name: ' + users[i].display_name + ' ' + 'User id: ' + users[i].id + '  ' + 'total hours: ' + total_hours + '  ' + 'Email: ' + users[i].email)

if (total_hours == 0) {


    sendMail(users[i])
}
}

}


function sendMail(user) {

    var emailAddress = user.email;
    var message = 'Dear ' + user.display_name + 'Please update your details in the system'
    var subject = ' Reminder';
    MailApp.sendEmail(emailAddress, subject, message);
}