How to Colorize Google Calendar Event Using Apps Script (2/2)
Automatically colorize your calendar events using the power of Google Calendar and Google Apps Script platform.
In the previous part, we have learned how to use Google Apps Script to colorize our Google Calendar events. However, it was very basic and you would have to go through that rigorous process of launching your Apps Script project then running the project and what not. In this part, we will automate the same by deploying our project as a web module and setting up an IFTTT applet which will get triggered every time a new event is added.
Outline
Google Apps Script requires doGet()
and doPost()
methods to handle GET and POST requests respectively. The structure might be the same but we will be using doPost()
method.
Every new event added in the calendar will cause IFTTT to invoke the doPost()
method and subsequently assign_color()
method that we previously wrote. (ref)
Let’s start coding, shall we?
doPost() Method
We will append to the script that we wrote in the previous part. (link)
function doPost() {
assign_color();
}
Wait, what? Is that it? Well, yes that is pretty much what goes into out doPost()
method! Although, we might want to make a slight optimizations to our script. As of now, anytime the script is getting executing, it is iterating through every date from 01/01/1970 to 01/01/2500. As you add more event, this process might take enormous amount of computing down the road. So, we will limit the amount of events that are being iterated in the process. It is better to process every event from 7-days ago to practically every events in the future. I hope you get the idea.
var beginDate = new Date(new Date().getTime() - (7 * 86400000));
var endDate = new Date(2500, 0, 1);
NOTE: Integer ‘7’ in the
beginDate
represents days.
Deploying As Web App
Click on ‘Deploy as web app…’. It will prompt a dialog box. Select “New” for the project version. For Execute the app as select Me (your email address) and finally for Who has access to the app select Anyone, even anonymous. Here is what each of those settings do.
- Execute the app as: When you request to the URL given at the time of deploying, it will execute as the user who deployed it. In this case, it is you.
- Who has access to the app: This setting indicates who has access to this URL. In this it is practically anyone with the link. The reason we might want to enable this setting is that by using IFTTT services, you will not get authorization prompt.
IFTTT
According to Wikipedia, If This Then That, also known as IFTTT, is a free web-based service to create chains of simple conditional statements, called applets. It is truly an amazing service for any kind of automation.
Sign up with IFTTT and we will start creating our first applet! Go to https://ifttt.com/create
Step 1
You will arrive at following screen.
Step 2
Select ‘Google Calendar’ for This part of the trigger.
Step 3
Choose ‘New event added’ trigger from the available list of Google Calendar triggers.
Step 4
Select your calendar from the list. You might want to authorize your IFTTT account with Google.
Step 5
Now, we will go to That part of the applet.
Step 6
Choose “Webhooks” for That part of the applet.
Step 7
Choose “Make a web request” action. That might be the only action available under “Webhooks” service.
Step 8
Enter URL that you obtained while deploying the project and select ‘POST’ as HTTP Method.
Step 9
Save and name the applet. Make sure it is enabled and you are good to go!
Testing
Create an event in the appropriate calendar using the naming convention that we discussed before. Wait for a couple of seconds and it will colorize the event automatically! There is so much more optimization that we can do on Apps Script. Let me know in the comments if you want to see those as well.
Conclusion
I hope this article was helpful! Thank you for taking time into reading.
Please share it if you found this article to be helpful. :)
Link to part one: https://medium.com/@dhavalsavalia/how-to-colorize-google-calendar-event-using-apps-script-1-2-d88dd27658f0