I’m trying to get the following Google Apps script to work, taken from another older Groups post. It is basically is working up to the point where the getThreads(n, m) is called. The error code coming back is:
TypeError: Cannot call method “addToThreads” of null.
Is there an easier way to debug the getThreads() function? Objective is to change the label from EVERNOTE to 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);
}
}
The error message
TypeError: Cannot call method "addToThreads" of null., in this code, means that theapplylabelvariable isnull. It can be only ifGmailApp.getUserLabelByName("Scripts/Forward");returnsnulland it happens if the system can’t find theScripts/Forwardlabel. I assume that the user under whose identity runs the script has no such label.