0

I have a script that processes email's data and saves it in google sheet. I have set a time trigger to run automatically every 15 minutes. It runs fine for few hours and then it stops working. I do not see any error or receive any notification from Apps Script; it's just like it doesn't exist. I have found these Stack Overflow questions (Question1, Question2), but I couldn't get any help from them. Removed and re-added the trigger again but same issue. Here is the sample code:

function processEmails() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var label = "InProcess";
  var msgs = Gmail.Users.Messages.list('me', { q: 'label:' + label }).messages || [];
  var LIMIT = 10, count = 0;

  for (var i = 0; i < msgs.length && count < LIMIT; i++) {
    var msg = GmailApp.getMessageById(msgs[i].id);
    var subject = msg.getSubject();
    var body = msg.getPlainBody();
    var date = msg.getDate();
    var row = [subject, date, body.substring(0, 100)]; 
    sheet.insertRowBefore(2); // insert under header
    sheet.getRange(2, 1, 1, row.length).setValues([row]);
    msg.getThread().addLabel(GmailApp.getUserLabelByName("AlreadyProcessed"));
    count++;
  }
  Logger.log("Processed " + count + " messages.");
}

And here is the time trigger panel, it's been 6 hours since the last run:

Trigger not firing

Any suggestions to resolve this strange behavior of Apps Script?

2
  • What are the error notification settings for your trigger? By default, the email is sent once a day. You might want to set it to send messages immediately. Are you using gmail.com or a Workspace account? Commented Sep 8 at 16:25
  • Earlier, it was once per day, then I set it to "Notify Immediately" option. I am using gmail.com account Commented Sep 9 at 9:17

1 Answer 1

-1

Why your trigger stops firing:

The issue is most likely a daily quota limit, specifically with the Advanced Gmail Service you are using. The Gmail.Users.Messages.list API call, while fast, has a separate, more restrictive daily limit compared to the standard GmailApp service. When this limit is reached, your trigger fails silently.

The Solution:

Switch from the Advanced Gmail Service to the standard GmailApp service. It's more stable for automated tasks and less likely to hit a quota limit with this type of trigger.

Here is the corrected and more robust code:

function processEmails() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var label = "inProcess"; 

  // Get up to 10 threads with the 'inProcess' label.
  var threads = GmailApp.getUserLabelByName(label).getThreads(0, 10);
  
  if (threads.length > 0) {
    threads.forEach(function(thread) {
      var lastMessage = thread.getMessages().pop();
      
      var subject = lastMessage.getSubject();
      var date = lastMessage.getDate();
      var body = lastMessage.getPlainBody();
      
      var row = [subject, date, body.substring(0, 100)];
      
      sheet.insertRowBefore(2);
      sheet.getRange(2, 1, 1, row.length).setValues([row]);
      
      // Update the labels.
      thread.addLabel(GmailApp.getUserLabelByName("AlreadyProcessed"));
      thread.removeLabel(GmailApp.getUserLabelByName(label));
    });
  }
  Logger.log("Processed " + threads.length + " threads.");
}

This code is more efficient because it fetches threads in a single call and avoids using a service that is prone to quota issues with frequent calls.

Sign up to request clarification or add additional context in comments.

2 Comments

the thing is, it does not throw any daily quota limit error; it just stops triggering the script. If it is a quota error, then it should just show an error while the script trigger runs every time?
Where do you get "Switch from the Advanced Gmail Service to the standard GmailApp service. It's more stable for automated tasks and less likely to hit a quota limit with this type of trigger."? Please elaborate on how you got to this conclusion. (Do you have any evidence? Have you read this somewhere?),

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.