我如何使用Google Apps脚本将标签应用于我回复的收件箱中的所有电子邮件?
我正在查看Gmail的过滤器,但无法弄清楚如何构建此过滤器。
答案 0 :(得分:1)
这个示例脚本怎么样?虽然我找到了这种情况的示例脚本,但不幸的是我也找不到。所以我创建了这个尝试,因为我也想使用这个脚本。该脚本的概念如下:
我从上面的概念中准备了一个示例脚本。
要使用此示例,请输入label
。运行myFunction()
后,label
会添加到所有者在收件箱中回复的邮件中。
function myFunction() {
var label = "temp"; // Please input a label you want to use.
var threadId = "";
var thread = GmailApp.getInboxThreads();
thread.forEach(function(th) {
th.getMessages().forEach(function(msg) {
var mm = msg.getThread().getMessages();
if (mm.length > 0) {
var temp = [];
mm.forEach(function(m) {
var re = /<(\w.+)>/g;
var from = m.getFrom();
var to = m.getTo();
temp.push({
from: from.match(re) ? re.exec(from)[1] : from,
to: to.match(re) ? re.exec(to)[1] : to
});
});
if (temp.length > 1 && threadId != th.getId()) {
if (temp.filter(function(e){return temp[0].from == e.to}).length > 0) {
var rr = th.addLabel(GmailApp.getUserLabelByName(label));
Logger.log("Label '%s' was added to threadId %s.", label, rr.getId())
}
threadId = th.getId();
}
}
});
});
}
var th = GmailApp.getInboxThreads(0, 50);
进行测试,并了解该流程。请在脚本上方运行。如果我误解了你的问题,我很抱歉。
要使用此示例脚本,请在Gmail API和API控制台启用Advanced Google Services。它的流程如下。
如果您现在使用使用Gmail API的脚本打开脚本编辑器,则可以通过访问此网址https://console.cloud.google.com/apis/api/gmail.googleapis.com/overview
为项目启用Gmail API此脚本仅为回复的电子邮件添加标签。 addLabel
GmailApp仅为该帖添加标签。这不能将标签添加到线程中的特定消息。所以我使用了Gmail API。使用此脚本时,请为您的环境修改label
,userId
和thread
。
function myFunction() {
var label = "#####"; // Please input a label you want to use.
var userId = "me"; // If you use this script by yourself, userId is "me".
var thread = GmailApp.getInboxThreads(); // In this setting, all mails in Inbox are retrieved. If you want to use the specific mails, please modify here.
var threadId = "";
thread.forEach(function(th) {
th.getMessages().forEach(function(msg) {
var mm = msg.getThread().getMessages();
if (mm.length > 0) {
var temp = [];
mm.forEach(function(m) {
var re = /<(\w.+)>/g;
var from = m.getFrom();
var to = m.getTo();
temp.push({
from: from.match(re) ? re.exec(from)[1] : from,
to: to.match(re) ? re.exec(to)[1] : to,
threadId: th.getId(),
messageId: m.getId()
});
});
if (temp.length > 1 && threadId != th.getId()) {
if (temp.filter(function(e){return temp[0].from == e.to}).length > 0) {
var receivedFrom = temp.filter(function(e){return e.threadId == e.messageId})[0].from;
temp.filter(function(e){return e.to == receivedFrom}).forEach(function(e){
Gmail.Users.Messages.modify(
{"addLabelIds": [Gmail.Users.Labels.list(userId).labels.filter(function(e){return e.name == label})[0].id]},
userId,
e.messageId
);
Logger.log("Label '%s' was added to messageId '%s'.", label, e.messageId)
});
}
threadId = th.getId();
}
}
});
});
}
var thread = GmailApp.getInboxThreads();
来检索线程。这意味着将检索收件箱中的所有线程。因此,如果要检索特定的线程,请修改此项。此修改过的脚本的流程如下:
function myFunction() {
var label = "#####"; // Please input a label you want to use.
var userId = "me"; // If you use this script by yourself, userId is "me".
var thread = GmailApp.getInboxThreads(); // In this setting, all mails in Inbox are retrieved. If you want to use the specific mails, please modify here.
var threadId = "";
thread.forEach(function(th) {
th.getMessages().forEach(function(msg) {
var mm = msg.getThread().getMessages();
if (mm.length > 0) {
var temp = [];
mm.forEach(function(m) {
var re = /<(\w.+)>/g;
var from = m.getFrom();
var to = m.getTo();
temp.push({
from: from.match(re) ? re.exec(from)[1] : from,
to: to.match(re) ? re.exec(to)[1] : to,
threadId: th.getId(),
messageId: m.getId()
});
});
if (temp.length > 1 && threadId != th.getId()) {
if (temp.filter(function(e){return temp[0].from == e.to}).length > 0) {
var receivedFrom = temp.filter(function(e){return e.threadId == e.messageId})[0].from;
if (temp.filter(function(e){return e.to == receivedFrom}).length > 0) {
temp.forEach(function(e, i){
if (i > 0) {
Gmail.Users.Messages.modify(
{"addLabelIds": [Gmail.Users.Labels.list(userId).labels.filter(function(e){return e.name == label})[0].id]},
userId,
e.messageId
);
Logger.log("Label '%s' was added to messageId '%s'.", label, e.messageId)
}
});
}
}
threadId = th.getId();
}
}
});
});
}
- A向我发送电子邮件。
- 我回复并将此电子邮件保留在我的收件箱中。
- A回应我的回复。
- A的回复是第二封电子邮件,因为会话视图已关闭。
醇>我不打算再回复他4日发来的电子邮件。 我正在运行脚本。 1.应标记但不应标记为
我可以知道你想在上面的情况下将标签添加到2。在这种情况下,我的第二个样本工作正常。但你说这个脚本不是你想要的。我认为线程ID和消息ID之间的关系可能与我的考虑不同。所以我准备了一个示例脚本,用于检索线程ID中的消息ID。在使用此脚本之前,请在您的方案之上执行操作。在此之后,请运行此脚本,并检索消息ID和线程ID。请告诉我您要添加标签的结果和消息ID。
此脚本检索线程ID中的消息。检索messageId,from,to和subject以获取每条消息的信息。
function myFunction() {
var result = [];
var thread = GmailApp.getInboxThreads();
var threadId = "";
thread.forEach(function(th) {
th.getMessages().forEach(function(msg) {
var mm = msg.getThread().getMessages();
if (mm.length > 1) {
var ids = {threadId: msg.getThread().getId()}
var mids = [];
mm.forEach(function(m) {
mids.push({
messageId: m.getId(),
from: m.getFrom(),
to: m.getTo(),
subject: m.getSubject()
});
});
ids.message = mids;
if (threadId != th.getId()) {
result.push(ids);
}
}
threadId = th.getId();
});
});
Logger.log(JSON.stringify(result))
}
此脚本为已回复的邮件添加标签。此脚本假设以下情况。
对于上述情况,此脚本仅向消息&#34; 2&#34;。
添加标签function myFunction() {
var label = "temp"; // Please input a label you want to use.
var userId = "me"; // If you use this script by yourself, userId is "me".
var thread = GmailApp.getInboxThreads(); // In this setting, all mails in Inbox are retrieved. If you want to use the special mails, please modify here.
var threadId = "";
thread.forEach(function(th) {
th.getMessages().forEach(function(msg) {
var mm = msg.getThread().getMessages();
if (mm.length > 1) {
var temp = [];
mm.forEach(function(m) {
var re = /<(\w.+)>/g;
var from = m.getFrom();
var to = m.getTo();
temp.push({
from: from.match(re) ? re.exec(from)[1] : from,
to: to.match(re) ? re.exec(to)[1] : to,
threadId: th.getId(),
messageId: m.getId()
});
});
if (temp.length > 1 && threadId != th.getId()) {
var ar = temp.filter(function(e){return temp[0].from == e.to});
if (ar.length > 0) {
if (ar.length > 1) ar.splice(1, ar.length - 1);
ar.forEach(function(e){
Gmail.Users.Messages.modify(
{"addLabelIds": [Gmail.Users.Labels.list(userId).labels.filter(function(e){return e.name == label})[0].id]},
userId,
e.messageId
);
Logger.log("Label '%s' was added to messageId '%s'.", label, e.messageId)
});
}
threadId = th.getId();
}
}
});
});
}
var thread = GmailApp.getInboxThreads();
用于检索邮件。这意味着将检索收件箱中的邮件。如果您想在其他地方检索,请修改此内容。function myFunction() {
var label = "temp"; // Please input a label you want to use.
var userId = "me"; // If you use this script by yourself, userId is "me".
var thread = GmailApp.getInboxThreads(); // In this setting, all mails in Inbox are retrieved. If you want to use the special mails, please modify here.
var threadId = "";
thread.forEach(function(th) {
th.getMessages().forEach(function(msg) {
var mm = msg.getThread().getMessages();
if (mm.length > 1) {
var temp = [];
mm.forEach(function(m) {
var re = /<(\w.+)>/g;
var from = m.getFrom();
var to = m.getTo();
temp.push({
from: from.match(re) ? re.exec(from)[1] : from,
to: to.match(re) ? re.exec(to)[1] : to,
threadId: th.getId(),
messageId: m.getId()
});
});
Logger.log("temp : %s", temp) // Added
if (temp.length > 1 && threadId != th.getId()) {
var ar = temp.filter(function(e){return temp[0].from == e.to});
Logger.log("ar : %s", ar) // Added
if (ar.length > 0) {
if (ar.length > 1) ar.splice(1, ar.length - 1);
ar.forEach(function(e){
Gmail.Users.Messages.modify(
{"addLabelIds": [Gmail.Users.Labels.list(userId).labels.filter(function(e){return e.name == label})[0].id]},
userId,
e.messageId
);
Logger.log("Label '%s' was added to messageId '%s'.", label, e.messageId)
});
}
threadId = th.getId();
}
}
});
});
}