0
votes

I have a Google Apps Script which searches the inbox for messages from a certain email address, adds a new label and then archives the email so as to remove the inbox tag. I'm wondering, is there an easy way to apply the script to many different email addresses and apply different filing labels without making an individual script or having a bloated single script with all the different email addresses in it?

    function _getFilingLabel() {

      var label_text = "**folder name for filing**";

      var label = GmailApp.getUserLabelByName(label_text);

      if (label == null) {
        var label = GmailApp.createLabel(label_text);
      }

      return label;
    }



function addfilelabel() {
  var label = _getFilingLabel();

  var threads = GmailApp.search('from:(**email address here**) label:inbox older_than:30d');

  for (var i = 0; i < threads.length; i++) {
    label.addToThread(threads[i]);
  }
}

/**
 * SCAN THROUGH THE "**folder name for filing**" label and unlabel any items that aren't currently in the inbox
 */
function removeinboxlabel() {
// Every thread in label is archived to remove the inbox label.
var threads = GmailApp.search('label:"**folder name for filing**"');
  for (var i = 0; i < threads.length; i++) {
    threads[i].moveToArchive();
}
}

function incorrectfiled() {
// Checks for items incorrectly filed in folder with activity newer than 30d.
  var threads = GmailApp.search('label:"**folder name for filing**" newer_than:30d');
  for (var i = 0; i < threads.length; i++) {
    threads[i].moveToInbox();
}
}
1

1 Answers

0
votes

The first step you can take toward making your script re-usable is to replace your hard-coded values with parameters. For example, here's addfilelabel() with parameters for from_address and label_text, with the balance of the functions following in a snippet:

function addfilelabel(from_address, label_text) {
  var label = _getFilingLabel(label_text);

  var threads = GmailApp.search('from:(%FROM%) label:inbox older_than:30d'
                                .replace('%FROM%',from_address));

  for (var i = 0; i < threads.length; i++) {
    label.addToThread(threads[i]);
  }
}

function _getFilingLabel(label_text) {
  var label = GmailApp.getUserLabelByName(label_text);

  if (label == null) {
    var label = GmailApp.createLabel(label_text);
  }

  return label;
}

function addfilelabel(from_address, label_text) {
  var label = _getFilingLabel(label_text);

  var threads = GmailApp.search('from:(%FROM%) label:inbox older_than:30d'
                                .replace('%FROM%',from_address));

  for (var i = 0; i < threads.length; i++) {
    label.addToThread(threads[i]);
  }
}

/**
 * Scan the given label and archive any items that aren't currently in the inbox
 */
function removeinboxlabel(label_text) {
  // Every thread in label is archived to remove the inbox label.
  var threads = GmailApp.search('label:"%LABEL%"'
                                .replace('%LABEL%',label_text));
  for (var i = 0; i < threads.length; i++) {
    threads[i].moveToArchive();
  }
}

function incorrectfiled(label_text) {
  // Checks for items incorrectly filed in folder with activity newer than 30d.
  var threads = GmailApp.search('label:"%LABEL%" newer_than:30d'
                                .replace('%LABEL%',label_text));
  for (var i = 0; i < threads.length; i++) {
    threads[i].moveToInbox();
  }
}

Note how from_address is placed into the search string using the JavaScript String.replace() method to fill in a placeholder. There's no magic with the placeholder string '%FROM%', this is simply a convention intended to make the placeholder stand out and to decrease the likelihood of accidentally replacing the wrong text.

Now you can call the function like this, which is one simple way to reuse the basic code you've written:

function do_addfilelabel() {
  addfilelabel("[email protected]", "from-user1");
  addfilelabel("[email protected]", "from-user2");
  addfilelabel("[email protected]", "from-user3");
}

From there, you could progress to have a user interface in a Web Application, say. The Web Application could have a simple user interface allowing you to enter an email address and label, then pass those to your server-side function(s) as parameters with google.script.run.