DEV Community

Vinit Jogani
Vinit Jogani

Posted on

Why Google AppScript is a real blessing

The Problem

As a developer, sometimes you are working on small-scale projects e.g. a personal project or as a short-term freelance gig. The requirements for these projects are usually not scalability or extendability, but rather getting the job done as quickly as possible for cost efficiency.

Something about this whole process that has personally frustrated me in the past is that the client may keep requesting small changes, or update the data slightly, or request data snapshots. Obviously, something you could do is build a configuration screen in your application to let the client tune the knobs. You could also build in functionality for exporting/importing data more easily. But all of this has a lot of overhead in terms of development time!

Why spend all of this time reinventing the wheel when you could leverage the existing functionality of an app in the Google Suite? For example, you could provision a SQL database, but if all you need is to save POST data from a few forms and organize them with some algorithmic logic, why not just save the data to a Google Sheets? Sure, there are a lot of extra features that a SQL database would provide in terms of performance, advanced queries etc. However, if you are expecting ~5-10 users for your project and the client cares only about the functionality, this can be of significant value because suddenly the client gets all the functionality that Google Sheets provides without any additional costs. They can now use this Sheet for creating their own graphs and analyses without having to ask you to implement a full-fledged dashboard! Every small change in the data doesn't need to go through you anymore. The Software Architect within you may cringe from this idea, but as my former manager would say "Speed Wins!".

Here's the second problem - often you need to connect some form of APIs within your app. For example, you might need directions data (e.g. from Google Maps) or you might need to integrate with a mail app (e.g. with GMail), or you might want to automate Calendar events (e.g. from Google Calendar). Setting up all of this requires you to manage API keys and dependencies in your app, and then set up all the boilerplate. This can be a big pain for a small app. You would also need to provision a server to run your app (unless you go serverless). In either case, connecting with these APIs can be a real task!

TL;DR: For small projects, sometimes you just need a quick way to connect some APIs and allow the client to make small changes without going through you. This can be for a personal automation project OR for a short-term freelance gig, but speed and efficiency is of the essence!

What Google AppScript provides

Google AppScript is a lesser known service by Google that allows you to automate several Google services to address the problems above. Some of the key features it provides is:

  • Seamless integration with many Google services (including Gmail, Google Drive, Calendar, Maps, etc.) without any API key management!
  • Publishing as web apps and/or extensions (so you can expose a public API)
  • Ability to setup automatic triggers based on recurring events or emails etc.
  • Familiar JavaScript like syntax (most JavaScript features work, except accessing the DOM).
  • Store user properties across sessions.
  • All of this for free!

A Simple Example

Let's walk through a simple example on how to implement something in Google AppScript. We're going to build a simple API that takes in the name of a person and gets their phone number from your contacts.

Step 1: Head to Google Drive and create a new Google Apps Script:

Step 2: Create a function as shown below.

function getPhones() {
  var contacts = ContactsApp.getContactsByName('Dad');
  for (var i in contacts) {
    var contact = contacts[i];
    var phones = contact.getPhones();
    for (var j in phones) {
      Logger.log(phones[j].getPhoneNumber());
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

This should be pretty self-explanatory. We first get a list of contacts which match some name, then we loop through them and get all their phones, and then we print the phone number for each one of those phones to the logging console.

Step 3: From the toolbar, select getPhones as the function and hit the play button. Alternatively, you can also press Ctrl+R (or Cmd+R on OS X) to run the function. At this point, you will have to grant some permissions so that your function can access your contacts. After granting access, the function should run without any issues. You can then go to View > Logs (or press Ctrl+Enter) to view the phone numbers printed to console.

Step 4: Now it's time to convert this into an API! I am going to update my function to take in a query parameter, and return a list of phone numbers. Then, I am going to create a new function called doGet (the exact name is important) as follows.

function getPhones(query) {
  var contacts = ContactsApp.getContactsByName(query);
  var output = []
  for (var i in contacts) {
    var contact = contacts[i];
    var phones = contact.getPhones();
    for (var j in phones) {
      output.push(phones[j].getPhoneNumber());
    }
  }
  return output;
}

function doGet(e) {
  var phones = getPhones(e.parameters.query);
  var response = JSON.stringify(phones);
  var response = ContentService.createTextOutput(response);
  var response = response.setMimeType(ContentService.MimeType.JSON);
  return response;
}
Enter fullscreen mode Exit fullscreen mode

The doGet function extracts the query variable from the query string, passes it to the getPhones function and then outputs the list of phone numbers as JSON.

Step 5: Now you can deploy your app from "Publish > Deploy as Web App". You can configure some parameters such as who has access, and then hit Publish. This should give you a url. You can then navigate to https://<YOUR_URL>?query=<YOUR_QUERY> and it should work as expected! If you set your permissions right, this should now be accessible by any client app (not very safe if you have sensitive contacts!).

And that's it! Just like that, without any API Key nonsense, or servers, we could deploy a simple app in minutes.

How I've used it so far

Here are some of the use cases I ended up going with App Scripts:

  • I had a team calendar with events named after me. I wanted to turn notifications on for all my events but by default, I would get notifications for every event on the calendar. I wrote an App Script to periodically sync all my events into a personal calendar that I could turn on notifications for.
  • One of the freelance projects I was working on required a hierarchical form where next choices depended on previous selections and at the end of it, we could see an output based on the choices. The dataset for this would likely have changed so I didn't want to keep updating the dataset myself. I automated a data pull from Google Sheets and exposed it as a JSON API for my Vue Frontend to use.
  • I once had to build an MVP for a database design project. The fields for the database were automatically rendered based on an excel template that the user could set up over time without my intervention.
  • I wanted to use the Google Translate API to translate short English phrases into Hindi phrases for an Android app I was building. I didn't want to set up API keys or an app server, and ended up deploying a 10-line function through Google Apps Script.

Overall, I hope you see how this can make life so much easier.

Conclusion

There's a lot more things you can do in this environment but hopefully you get how this is so easy and helpful when you want to automate your workflow or implement small projects! You can check out the documentation to explore the full power that this service provides. (View Docs)

I'd be happy to hear any feedback on this post. Feel free to share any interesting projects you make with this.

Top comments (1)

Collapse
 
mohsin708961 profile image
{{7*7}}

Awesome