Saturday, December 7, 2013

Google App Script: How to set your personal assistant to send B'day wishes automatically?

Today we will explore a simple Google App Script which will send out the Birth day wishes to our friends; the beauty of the solution is that it will send out the Happy B'day messages, not matter whether we are online or not; we remember the b'day date or not; No matter we have internet connection or not; once you setup the Google App Script it is going to act as a personal assistant to send B'day wishes.

The idea is we will keep the name, DOB and email address of our contacts in the Google Spreadsheet. We will develop a Google App Script which will read the DOB of each of the contacts and if the DOB is same as today date it will sent out a birthday message to the contact’s email address. The Script will be setup as a job which will run automatically once every midnight.

For the demonstration purpose, I have taken some dummy name, DOB and email address. I have kept my email address in the spreadsheet so that all b’day messages are send out to my email address; in reality we will keep our friends name, DOB and their email address in the spreadsheet.

Once our Spreadsheet file is ready we need to go to Tools -> Script Editor. It will open the Script editor window. Delete everything written by default in the code editor and write following App Script.

function SendBdayWishes() {
  
  /* --- Set Bday Message --- */
  
  var Bdymsg="Wish You a very Happy Bday, God bless you always!!!";
  
  /* --- Reading Today's Date --- */
  
    var todayDate=new Date();
    var dayOfToday = Utilities.formatDate(todayDate, "GMT-2","MM");
    var monthOfToday = Utilities.formatDate(todayDate, "GMT-2","dd");
   
  
    var mySheet = SpreadsheetApp.getActiveSheet();   
    var frndData = mySheet.getDataRange().getValues();   
  
    var frndDay;
    var frndMonth;
  
  
  for (var i = 0; i < frndData.length; i++) 
  { 
    
   /* --- Reading Day and Month from Date of Birth Column  --- */
    
    frndDay = Utilities.formatDate(new Date(frndData[i][1]), "GMT-2","MM");
    frndMonth = Utilities.formatDate(new Date(frndData[i][1]), "GMT-2","dd");
    frndEmail=frndData[i][2];
    
    if((frndDay==dayOfToday) && (frndMonth==monthOfToday))
    {
      GmailApp.sendEmail(frndEmail, "Happy Bday" , "Dear " + frndData[i][0] + " " + Bdymsg)       
    }
    
  
  }
}

Once you are done with the writing Script you can click on run button to test your script. If the script runs successfully and there is a DOB today an email wishing Happy B'day will be sent to that email address.

Now we will set this script as a Job which will run every midnight and check to see if there is a b’day today and send the message subsequently.

To do so, click on the Current Project Trigger button (watch shape button).

It will show the list of project triggers setup for this project or spreadsheet. Click on the link “Click to add one now”.

This will take us to the project triggers window; select the function name, Event and time when you want to run this Google App Script. Click on Save to return.

That is all you need to do. The Google App Script will take care of running the function every midnight and sending the b’day wishes. Next time you have a new friend; just add his/her name in the spreadsheet. You can definitely call or wish your friend on facebook or twitter but this script will wish him even if you forget or got busy in doing so.

I setup the DOB for one record to be today date for testing purpose and I got the email wishing Happy B'day.

For more Google App Script visit the Google App Script section.

Popular Posts

Real Time Web Analytics