温馨提示:本文翻译自stackoverflow.com,查看原文请点击:arrays - How to map and send emails to specific users who don't meet a certain criteria in Javascript/GAS
arrays google-apps-script google-sheets loops

arrays - 如何将电子邮件映射并发送给不符合Javascript / GAS中特定条件的特定用户

发布于 2020-04-03 23:56:46

我是Java语言的新手,我在StackOverflow上使用了一些资源来构建一个使用外部API从10k ft项目管理系统获取用户时间条目的项目。我最终具有以下不同的功能:

  • 要求提供包括user_id在内的用户详细信息
  • 获取时间条目并汇总每个批准的用户在特定日期范围内的值(待定或批准)的时间。那些未经批准的变量将被忽略,并且其总条目保留为0。

  • 我现在的挑战是,只有总时间为0的那些人才能收到电子邮件以更新其时间条目。这段代码似乎并不仅选择那些0,而且专门向他们发送电子邮件。我将不胜感激任何指示和/或帮助。发送电子邮件后,应将其记录在Google工作表中


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

} 

查看更多

提问者
Just
被浏览
149
Just 2020-01-31 19:44

我能够为此找到一个解决方案,如下所示:

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

最新帖子

最新文章