Apps Script with clasp

293 VIEWS

G Suite is a powerful set of cloud-based collaboration and productivity tools by Google. You may not be familiar with the name G Suite, but may have used some popular tools contained in the suite such as Gmail, Docs, Drive, and Calendar. G Suite helps teams boost productivity by providing several tools to get work done faster and leveraging the power of the cloud.

To give G Suite apps even more power, Google introduced Google Apps Script (aka Apps Script). The question now is: what is Apps Script? Apps Script refers to a serverless JavaScript runtime for G Suite that makes it possible for adding extensions, integration, and automation. Think of it as a language for writing plugins for G Suite apps. Apps Script is based on version 1.6 of JavaScript, with some features from version 1.7 and 1.8. Given JavaScript’s monopoly on the web, and its growing popularity in other domains, developers can quickly get started with Apps Script. Apps Script truly gives developers full control over apps by allowing them to build things from simple, useful macros to complex addons and even webapps that interface with third-party services.

I wrote about Apps Script in a previous post. You should check it out as this article assumes some basic knowledge of Apps Script. Additionally, you’ll need some context from the previous article for this piece.

The main focus of this article is the clasp tool, as the title already points out. The focus of the rest of this article is on the tool. clasp is a command line tool that is used to create Apps Script projects locally. Local development of Apps Scripts gives developers the freedom to collaborate easily on projects using version control and their preferred IDE. Other benefits of clasp are that it enables code structuring and provides TypeScript support. The latter is even more exciting. TypeScript is a superset of JavaScript, with static type-checking and other advanced features that make it suitable for many use cases.

Write a Script

clasp is built with Node.js and is available on npm. To install and use clasp requires you to have Node.js installed.

// install tool globally 

npm i @google/clasp -g
yarn global add @google/clasp  

Verify your installation by typing clasp in the terminal. If you see a usage guide, then your installation was successful. Otherwise, you may have to ensure that npm or yarn is working correctly.

A login is required to start creating scripts. However, the Google Apps Script API must be enabled first. To log in, type clasp login and grant the required permissions via the web interface. In the case that the login fails, you may try this command instead, and follow the command line instructions:

clasp login --no-localhost

Once login is successful, you have the greenlight to create the sample script. For our simple hello-world script, we’ll create an App Script with title hello-world. The script type is standalone; the default. This means the project is not connected to a single G Suite app such as Docs, or Slides.

clasp create --title hello-world

You should see a file, or two files (that’s if you see hidden files): .clasp.json and appscript.json. The next steps involve creating a source file for our Apps Script. For this example our file is called hello.gs. Take note of the file extension. The hello.gs script will have the following content.

 function helloApp() { 
  // get current document
   var doc = DocumentApp.create('created with clasp');

  
  // set footer text
  doc.addFooter().appendParagraph(new Date().toDateString()); 
}

This script creates a new Google Doc document, with the title created with clasp, and the document creation date in the footer. This doesn’t solve a complex issue; nonetheless, it demonstrates how to begin G Suite automation and provides the basics for creating far more useful and sophisticated scripts or apps. Use clasp push and clasp open to push the script to the cloud and open it for execution respectively.

From the Apps Script dashboard, run the script; then, head over to Google Docs and view the newly created document file with the title provided and the date as footer text.

Closing Thoughts

Apps Script is a vital tool for taking advantage of the great G Suite tool collections. It gives developers power to automate, extend, and integrate with various tools. The example in this article demonstrates only a small aspect of Apps Script. If your team uses any G Suite apps, try automating several tasks like document creation, and share your experience in the comment section. Happy hacking!


Bruno is a junior at Ashesi University College studying Computer Science. He is interested in leveraging the power of technology to increase productivity. As a big fan of open source technology, he is currently exploring the possibility of using the Bitcoin Blockchain to fight corruption in government.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu