需要一只手来整理一下。 我有一个脚本(请参阅下文),可在Google表格中进行浏览,并检查项目是否接近到期日期或是否违反了到期日期。那部分正在工作。我需要做的是在接近或已破坏资源时向分配的资源发送电子邮件。这很简单,mailApp可以正常工作。但是我要做的是对信息进行分组,以免向用户的邮箱发送垃圾邮件。
代码:
function mailBot() {
var warningDays = 14;
var data = cwSheet.getDataRange().getValues();
data.slice(1).forEach(function(row) {
var user = row[0];
var userEmail = buildMailAddress(user);
var projectName = row[3];
var date = row[8];
var today = new Date();
if (
row[0] !== "" &&
row[0] !== "Unassigned" &&
row[0] !== "Proof of Concept"
) {
var dueDate = new Date(date);
if (dateDiff(dueDate, today) >= 1) {
// Logger.log("**BREACHED** | %s", row);
// TODO: Group results to send a single mail rather than multiple mails.
} else if (dateDiff(today, dueDate) <= warningDays) {
// Logger.log("**DUE** | %s", row);
// TODO: Group results to send a single mail rather than multiple mails.
}
}
});
}
日志输出:
[19-01-22 02:24:40:852 PST] **BREACHED** | [Darth Vader, Link, , Some project, , Project, Group, Shaping, Tue Jan 01 00:00:00 GMT+00:00 2019, Green, , , ]
[19-01-22 02:24:40:853 PST] **BREACHED** | [Darth Vader, Link, www.google.com, Some project, , Project, Department, Shaping, Fri Oct 26 00:00:00 GMT+01:00 2018, Green, , , ]
[19-01-22 02:24:40:855 PST] **BREACHED** | [Frodo Baggins, , , Test Due Date 1, , Project, JL, Development, Tue Jan 01 00:00:00 GMT+00:00 2019, , , , ]
[19-01-22 02:24:40:856 PST] **BREACHED** | [Frodo Baggins, , , Test Due Date 2, , Small Dev, Waitrose, Development, Tue Jan 01 00:00:00 GMT+00:00 2019, , , , ]
[19-01-22 02:24:40:857 PST] **BREACHED** | [Frodo Baggins, , , Test Due Date 3, , Project, Group, Development, Tue Jan 01 00:00:00 GMT+00:00 2019, , , , ]
[19-01-22 02:24:40:857 PST] **BREACHED** | [Frodo Baggins, , , Test Due Date 3, , Small Dev, I&S, Development, Mon Jan 14 00:00:00 GMT+00:00 2019, , , , ]
以上面的日志为例,Darth Vader将收到1封电子邮件,其中包含2条警报的内容。 Frodo将收到1封电子邮件,其中包含4个警报的内容。有人能指导我如何最好地实现这一目标吗?
更新1
给出以下我为适应GAS而调整的cen0r中的代码
function mailBot() {
var warningDays = 14;
var data = cwSheet.getDataRange().getValues();
var today = new Date();
// Parse the data using custom function
var parsed = parseData(data);
// Filter entries that are breached
var breached = parsed.filter(function(entry) {
return dateDiff(entry.date, today) >= 1;
});
// Group breached entries by user and do something with them
groupBy(breached, "user").map(function(group) {
// Here group is an array of entries for one user
// You can send one email for all breached entries here
});
// Filter entries that are due
var due = parsed.filter(
function(entry) {
return (
dateDiff(today, entry.date) <= warningDays &&
dateDiff(entry.date, today) < 1
);
} // This is so there won't be 2 emails sent for breached & due
);
// Group due entries by user and do something with them
groupBy(breached, "user").map(function(group) {
Logger.log(group)
// Here group is an array of entries for one user
// You can send one email for all due entries here
});
}
// Parses the data from your input
function parseData(data) {
// Parse the data
var parsed = data.slice(1).map(function(row) {
var user = row[0];
var userEmail = buildMailAddress(user);
var today = new Date();
var projectName = row[3];
var date = new Date(row[8]);
return {
user: user,
userEmail: userEmail,
projectName: projectName,
date: date,
today: today
};
});
// Filter entries that you want to do something with
return parsed.filter(function(entry) {
return (
entry.user !== "" &&
entry.user !== "Unassigned" &&
entry.user !== "Proof of Concept"
);
});
}
// Groups array entries according to a property of the entry (the key)
function groupBy(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
现在会产生错误:
TypeError:在对象[object Object]中找不到功能映射。 (线 25,文件“ 0-Main”)
哪个是这段代码:
groupBy(breached, "user").map(function(group) {
});
答案 0 :(得分:0)
基本上,该日志在foreach循环完成时可用,因此您需要在循环外创建一个对象(具有两个属性名称和消息数组),并将该日志信息填充到其中。填充时,您可以检查其中是否存在用户,然后在此循环之后可以轻松地向用户发送邮件。
否则您需要使用jquery库,我不确定您是否愿意使用它们。
答案 1 :(得分:0)
我建议首先返回一组已解析的数据,然后对其进行分组,最后发送电子邮件。请注意,我使用了一些ES6语法,如果这对您不起作用,我可以对其进行调整。像这样:
// Parses the data from your input
function parseData(data) {
// Parse the data
var parsed = data.slice(1).map(function(row) {
var user = row[0];
var userEmail = buildMailAddress(user);
var projectName = row[3];
var date = new Date(row[8]);
return {
"user": user,
"userEmail": userEmail,
"projectName": projectName,
"date": date,
"today": today
};
)};
// Filter entries that you want to do something with
return parsed.filter(entry =>
entry.user !== "" &&
entry.user !== "Unassigned" &&
entry.user !== "Proof of Concept"
);
};
// Groups array entries according to a property of the entry (the key)
function groupBy(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
function mailBot() {
var warningDays = 14;
var data = cwSheet.getDataRange().getValues();
var today = new Date();
// Parse the data using custom function
var parsed = parseData(data);
// Filter entries that are breached
var breached = parsed.filter(funciton(entry) {
return dateDiff(entry.date, today) >= 1;
});
// Group breached entries by user and do something with them
var breachedGroup = groupBy(breached,"user");
Object.keys(breachedGroup).forEach(function (key) {
var group = breachedGroup[key];
// Here group is an array of entries for one user
// You can send one email for all breached entries here
console.log(g[key]);
})
// Filter entries that are due
var due = parsed.filter(function(entry) {
return dateDiff(today, entry.date) <= warningDays &&
dateDiff(entry.date, today) < 1 // This is so there won't be 2 emails sent for breached & due
});
// Group due entries by user and do something with them
var dueGroup = groupBy(due,"user");
Object.keys(dueGroup).forEach(function (key) {
var group = dueGroup[key];
// Here group is an array of entries for one user
// You can send one email for all breached entries here
});
};