使用Google Apps脚本在Gmail中创建本机过滤器 [英] Create a native filter in Gmail using Google Apps Script
问题描述
我想为某些参数设置一个Gmail固有的过滤器.
I want to make a filter that is native to gmail for certain set of parameters.
基本上,我经常使用google别名功能(电子邮件后的+号).我想自动化创建一个过滤器的过程,该过滤器读取"To"行然后寻找"+".如果找到一个"+",它将标记"+"之后的内容.然后,它将创建一个专用/本机过滤器,该过滤器将:跳过收件箱,并在"+"之后加上标签.
Basically I use the google alias function a lot (the + sign after your email). I want to automate the process of creating a filter that reads the "To" line then looks for a "+". If the a "+" is found it will make a label of what is after the "+". Then it will create a dedicated/native filter that will: skip the inbox and apply the label of what is after the "+".
我浏览了gmail脚本,但没有找到制作本地过滤器的方法.我了解此功能可能刚刚实现.
I have looked through the gmail scripts and have not found a way to make a native filter. I understand that this functionality might have just been implemented.
function getTo() {
// Log the To line of up to the first 50 emails in your Inbox
var email = Session.getActiveUser().getEmail();
Logger.log(email);
var threads = GmailApp.getInboxThreads(0, 50);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
Logger.log(messages[j].getTo()||email);
}
}
}
任何帮助都会很棒.
解决方案:
// Creates a filter to put all email from ${toAddress} into
// Gmail label ${labelName}
function createToFilter(toAddress, labelName) {
// Lists all the filters for the user running the script, 'me'
var labels = Gmail.Users.Settings.Filters.list('me')
// Search through the existing filters for ${toAddress}
var label = true
labels.filter.forEach(function(l) {
if (l.criteria.to === toAddress) {
label = null
}
})
// If the filter does exist, return
if (label === null) return
else {
// Create the new label
GmailApp.createLabel(labelName)
// Lists all the labels for the user running the script, 'me'
var labelids = Gmail.Users.Labels.list('me')
// Search through the existing labels for ${labelName}
// this operation is still needed to get the label ID
var labelid = false
labelids.labels.forEach(function(a) {
if (a.name === labelName) {
labelid = a
}
})
Logger.log(labelid);
Logger.log(labelid.id);
// Create a new filter object (really just POD)
var filter = Gmail.newFilter()
// Make the filter activate when the to address is ${toAddress}
filter.criteria = Gmail.newFilterCriteria()
filter.criteria.to = toAddress
// Make the filter remove the label id of ${"INBOX"}
filter.action = Gmail.newFilterAction()
filter.action.removeLabelIds = ["INBOX"];
// Make the filter apply the label id of ${labelName}
filter.action.addLabelIds = [labelid.id];
// Add the filter to the user's ('me') settings
Gmail.Users.Settings.Filters.create(filter, 'me')
}
}
谢谢,恩德
推荐答案
该功能似乎存在,但需要启用完整的Gmail API.
The functionality seems to exist, but requires enabling the full Gmail API.
对于您的Apps脚本,请按照 https://developers.google上的说明进行操作.com/apps-script/guides/services/advanced 启用高级Google服务.
当我第一次尝试使用Gmail.Users.Settings.Filters.list('me')
调用时,遇到了一个授权错误,该错误为我提供了一个在开发人员控制台中启用Gmail API的链接,这等同于翻转开关.
For your Apps script, follow the directions at https://developers.google.com/apps-script/guides/services/advanced to enable Advanced Google Services.
When I first tried using the Gmail.Users.Settings.Filters.list('me')
call I got an authorization error which gave me a link to enable the Gmail API in the Developer Console which just amounted to flipping a switch.
启用此功能后,您可以编写脚本来制作过滤器,例如
Once you have this enabled, you can write a script to make filters, like
//
// Creates a filter to put all email from ${toAddress} into
// Gmail label ${labelName}
//
function createToFilter (toAddress, labelName) {
// Lists all the labels for the user running the script, 'me'
var labels = Gmail.Users.Labels.list('me')
// Search through the existing labels for ${labelName}
var label = null
labels.labels.forEach(function (l) {
if (l.name === labelName) {
label = l
}
})
// If the label doesn't exist, return
if (label === null) return
// Create a new filter object (really just POD)
var filter = Gmail.newFilter()
// Make the filter activate when the to address is ${toAddress}
filter.criteria = Gmail.newFilterCriteria()
filter.criteria.to = toAddress
// Make the filter apply the label id of ${labelName}
filter.action = Gmail.newFilterAction()
filter.action.addLabelIds = [label.id]
// Add the filter to the user's ('me') settings
Gmail.Users.Settings.Filters.create(filter, 'me')
}
function main () {
createToFilter('youruser+foo@gmail.com', 'Aliases/foo')
}
由于Google似乎没有在API中公开该过滤器,因此我无法弄清楚如何创建一个可以自动为消息加标签的过滤器.
I wasn't able to figure out how to create a filter that retroactively labels messages automatically since Google doesn't seem to expose that in their API.
这篇关于使用Google Apps脚本在Gmail中创建本机过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!