GAS/ Javascript:将 API 保存为响应,对它们进行计算,如果它们满足条件,则发送电子邮件

GAS/ Javascript: Save API as a response perform calculations on them and if they meet condition send emails

我正在做一个个人项目,尝试扩展 10K 英尺项目管理系统的限制 API 并且是 GAS 的新手或 Javascript 我将感谢对我正在尝试的代码的帮助去做。 API 文档可在 https://github.com/10Kft/10kft-api.

找到

使用时间条目和用户端点,我想遍历所有用户并获取他们在特定时间范围内的时间条目。我希望将这些数据保存为一个数组,并添加时间条目(小时)以获得总计。如果出于某种原因,特定用户时间条目低于 3 小时,则会向用户发送一封电子邮件,通知 him/her 完成时间表。我在某个时候迷路了。这是我目前的代码:任何擅长此的人,请帮忙。

function getTime() {
    var range = [5323, 9626, 4998];
    var user = [];
    for (var i = 0; i < range.length; i++) {

        var auth = 'xxxxxxxx=';

        var from = '2020-01-08'
        var to = '2020-01-09'
        var url = 'https://api.10000ft.com/api/v1/users/' + range[i] + '/time_entries?from=' + from + '&to=' + to + '&auth=' + auth;

        var options = {
            method: 'get',
            headers: {
                Authorization: 'Bearer ' + auth
            }
        };
        var response = UrlFetchApp.fetch(url, options);
        Logger.log(response.getContentText());
        var user_data = response.getContentText();
        user_data.foreach(function(data) {
        var total_hours = sum.reduce(data.hours);
        })

        var array = [];

        return array;
    }}
  1. 您需要解析响应。
  2. 遍历用户条目。
  3. 根据用户 ID 聚合它们。
  4. 循环聚合。
  5. 有条件地发送邮件。

类似这样的事情:

var submitted_time_entries = {};

var response = UrlFetchApp.fetch(url, options);
var response = JSON.parse(response.getContentText());
var time_entries = response.data;

time_entries.foreach(function(time_entry) {
    if (time_entry.user_id in submitted_time_entries) {
        submitted_time_entries[time_entry.user_id] += time_entry.hours;
    } else {
        submitted_time_entries[time_entry.user_id] = time_entry.hours;
    }
});

submitted_time_entries.forEach(function(user_id) {
    if (submitted_time_entries[user_id] < 3) {
        //send mail
    }
});