Search code examples
google-apps-scriptgoogle-sheetstriggersgmail

Puling 90,000 emails to Google Sheet


I have used https://webapps.stackexchange.com/questions/160232/gmail-bulk-emails-manipulation and modified a bit to copy some 90,000 emails (from a free gmail account) to a sheet.

The script is working only once. After one cycle, the trigger becomes "disabled" with "Unknown reason".

I reduced the batch size, increased the time gap, but still it is getting the same error.

Where am I going wrong?


/**
 * Creates the first trigger to call batchArchiveEmail. 
 */
function init(){

    var Triggers = ScriptApp.getProjectTriggers();
    for (var p = 0; p < Triggers.length; p++) {
      ScriptApp.deleteTrigger(Triggers[p])
    }



   ScriptApp
    .newTrigger('batchArchiveEmail')
    .timeBased()
    .after(60 * 1000)
    .create();
    console.log(`trigger created`)
}

/**
 * Archive emails by batches preventing controlling limiting the execution time and  
 * creating a trigger if there are still threads pending to be archived.
 */ 
function batchArchiveEmail(){
  const start = Date.now();
  /** 
   * Own execution time limit for the search and archiving operations to prevent an 
   * uncatchable error. As the execution time check is done in do..while condition there  
   * should be enough time to one search and archive operation and to create a trigger 
   * to start a new execution. 
   */ 
  const maxTime = 3 * 60 * 1000; // Instead of 25 use 3 for Google free accounts
  const batchSize = 50;
  let threads, elapsedTime;
  var ss=SpreadsheetApp.getActive();
  var sheet=ss.getSheetByName("Sheet1");
  /** Search and archive threads, then repeat until the search returns 0 threads or the 
   * maxTime is reached
   */ 
  var ms=[];
  do {

    threads = GmailApp.search('label:inbox before:2022/5/1');
    for (let j = 0; j < threads.length; j += batchSize) {
      //below code added by me
      ms=[];
      var messages = threads[j].getMessages();
      for (var m = 0; m < messages.length; m++) {
        var from = messages[m].getFrom(); //from field
        var mId = messages[m].getId();//id field to create the link later
        var supportStats = [];
        var to = messages[m].getTo();//to field
        var time = messages[m].getDate();//date field
        var subject = messages[m].getSubject();//subject field
        var body=messages[m].getPlainBody();
        var tel=[];
        tel = body.match(/[\+]?\d{10}|\(\d{3}\)\s?-\d{6}|\d{3}\s-\d{3}\s\d{4}/);
        supportStats.push(from);
        supportStats.push(to);
        supportStats.push(time);
        supportStats.push(subject);
        supportStats.push('https://mail.google.com/mail/u/0/#inbox/'+mId); //build the URL to the email
        supportStats.push(body);
        if (tel){supportStats.push(tel[0])} else {supportStats.push("")};
        ms.push(supportStats);
      }
    var lr=sheet.getLastRow();
    sheet.getRange(lr+1,1,ms.length,7).setValues(ms);
    //above code added by me
    GmailApp.moveThreadsToArchive(threads.slice(j, j + batchSize));
    };
    /**
     * Used to prevent to have too many calls in a short time, might not be 
     * necessary with a large enough batchSize
     */
    Utilities.sleep(`2000`); 
    elapsedTime = Date.now() - start;
  } while (threads.length > 0 &&  elapsedTime < maxTime);
  if(threads.length > 0){
    /** Delete the last trigger */

    var Triggers = ScriptApp.getProjectTriggers();
    for (var p = 0; p < Triggers.length; p++) {
      ScriptApp.deleteTrigger(Triggers[p])
    }


    //deleteTriggers();

    /** Create a one-time new trigger */
    ScriptApp
    .newTrigger('batchArchiveEmail')
    .timeBased()
    .after(300 * 1000)
    .create();
    console.log(`next trigger created`)
  } else {
    /** Delete the last trigger */
    var Triggers = ScriptApp.getProjectTriggers();
    for (var p = 0; p < Triggers.length; p++) {
      ScriptApp.deleteTrigger(Triggers[p])
    }
    console.log(`No more threads to process`);
  }
}



Solution

  • The issue of This trigger has been disable for an unknown reason is reported here, and seems to be related to the V8 runtime. Click on the +1 to let Google know that you are also affected by the issue.

    Deactivating the Project Settings > General Settings > Enable Chrome V8 runtime seems to fix the issue for some users. Same as changing the appsscript.json key runtimeVersion from:

      "runtimeVersion": "V8"
    

    to

      "runtimeVersion": "DEPRECATED_ES5"