Cannot read property ‘getThreads’ of null

Posted on

Cannot read property ‘getThreads’ of null – Whether you’re just starting out with Google Sheets or are a seasoned pro, sooner or later one of your formulas will give you a formula or script parse error message rather than the result you want. However, every Formula user, irrespective of whether it’s a beginner or expert, has invariably come across a formula parse error in Google Sheets at least once in their life (and if you haven’t. you soon will). It can be frustrating, especially if it’s a longer formula where the formula parse error may not be obvious. In this article, we will talk about some common script or formula parse errors in Google Sheets, like Cannot read property ‘getThreads’ of null, and how to fix it.

I have this code, and I’m not getting why the getThreads property is not being read.

Is getThreads() deprecated or so?

function cleanUp() {  
var delayDays = 2 // Enter # of days before messages are moved to trash   
var maxDate = new Date(); 
maxDate.setDate(maxDate.getDate()-delayDays);    
var label = GmailApp.getUserLabelByName("2dehands");  
var threads = label.getThreads() || [];  
for (var i = 0; i < threads.length; i++) {  
  if (threads[i].getLastMessageDate()<maxDate)
  {  
    threads[i].moveToTrash();
  } 
} 
}

TypeError: Cannot read property ‘getThreads’ of null
cleanUp @ Code.gs:6

Answer :

The error means that the value/object referenced by label is null. Check if there is a label named 2dehands in the user Gmail mailbox.

Leave a Reply

Your email address will not be published.