title: "Invalid Argument" error with addLabel(label) method
body: <p>For a while now, I've been trying to create an autolabeler for Gmail in Google Apps Script. This script parses through an email's body and looks for a line with the form "#DL:", extracts the text after that (which is a date), runs this string through a standardizer I have coded, calculates the date difference between that date and now, and if it's urgent, assigns a color to that. It gets all that information and labels that email. Here is the code I use: </p>
  
  <pre><code>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) || createLabelByGmailApi(name);
          }
          //GmailApp.createLabel(name);
          //createLabelByGmailApi(name);
          return labelCache[name];
      }
  
      var applyLabel = function(name, thread) {
          var label = null;
          var labelName = "";
          name.split('&amp;').forEach(function(labelPart, i) {
              labelName = labelName + (i === 0 ? "" : "&amp;") + 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 &amp;&amp; !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);
  }
  
  function datecalc(stringdata) {
  
      var len = stringdata.length
      var min = stringdata.slice(len - 2, len);
      var hora = stringdata.slice(len - 5, len - 3);
      var mes = stringdata.slice(len - 9, len - 7);
      var dia = stringdata.slice(len - 12, len - 10);
      min = Number(min);
      hora = Number(hora);
      mes = Number(mes);
      dia = Number(dia);
      var data = new Date(2019, mes - 1, dia, hora, min);
      var data2 = Date.now();
      var diff = data - data2;
      diff = diff / 86400000
      var color;
      if (diff &lt;= 1.5) {
          color = 'red'
      } else if (diff &gt; 1.5 &amp;&amp; diff &lt;= 4) {
          color = 'yellow'
      } else {
          color = 'green'
      }
      return color;
  }
  
  function mesnum(mon) {
      var m = {
          'jan': '01',
          'fev': '02',
          'mar': '03',
          'abr': '04',
          'mai': '05',
          'jun': '06',
          'jul': '07',
          'ago': '08',
          'set': '09',
          'out': '10',
          'nov': '11',
          'dez': '12'
      };
      var s = mon.slice(0, 3)
      var idc = String(m[s]);
      if (idc.length &lt; 2) {
          idc = "0" + idc;
      }
      return idc;
  }
  
  function datestd(date) {
      var ano = "2019";
      var whitelistdias = ["terça-feira", "quarta-feira",
          "quinta-feira", "sexta-feira", "sábado", "domingo",
          "segunda", "terça", "quarta", "quinta", "sexta", "sabado",
          "terca"
      ];
  
      var whitelistmes = ["janeiro", "fevereiro", "março", "abril", "maio", "junho",
          "julho", "agosto", "setembro", "outubro", "novembro",
          "dezembro", "jan", "fev", "mar", "abr", "mai", "jun",
          "jul", "ago", "set", "out", "nov", "dez"
      ];
      var whitelistchar = ["/", "-", "."];
  
      var idk = date.toLowerCase();
      idk = idk.replace(/,/g, " ,");
      idk = idk.split(" ");
      var v;
      var pos;
      var dia;
      var hora;
      var mes;
      var searchd;
      var posfinal;
  
      if (whitelistmes.some(function(v) {
              return idk.indexOf(v) !== -1;
          }) == true) {
          idk = String(idk);
          whitelistmes.forEach(function(strs) {
              return idk.replace(strs, "");
          });
          whitelistdias.forEach(function(strq) {
              return idk.replace(strq, "");
          });
  
          idk = String(idk);
          idk = idk.replace("[", "");
          idk = idk.replace(".", ":");
          idk = idk.replace("]", "");
          idk = idk.replace("de", "");
          idk = idk.replace(" ", "");
          idk = idk.replace("'", "");
          idk = idk.replace("as", ",");
          idk = idk.replace("at", ",");
          idk = idk.replace("of", ",");
          idk = idk.replace("às", ",");
          idk = idk.replace("h", "");
          idk = idk.replace(ano, "");
          pos = idk.indexOf(",");
          dia = idk.slice(0, pos);
          idk = String(idk);
          hora = idk.slice(idk.lastIndexOf(",") + 1, idk.length);
          idk = idk.split(",");
          mes = idk.filter(function(n) {
              return whitelistmes.indexOf(n) !== -1;
          });
          mes = String(mes);
  
          return dia + "/" + mesnum(mes) + ", " + hora;
      } else {
  
          idk = String(idk);
          if (idk.includes("/") || idk.includes("-")) {
              whitelistmes.forEach(function(strs) {
                  return idk.replace(strs, "");
              });
              whitelistdias.forEach(function(strq) {
                  return idk.replace(strq, "");
              });
          }
          idk = String(idk);
          idk = idk.replace(".", ":");
          idk = idk.replace("[", "");
          idk = idk.replace("]", "");
          idk = idk.replace("de", "");
          idk = idk.replace(/ /g, "");
          idk = idk.replace("'", "");
          idk = idk.replace("as", ",");
          idk = idk.replace("às", ",");
          idk = idk.replace(ano, "");
          var hmm = new Array();
  
          idk = idk.split('');
          hmm = idk.reduce(function(matches, character, index) {
              if (whitelistchar.includes(character)) hmm.push(index);
              return hmm;
          }, []);
  
          hmm = String(hmm);
          hmm = hmm.replace("[", "");
          hmm = hmm.replace("]", "");
          hmm = hmm.replace(" ", "");
          var pos1 = hmm.indexOf(",");
          if (pos1 !== -1) {
              var prim = hmm.slice(0, pos1);
              prim = Number(prim);
              var seg = hmm.slice(pos1 + 1, hmm.length);
              seg = Number(seg);
              dia = idk.slice(0, prim);
              mes = idk.slice(prim + 1, seg);
              hora = idk.slice(seg + 1, idk.length);
          } else {
              hmm = Number(hmm);
              pos1 = idk.indexOf(",");
              dia = idk.slice(0, hmm);
              mes = idk.slice(hmm + 1, pos1);
              hora = idk.slice(pos1 + 1, idk.length);
          }
          hora = String(hora);
          searchd = hora.match(/\d/);
          posfinal = hora.indexOf(searchd);
  
          hora = hora.slice(posfinal, hora.length);
          idk = String(idk);
          idk = idk.replace(/,/g, "");
          dia = String(dia);
          mes = String(mes);
          hora = String(hora);
          dia = dia.replace(/,/g, "");
          mes = mes.replace(/,/g, "");
          hora = hora.replace(/,/g, "");
          hora = hora.replace(/h/g, "");
          return dia + "/" + mes + ", " + hora;
      }
  }
  
  </code></pre>
  
  <p>To give some context to what's happening:
  The labeler function gets the string from the email, sends it to the datestd function to get standardized, and that output is the name of the label. That output is also used to calculate the date difference between that date and now, through the function datecalc, which outputs a color. This color will be used by the <code>createLabelByGmailApi</code> function to create a label with that name and color. After this, the labeler function applies that label to the email in question.</p>
  
  <p>There are 2 problems I am trying to fix, to no avail:</p>
  
  <p><strong>1. Invalid Argument Error</strong></p>
  
  <p>When running the code above, I get an "Invalid Argument" error in the <code>thread.addLabel(label);</code> line (the argument is label), and I don't know why. </p>
  
  <p><strong>2. Can't seem to fetch the color for the <code>createLabelByGmailApi</code> function</strong></p>
  
  <p>Due to the way the code is structured, I can't seem to fetch the color for the function, since this color depends on the date, which depends on the parsing of the email body, which happens later in the function. I can't seem to find a way to rearrange this so I am able to provide a color for the function, would be great if you could help.</p>
  
  <p>I know this is a handful, and thanks so much for reading this, your help would be much appreciated.</p>