1
votes

I have been using code from Suyash Gandhi in order to count the number of emails that fall under a certain label everyday. Below is the code I've been using:

function CountVoicemail() 
{
var label = GmailApp.getUserLabelByName("X140");
var labelname = label.getName();
var mails = label.getThreads();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data 
Sheet");
var date = new Date();
sheet.appendRow([labelname, date, mails.length]);
}

I have the code running from a Google Sheet where everyday at a specified time I chose, the script will run and a new row with three columns will be added; the Label Name, the Date and Time the Script Ran, and the total number of emails with that specific label. I then have an Array Formula (in column D) in the actual Google Sheet subtract the previous day's total in order to calculate the number of emails received with that label for the day (I have a filter in Gmail set up to label emails from a specific source). This set up worked perfectly up until recently where once the number of emails in my label surpassed 500 the getThreads() call in the script would still only return the value of 500. Is there way that I make the getThreads() call return a value bigger than 500? If not is there another call I can use? I've tried specifying ranges for the getThreads call [getThreads(start, max)] but this doesn't really help me since I want to get a count of the total number of emails in my label in order to calculate the day's total from my ArrayFormula.

1
It would be easier for you to grab all of the emails for that day rather than grabbing potentially thousands of emails daily with your script. You could then change your formula to calculate your running total (or do this as part of your script too).ross

1 Answers

0
votes

Read this: https://developers.google.com/apps-script/reference/gmail/gmail-label#getthreads

And take look at this:

getThreads(start, max)
Gets a range of threads marked with this label.

// log the subject lines of up to the first 30 threads with the label MyLabel
var label = GmailApp.getUserLabelByName("MyLabel");
var threads = label.getThreads(0, 30);
for (var i = 0; i < threads.length; i++) {
  Logger.log(threads[i].getFirstMessageSubject());
}