Search code examples
google-apps-scriptgmail-api

Is it possible to create a gmail label and choose its color using Google Apps Script?


I am trying to create an autolabeler for my gmail inbox, using Google Apps Script.

For some context, most of my emails have deadlines, and the code I will post below basically parses through the email, finds the #DL and creates a label with the deadline as its name. I also want the label to have a specific color, depending on the time remaining for that deadline.

Unfortunately, I found that the GAS API shows no way of changing/creating a label with a color, but I also found that, through Advanced Google Services, it is possible to define a label's color. When accepting the terms and conditions and enabling Advanced Google Services, autocomplete shows me the option:

Gmail.newLabelColor()

Is it possible to use this to create a new label with a different color using GAS, and if so, how would I be able to interface this with the code below?

var filters = [
  { match: /[\n\r][ \t]*#DL:[ \t]*([^\n\r]*)/, archive: false },
];

var from = [];

function labeler() {

  var batchSize = 10;
  var labelCache = {};
  var query = "in:anywhere";
  var threads = GmailApp.search(query, 0, batchSize);
  GmailApp.getMessagesForThreads(threads);

  var findOrCreateLabel = function(name) {
  if (labelCache[name] === undefined) {
    labelCache[name] = GmailApp.getUserLabelByName(name) || GmailApp.createLabel(name);
  }
    //GmailApp.createLabel(name);
    //createLabelByGmailApi(name);
  return labelCache[name];
}

  var applyLabel = function(name, thread){
    var label = null;
    var labelName = "";
    name.split('&').forEach(function(labelPart, i) {
      labelName = labelName + (i===0 ? "" : "&") + labelPart.trim();
      label = findOrCreateLabel(labelName);
    });

    thread.addLabel(label);
  }
  threads.forEach(function(thread) {
    var messages = thread.getMessages();
    if (messages == null) return;

    var message = messages[messages.length - 1];
    var body = message.getRawContent();
    var archive = true;

    filters.forEach(function(filter){

      var matches = filter.match.exec(body);
      if (matches !== null) {

        var label = filter.name || matches[1];
        var data = datestd(label);
        var cor = datecalc(data);

        label = "Datas/" + data;
        if (label !== undefined) applyLabel(label, thread);

        if (filter.archive !== undefined && !filter.archive) archive = false;
      }
    });

    if (archive) thread.moveToArchive();
  });
}

function createLabelByGmailApi(name,color) {
  var label = GmailApp.getUserLabelByName(name);
  if (label) return label;
  var textColor = "#ffffff";
  if (color=='red'){
  var backgroundColor = "#ac2b16";
  } else if (color=='yellow'){
  var backgroundColor = "#fad165";
  } else if (color=='green'){
  var backgroundColor = "#076239";
  } else {
  var backgroundColor = "#41236d";
  }

  var userId = "me";
  var resource = Gmail.newLabel();
  resource.labelListVisibility = "labelShow";
  resource.messageListVisibility = "show";
  resource.name = name;
  var labelColor = Gmail.newLabelColor();
  labelColor.textColor = textColor;
  labelColor.backgroundColor = backgroundColor;
  resource.color = labelColor;
  Gmail.Users.Labels.create(resource, userId);
  return GmailApp.getUserLabelByName(name);
}

Sorry if this question seems stupid, or if it's really easy to do, I am not really that experienced with this and have found no way to do it through my searches.


Solution

  • For Question 1:

    Is it possible to use this to create a new label with a different color using GAS?

    This answer is yes.

    • You want to create new label with the color.
    • You want to achieve this using Gmail API with Google Apps Script.
    • You have already enabled Gmail API at Advanced Google services.

    If my understanding is correct, how about this sample script and modification? Please think of this as just one of several answers.

    In this sample script, the method of Users.labels: create of Gmail API is used.

    Sample script:

    Please copy and paste the following function to your script editor. When you use this, please set the colors you want to use.

    function createLabelByGmailApi(name) {
      var label = GmailApp.getUserLabelByName(name);
      if (label) return label;
    
      var textColor = "#89d3b2"; // Please set this.
      var backgroundColor = "#ffbc6b"; // Please set this.
    
      var userId = "me";
      var resource = Gmail.newLabel();
      resource.labelListVisibility = "labelShow";
      resource.messageListVisibility = "show";
      resource.name = name;
      var labelColor = Gmail.newLabelColor();
      labelColor.textColor = textColor;
      labelColor.backgroundColor = backgroundColor;
      resource.color = labelColor;
      Gmail.Users.Labels.create(resource, userId);
      return GmailApp.getUserLabelByName(name);
    }
    

    For Question 2:

    how would I be able to interface this with the code below?

    In order to use above function using your script, please modify as follows.

    Modified your script:

    From:
    var findOrCreateLabel = function(name) {
      if (labelCache[name] === undefined) {
        labelCache[name] = GmailApp.getUserLabelByName(name) || GmailApp.createLabel(name);
      }
      return labelCache[name];
    }
    
    To:
    var findOrCreateLabel = function(name) {
      if (labelCache[name] === undefined) {
        labelCache[name] = GmailApp.getUserLabelByName(name) || createLabelByGmailApi(name); // Modified
      }
      return labelCache[name];
    }
    

    Note:

    • The sample color for textColor and backgroundColor is an extreme example.

    Reference:

    If I misunderstood your question and this was not the result you want, I apologize.