Search code examples
google-apps-scriptgmailgmail-apigoogle-appsgoogle-workspace

Auto move/apply label to Gmail with Google Apps Script


I'm trying to make an automated archiving script for my G Suite mailbox.

The script contains a search query which then should take all found results, remove from inbox and add a custom label (archive) to it.

I'm struggling with the addLabel part. It throws an error InternalError: Cannot find method addLabel(string). but when I check the documentation it seems to be the right method on the right object.

Any help would be appreciated.

Auto-gmail archiving

var ARCHIVE_LABEL = "archived";    
var AUTO_ARCHIVE_AFTER = "30";

function Intialize() {
  return;
}

function Install() {

  ScriptApp.newTrigger("autoArchive")
           .timeBased()
           .at(new Date((new Date()).getTime() + 1000*60*2))
           .create();

  ScriptApp.newTrigger("autoArchive")
           .timeBased().everyDays(1).create();

}

function Uninstall() {

  var triggers = ScriptApp.getScriptTriggers();
  for (var i=0; i<triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }

}

function autoArchive() {

  var age = new Date();  
  age.setDate(age.getDate() - AUTO_ARCHIVE_AFTER);

  var auto  = Utilities.formatDate(age, Session.getScriptTimeZone(), "yyyy-MM-dd");
  var search = "in:inbox is:read before:" + auto;

  try {

    var threads = GmailApp.search(search, 0, 100);
    console.log("Found total:", threads.length); //This gives me 100 for the debug/test run which matches the result I should be getting


    if (threads.length == 100) {
      ScriptApp.newTrigger("autoArchive")
               .timeBased()
               .at(new Date((new Date()).getTime() + 1000*60*10))
               .create();
    }

    for (var i=0; i<threads.length; i++) {
      if(threads[i].isInInbox()){
        console.log("So far so good. Let's add label")
        threads[i].addLabel('archived'); // This throws back the error **InternalError: Cannot find method addLabel(string).**
      }
    }

  } catch (e) {
    console.log('Error');
    console.log(e);
  }

}

Solution

  • How about this modification?

    Modification points:

    • Type of label of addLabel(label) is GmailLabel.
      • In your situation, you can use the method of getUserLabelByName().

    Modified script:

    Please modify as follows.

    From:
    for (var i=0; i<threads.length; i++) {
      if(threads[i].isInInbox()){
        console.log("So far so good. Let's add label")
        threads[i].addLabel('archived'); // This throws back the error **InternalError: Cannot find method addLabel(string).**
      }
    }
    
    To:
    var label = GmailApp.getUserLabelByName("archived"); // Added
    for (var i=0; i<threads.length; i++) {
      if(threads[i].isInInbox()){
        console.log("So far so good. Let's add label")
        threads[i].addLabel(label); // Modified
      }
    }
    

    Note:

    • If you want to give new label, please create the label before you run the script. Of course, you can create the new label using script. At that time, please use the method of createLabel() as @Cooper's comment.

    References:

    If I misunderstand your question, please tell me. I would like to modify it.