TypeError:无法调用null的方法“getThreads”。 (第59行)+ 1其他错误

时间:2012-09-24 10:02:46

标签: javascript api gmail google-apps-script

我对Javascript知之甚少,但想使用此代码来增强我的GMail体验。它有效,但我也有错误。当我在Google电子表格中运行调试器时,两个功能似乎出现故障:

  

TypeError:无法调用null的方法“getThreads”。 (第59行)

使用以下执行记录

  

GmailApp.getUserLabelByName([随访])

     

GmailApp.getUserLabelByName([随访/ 1undefined])

已插入评论:此处有一些关于GMail API调用getThreads(和其他)的信息:https://developers.google.com/apps-script/class_gmaillabel#getThreads

我没有得到的是它为什么要调用Followup / 1undefined - >为什么不定义?它应该是Followup / 1days

另一个函数的另一个错误:

  

无法找到方法moveThreadsT​​oInbox(。(第26行)

执行记录中没有任何内容

整个代码是:

// Adapted from:
// http://gmailblog.blogspot.com/2011/07/gmail-snooze-with-apps-script.html
//
// To setup:
// - From the |Run| menu select |setup|
//   - if prompted to authorize, do so, and then repeat this step.
//
// - Verify the script is set to be triggered to run
//   - |Triggers| menu |Current script's triggers...|
//   - 3 triggers should exist to call e.g. 
//     - dailyUpdate, Time Driven, Daily

function getLabelName(i, labelSuffixString) {
  return "FollowUp/" + i + labelSuffixString;
}

function setup() {
  for (var i = 1; i <= 7; ++i) {
    GmailApp.createLabel(getLabelName(i, "days"));
    GmailApp.createLabel(getLabelName(i, "weeks"));
  }
  GmailApp.createLabel("FollowUp");
}

function moveToInbox(page) {
  GmailApp.moveThreadsToInbox(page);
  GmailApp.markThreadsImportant(page);
}

function cleanOldFollowUpLabels() {
  var searchString = "-label:inbox label:FollowUp";
  for (var i = 1; i <= 7; ++i) {
    searchString += " -label:" + getLabelName(i, "days");
    searchString += " -label:" + getLabelName(i, "weeks");
  }
  searchString = searchString.replace(RegExp("/", "g"), "-");
  Logger.log("cleanOldFollowUpLabels() Search String:");
  Logger.log(searchString);
  var followUpLabel = GmailApp.getUserLabelByName("FollowUp");  
  var page = null;
  // Get threads in "pages" of 100 at a time
  while(!page || page.length == 100) {
    page = GmailApp.search(searchString, 0, 100);
    Logger.log("found: " + page.length);
    if (page.length > 0)
      followUpLabel.removeFromThreads(page);   
  }
}

function update(labelSuffixString) {
  var oldLabel, newLabel, page;
  var followUpLabel = GmailApp.getUserLabelByName("FollowUp");
  for (var i = 1; i <= 7; ++i) {
    newLabel = oldLabel;
    oldLabel = GmailApp.getUserLabelByName(getLabelName(i, labelSuffixString));
    page = null;
    // Get threads in "pages" of 100 at a time
    while(!page || page.length == 100) {
      page = oldLabel.getThreads(0, 100);
      if (page.length > 0) {
        followUpLabel.addToThreads(page);
        if (newLabel) {
          // Move the threads into "today’s" label
          newLabel.addToThreads(page);
        } else {
          moveToInbox(page);
        }     
        // Move the threads out of "yesterday’s" label
        oldLabel.removeFromThreads(page);
      }  
    }
  }
}

function dailyUpdate() {
  update("days");
}
function weeklyUpdate() {
  update("weeks");
}

此处:http://pastie.org/4790086.js

1 个答案:

答案 0 :(得分:0)

mm。在同事的帮助下,我们找到了自己问题的答案。我有三个触发器运行:每日触发器,每周触发器和更新触发器。现在更新触发器不是必需的,因为它是由每日和每周触发器调用的,没有任何输入。这导致了错误。现在我必须等待,看看是否有任何错误以及脚本是否有效。