1

我正在尝试使以下 Google Apps 脚本正常工作,取自另一个较早的 Groups 帖子。它基本上正在工作到调用 getThreads(n, m) 的地步。返回的错误代码是:

TypeError:无法调用 null 的方法“addToThreads”。

有没有更简单的方法来调试 getThreads() 函数?目标是将标签从 EVERNOTE 更改为 Scripts/Forward。

// Set up multiple labels/sub-labels and add one or more email addresses
function forwardEmails() {
  forwardThreads("EVERNOTE", "username@m.evernote.com", "@Filtered Email Archive");
//  forwardThreads("Forward/MoreItems", "username1@gmail.com, username2@gmail.com", "MORE INFO");
  }

function forwardThreads(label, addr, subjSuffix) {

  var maxSubjLength = 250; 
  var applylabel = GmailApp.getUserLabelByName("Scripts/Forward");

  // Send individual and threaded emails.
  var msgs, msg, i, j, subject, options, labels, page;
  labels = GmailApp.getUserLabelByName(label)
  var threads = labels.getThreads()
  for (i=0; i < threads.length; i++) {
    msgs = threads[i].getMessages();
    for (j=0; j < msgs.length; j++) {
      msg = msgs[j];

      subject = msg.getSubject();
      if (subject.length + subjSuffix.length > maxSubjLength) {
        subject = subject.substring(0, maxSubjLength - subjSuffix.length);
      }

      options = { htmlBody: msg.getBody(), attachments : msg.getAttachments() };

      GmailApp.sendEmail(addr, subject +" "+ subjSuffix, msg.getBody(), options);

    }
  }

  while(!page || page.length == 100) {
    page = labels.getThreads(0, 100);

  // Apply new label; move the thread out of other label
     applylabel.addToThreads(page);    // ***** Failing on this line - assume page is NULL
     labels.removeFromThreads(page);
      }  
}
4

1 回答 1

1

此代码中的错误消息TypeError: Cannot call method "addToThreads" of null.表示applylabel变量为null。只有在GmailApp.getUserLabelByName("Scripts/Forward");退货null时才会发生,如果系统找不到Scripts/Forward标签,就会发生这种情况。我假设其身份运行脚本的用户没有这样的标签。

于 2012-08-12T15:38:08.093 回答