我有一个Google Apps脚本,可以在收件箱中搜索来自特定电子邮件地址的邮件,添加新标签,然后存档电子邮件以删除收件箱标记。我想知道,是否有一种简单的方法可以将脚本应用于许多不同的电子邮件地址并应用不同的文件标签,而无需制作单独的脚本或具有包含所有不同电子邮件地址的臃肿单一脚本?
function _getFilingLabel() {
var label_text = "**folder name for filing**";
var label = GmailApp.getUserLabelByName(label_text);
if (label == null) {
var label = GmailApp.createLabel(label_text);
}
return label;
}
function addfilelabel() {
var label = _getFilingLabel();
var threads = GmailApp.search('from:(**email address here**) label:inbox older_than:30d');
for (var i = 0; i < threads.length; i++) {
label.addToThread(threads[i]);
}
}
/**
* SCAN THROUGH THE "**folder name for filing**" label and unlabel any items that aren't currently in the inbox
*/
function removeinboxlabel() {
// Every thread in label is archived to remove the inbox label.
var threads = GmailApp.search('label:"**folder name for filing**"');
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
}
}
function incorrectfiled() {
// Checks for items incorrectly filed in folder with activity newer than 30d.
var threads = GmailApp.search('label:"**folder name for filing**" newer_than:30d');
for (var i = 0; i < threads.length; i++) {
threads[i].moveToInbox();
}
}
答案 0 :(得分:0)
使脚本可重复使用的第一步是用参数替换硬编码值。例如,这里的addfilelabel()
包含from_address
和label_text
的参数,其余部分包含在代码段中:
function addfilelabel(from_address, label_text) {
var label = _getFilingLabel(label_text);
var threads = GmailApp.search('from:(%FROM%) label:inbox older_than:30d'
.replace('%FROM%',from_address));
for (var i = 0; i < threads.length; i++) {
label.addToThread(threads[i]);
}
}
function _getFilingLabel(label_text) {
var label = GmailApp.getUserLabelByName(label_text);
if (label == null) {
var label = GmailApp.createLabel(label_text);
}
return label;
}
function addfilelabel(from_address, label_text) {
var label = _getFilingLabel(label_text);
var threads = GmailApp.search('from:(%FROM%) label:inbox older_than:30d'
.replace('%FROM%',from_address));
for (var i = 0; i < threads.length; i++) {
label.addToThread(threads[i]);
}
}
/**
* Scan the given label and archive any items that aren't currently in the inbox
*/
function removeinboxlabel(label_text) {
// Every thread in label is archived to remove the inbox label.
var threads = GmailApp.search('label:"%LABEL%"'
.replace('%LABEL%',label_text));
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
}
}
function incorrectfiled(label_text) {
// Checks for items incorrectly filed in folder with activity newer than 30d.
var threads = GmailApp.search('label:"%LABEL%" newer_than:30d'
.replace('%LABEL%',label_text));
for (var i = 0; i < threads.length; i++) {
threads[i].moveToInbox();
}
}
请注意from_address
如何使用JavaScript String.replace()
方法填充占位符,将其置于搜索字符串中。占位符字符串'%FROM%'
没有什么神奇之处,这只是一个旨在使占位符脱颖而出并减少意外替换错误文本的可能性的约定。
现在您可以像这样调用函数,这是重用您编写的基本代码的一种简单方法:
function do_addfilelabel() {
addfilelabel("user1@example.com", "from-user1");
addfilelabel("user2@example.com", "from-user2");
addfilelabel("user3@example.com", "from-user3");
}
从那里开始,你可以在Web Application中建立一个用户界面。 Web应用程序可以有一个简单的用户界面,允许您输入电子邮件地址和标签,然后将其作为参数传递给服务器端函数google.script.run
。