【发布时间】:2019-06-06 00:29:28
【问题描述】:
我正在尝试为我的 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);
}
}
【问题讨论】:
标签: google-apps-script gmail gmail-api google-apps google-workspace