使用 Google Apps 脚本为 Gmail 自动添加 move/apply 标签
Auto move/apply label to Gmail with Google Apps Script
我正在尝试为我的 G Suite 邮箱制作一个自动归档脚本。
该脚本包含一个搜索查询,该查询随后应获取所有找到的结果,从收件箱中删除并向其添加自定义标签(存档)。
我正在努力处理 addLabel
部分。它抛出一个错误 InternalError: Cannot find method addLabel(string).
但是当我检查文档时它似乎是正确对象上的正确方法。
如有任何帮助,我们将不胜感激。
自动 gmail 存档
var ARCHIVE_LABEL = "archived";
var AUTO_ARCHIVE_AFTER = "30";
function Intialize() {
return;
}
function Install() {
ScriptApp.newTrigger("autoArchive")
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*2))
.create();
ScriptApp.newTrigger("autoArchive")
.timeBased().everyDays(1).create();
}
function Uninstall() {
var triggers = ScriptApp.getScriptTriggers();
for (var i=0; i<triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function autoArchive() {
var age = new Date();
age.setDate(age.getDate() - AUTO_ARCHIVE_AFTER);
var auto = Utilities.formatDate(age, Session.getScriptTimeZone(), "yyyy-MM-dd");
var search = "in:inbox is:read before:" + auto;
try {
var threads = GmailApp.search(search, 0, 100);
console.log("Found total:", threads.length); //This gives me 100 for the debug/test run which matches the result I should be getting
if (threads.length == 100) {
ScriptApp.newTrigger("autoArchive")
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*10))
.create();
}
for (var i=0; i<threads.length; i++) {
if(threads[i].isInInbox()){
console.log("So far so good. Let's add label")
threads[i].addLabel('archived'); // This throws back the error **InternalError: Cannot find method addLabel(string).**
}
}
} catch (e) {
console.log('Error');
console.log(e);
}
}
这个修改怎么样?
修改点:
addLabel(label)
的 label
的类型是 GmailLabel
。
- 你的情况,可以使用
getUserLabelByName()
的方法。
修改后的脚本:
请修改如下
从:
for (var i=0; i<threads.length; i++) {
if(threads[i].isInInbox()){
console.log("So far so good. Let's add label")
threads[i].addLabel('archived'); // This throws back the error **InternalError: Cannot find method addLabel(string).**
}
}
到:
var label = GmailApp.getUserLabelByName("archived"); // Added
for (var i=0; i<threads.length; i++) {
if(threads[i].isInInbox()){
console.log("So far so good. Let's add label")
threads[i].addLabel(label); // Modified
}
}
注:
- 如果要添加新标签,请在 运行 脚本之前创建标签。当然,您可以使用脚本创建新标签。届时请使用
createLabel()
as @Cooper评论的方法
参考文献:
如果我误解了你的问题,请告诉我。我想修改一下。
我正在尝试为我的 G Suite 邮箱制作一个自动归档脚本。
该脚本包含一个搜索查询,该查询随后应获取所有找到的结果,从收件箱中删除并向其添加自定义标签(存档)。
我正在努力处理 addLabel
部分。它抛出一个错误 InternalError: Cannot find method addLabel(string).
但是当我检查文档时它似乎是正确对象上的正确方法。
如有任何帮助,我们将不胜感激。
自动 gmail 存档
var ARCHIVE_LABEL = "archived";
var AUTO_ARCHIVE_AFTER = "30";
function Intialize() {
return;
}
function Install() {
ScriptApp.newTrigger("autoArchive")
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*2))
.create();
ScriptApp.newTrigger("autoArchive")
.timeBased().everyDays(1).create();
}
function Uninstall() {
var triggers = ScriptApp.getScriptTriggers();
for (var i=0; i<triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function autoArchive() {
var age = new Date();
age.setDate(age.getDate() - AUTO_ARCHIVE_AFTER);
var auto = Utilities.formatDate(age, Session.getScriptTimeZone(), "yyyy-MM-dd");
var search = "in:inbox is:read before:" + auto;
try {
var threads = GmailApp.search(search, 0, 100);
console.log("Found total:", threads.length); //This gives me 100 for the debug/test run which matches the result I should be getting
if (threads.length == 100) {
ScriptApp.newTrigger("autoArchive")
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*10))
.create();
}
for (var i=0; i<threads.length; i++) {
if(threads[i].isInInbox()){
console.log("So far so good. Let's add label")
threads[i].addLabel('archived'); // This throws back the error **InternalError: Cannot find method addLabel(string).**
}
}
} catch (e) {
console.log('Error');
console.log(e);
}
}
这个修改怎么样?
修改点:
addLabel(label)
的label
的类型是GmailLabel
。- 你的情况,可以使用
getUserLabelByName()
的方法。
- 你的情况,可以使用
修改后的脚本:
请修改如下
从:for (var i=0; i<threads.length; i++) {
if(threads[i].isInInbox()){
console.log("So far so good. Let's add label")
threads[i].addLabel('archived'); // This throws back the error **InternalError: Cannot find method addLabel(string).**
}
}
到:
var label = GmailApp.getUserLabelByName("archived"); // Added
for (var i=0; i<threads.length; i++) {
if(threads[i].isInInbox()){
console.log("So far so good. Let's add label")
threads[i].addLabel(label); // Modified
}
}
注:
- 如果要添加新标签,请在 运行 脚本之前创建标签。当然,您可以使用脚本创建新标签。届时请使用
createLabel()
as @Cooper评论的方法
参考文献:
如果我误解了你的问题,请告诉我。我想修改一下。