GMail Scripts: Putting an automatic label on new e-mails

The last couple of posts have been tech related due to heavy work and/or lack in activity on other interesting topics I like to yap about. Football season is over and summer seems to be finally setting in (although the current weather has it’s own ideas) . Anyway, I’ve been going over some JavaScript ideas and I ran over regular expression the other day. I thought it might be a good idea to incorporate regular expression into something a bit more useful.

A couple of months back, while I was learning JavaScript apps, I came upon Google scripts. It seemed powerful and useful but my limited JavaScript knowledge didn’t allow me to do anything useful with it. A month or so later, I found out that I was getting lots of emails that are work related and didn’t want to miss/delete any by mistake. Thankfully, GMail offers ‘labels’ which is just a simple way of marking your emails so you can easily differentiate them. I, manually, labelled my work emails for a couple of days until I realized how pointless it was. If I can mark them manually, then it becomes meaningless to use labels in the first place. And if I miss any, it would be even harder to notice the mistake. Then I remembered Google script and wondered if I could use it to automatically label all incoming emails.

For some background, Google script (actual name: Google Apps Script) is based on JavaScript and as a result, it is very easy to learn. It employs most syntax used in JavaScript, although there are differences to keep in mind. It’s very useful in doing personal projects and I would recommend it to anyone currently learning JavaScript. A huge benefit of using this is knowing that you can schedule your scripts to run at specific times due to being hosted on Google Drive. My script runs once every hour but that can be changed to even once every minute.

I usually receive my work emails from a web-app ticketing system. This usually leads to the emails subject having specific keywords that are common in every work-related email. Hence, I can easily employ Regular Expressions to search for these keywords and apply labels to any new email that contains them. As long as the keywords are unique for work-related emails and all work-emails contain them, I won’t risk losing any mis-labelled emails. The script can be found here. You might need to be logged into google to view it. You’ll also need to set up a ‘script trigger’ which is essentially a way of allowing google to know when and how often to run the script.

I pasted the script below, in case it isn’t visible from the above link:

var label = GmailApp.getUserLabelByName(“MY LABEL NAME”); //getting a label name you’ve specified
var firstThread = new Array(); //An Array that will contain your emails
var matching = /keyword/g; //the keyword you’ll be searching
function lab (){
 for (var i = 0; i<20; i++){ //you can set whatever number depending on the volume of emails you receive on average
  firstThread.push(GmailApp.getInboxThreads(0,50)[i]); //getting all the inbox messages
  var testing = firstThread[i].getFirstMessageSubject().match(matching); //checking whether the 'keyword' in the subject
   if (testing == "keyword"){ //checks whether testing returns match
   label.addToThread(GmailApp.getInboxThreads(0,50)[i])//adds the label to the message thread
   testing = null; //emptying testing before searching the next mail
  }
 }
}

//Couple of useful things to point out:
//- I search for threads because it's easier to look for keywords and label the whole thread instead of labelling individual emails
//- Ensure that the keywords are unique enough to not label emails that aren't related

Advertisements

Javascript Function Declaration vs Function expressions

Anyone familiar with javascript will know function objects are one of the most important parts, if not the most important reference type. Hence it comes with great confusion when they learn that functions can be expressed in various ways. From object elements to stand-alone variables, functions are usually flexible and can be appended to unlimited scenarios.

There are two main ways, however, in which we write javascript functions. There is the function declaration:

function myFunction(argument){
  return argument;
}

And there is the function expression:

var myFunction = function(argument){
  return argument;
}

The question on which style is preferred relies solely on preference of the user. Generally, both ways are acceptable and mostly interchangeable. Majority of people tend to use both without a preferred standard. Chances are when using one, the other can be substituted with no observable differences. There is however a minor but very crucial difference between the two. Consider a function that takes two arguments and returns their product. Below are both methods that can be used to call the function:

FUNCTION EXPRESSION:
var iMultiply = function(x , y){
  return x * y;
}
 
FUNCTION DECLARATION:
function iMultiply(x , y){
  return x * y;
}

Let’s call a variable product that passes the arguments 10 and 20 in the iMultiply function. To point out the difference, let’s call product before we define our iMultiply function

FUNCTION EXPRESSION:
var product = iMultiply(10,20);
var iMultiply = function(x , y){
  return x * y;
}
 
FUNCTION DECLARATION:
var product = iMultiply(10,20);
function iMultiply(x , y){
  return x * y;
}

In the first instance (i.e. function expression), an error will be returned. In the function declaration example, our code will still work. While javascript usually executes code on a line by line basis, functions are handled slightly differently. Before the code starts to execute, functions are usually pulled to the execution context, a process called function declaration hoisting. Function declarations make the respective functions available for execution regardless of their position. Function expression, on the other hand, limits the scope of the function until the line has been executed. This means the function is useless when called before function has been declared.

Aside from this ‘small’ difference, function declarations and function expressions are pretty much identical and can be interchanged.

Help When Needed

You should probably watch the video above. Although I’m not very fond of TEDx, this particular talk is something that resonates with my experiences. I could talk on and on about various shortcomings of development projects, especially in Africa, but I’d rather speak with anecdotal examples. Besides, who has time for data? /s.

A couple of calendars issues back I ran a project that involved several high-school kids from around the world going to a certain African country to work on a school project. It involved working with a local orphanage-run school with limited resources. If memory serves me correctly, we had to build several classes and do some overall maintenance projects. In short, a typical high school project. Except this one was someone else’s goldmine.

I had already worked with the same orphanage a year prior, but I was never involved with the organization side of things. By then, the previous leader had a house in the area where all of us could crash after a long day’s work. During my time, however, I had to organize our own lodging facilities. This involved contacting various local facilities and the orphanage founder and director who was of tremendous help. All things were sorted out, with the exception of minor unavoidable inconveniences. One can imagine my surprise when three days before my colleagues arrived I received an email from 6000 miles away, asking to prepare $3000 for each of the 13 people involved. That’s nearly $40K for a one month project. In our budget we had a rough total of $2000, not even enough for a single person. I had a mini-crisis and had to lock myself in my room for a while.

The email was from a US-based fund-raising director who was the main contact for the organization’s foreign activities. It turns out, the norm was to charge $3000 for every US college student who wanted to ‘volunteer’ in this school. For that amount they would live in special fully furbished quarters, right next door to the densely occupied dorms while never sharing the same meals. The relevance of their interactions with students were quite limited but the complaints were rarely raised. Luckily, I was a local and could spot them. And this was how our group was expected to interact with the kids. The understanding of the local culture was never brought to attention but the school was expected to revere practices from a country where none of the students and teachers had been in. The act of charging someone to volunteer is obscene, if not insulting. How could you charge me to help you?

Dan Pallotta gave an incredible talk on the perception of development NGOs and how they ought to be run. I do agree with him that such institutions should be held in the same standards as normal for-profit organizations, at least in their budget allocations. You can watch the video below on the talk:

I do have a problem however in the way wish-fulfilment runs rampant within the industry. Lots of capital is directed into projects that range from unsustainable to pure delusion. Majority of the time ideas seem to spring from outside and enforced in the societies.

We are still not sure about the optimal approach to development projects. However, we do know which ones aren’t.