【问题标题】:Auto move/apply label to Gmail with Google Apps Script使用 Google Apps 脚本自动将标签移动/应用到 Gmail
【发布时间】: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


    【解决方案1】:

    这个修改怎么样?

    修改点:

    • 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()的方式作为@Cooper的评论。

    参考资料:

    如果我误解了你的问题,请告诉我。我想修改它。

    【讨论】:

    • 你需要GmaillApp.createLabel('archived');吗?
    • @Cooper 是的。我原以为 OP 想给现有的标签。但是我通过假设没有使用标签的情况添加了一个注释。感谢您的评论。
    • 太棒了。它有效,不敢相信它是如此简单:-) 非常感谢您的帮助。
    • @Mestika 很高兴您的问题得到了解决。也谢谢你。
    猜你喜欢
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 2020-06-18
    相关资源
    最近更新 更多