What Is Google Script? How to Write Your First Google Apps Script
If you use Google Apps like Google Sheets or Google Docs, Google Script allows you to accomplish things you could never could with a similar desktop application.
Google Script (also known as Google Apps Script) is an application development platform that lets you integrate all of the Google Cloud services you use.
Google offers a long list of APIs for each one of their cloud services. By writing very simple Google apps, you can open up an entire world of additional features in each one of Google’s many services.
What Can You Do With Google Script?
Learning Google Script is very easy. Just a few of the things you can do with Google Script include:
- Creating custom functions in Google Sheets
- Integrating Google Sheets or Google Docs with Gmail
- Creating web apps you can deploy using Google Sites
- Adding a custom menu to Google Docs
- Creating web traffic dashboards in Google Sheets using Google Analytics data
- Sending email from Google Sheets or any other Google service
Since Google services are all in the cloud, you can create your Google Apps script from a single script editor. From that code you can tap into the APIs for any of Google’s services you use.
This creates a flexibility that’s hard to find in most other scripting platforms.
Writing Your First Google Apps Script
To see how easy it is to write a Google Script, try the following example.
Your first script will send an email from your Gmail account, with a message embedded in your Google Script.
- Open a web browser and type script.google.com into the URL field.
- Log into your Google account if you aren’t already.
- Once logged into Google Apps Script, click on New script.
- Where it says Untitled project, type the name My First Script.
Delete the code in the script window, and paste the following:
function SendAnEmail() {
// Set the recipient email address
var email = 'xxxxx@yahoo.com'
// Create the email subject line.
var subject = 'This is my first script!';
// Create the email body.
var body = 'Hello, world!';
// Send an email
GmailApp.sendEmail(email, subject, body);
}
Click on the disk icon to Save the code. Then click on the Run icon to run it.
You may need to provide permission for the script to run using your Google account the first time, and to send email from your Gmail account.
You may see a warning that the app isn’t verified. Just click on Advanced and Go to My First Script (unsafe). Since you’re the one who wrote the app, you can be confident that it’s perfectly safe to run.
Here’s what that incoming email will look like:
This script used the Gmail service to send an email from your account via Google Script.
It’s just one simple example of how a Google Apps Script can tap into any of your Google cloud services.
Adding More Features to Google Services
You’ll find access to Google Scripting from inside of many of Google’s services as well.
As mentioned above, this lets you add extended features to those services. For example, inside of Google Sheets, you can access your Google Script Editor by clicking on Tools, and then clicking on Script editor.
We’ve previously looked at examples of creating custom functions and menus in Google Sheets. That’s an excellent resource to get started building your own.
You can access the Google Scripts editor the same way from Google Docs.
You can even use a Google Script to create a Gmail add-on to enhance your Gmail experience. This lets you modify things like the message compose window or even writing a script that manages archiving old emails.
To enable developing and testing Google Script add-ons, you need to enable developer add-ons in your Gmail account. You can do this by going to Settings, then clicking on Add-ons and selecting Enable developer add-ons for my account. You’ll need to click Enable on a pop-up window as well.
Creating Gmail Add-ons is beyond the scope of this article. But if you’re serious about diving into this, you can study the Google Developer Guide on Building a Gmail add-on.
Accessing Google Script APIs
From within your Google Script editor, you can access each of your Google services by using a global object. You used the GmailApp global object in the Hello World example above.
To access all of the features (methods and calls you can make from within the Google Script editor), you just need to enable Advanced Google services for that service.
You do this from within the Google Scripts editor by clicking on Resources and Advanced Google services.
Don’t forget to click on the Google Cloud Platform API Dashboard link at the bottom and enable the service in that dashboard as well.
Once you’re in the Google Cloud Platform API Dashboard, click on Enable APIs and Services, search for the name of the service in the API Library, select it, and then click on Enable.
You only need to enable the advanced service once for each Google account you use for scripting.
If you scroll down on this page where you’ve enabled the API Library, you’ll notice a link for Reference documentation. Save this link, because it provides valuable examples and syntax on how to integrate with that API inside your own Google Scripts.
You can see an excellent example of integrating with Google Analytics using Google Scripting in our guide to creating a website analytics dashboard.
Browsing the API Library shows just how many Google Services you can integrate into your scripts.
There are hundreds of available APIs.
If you are interested in becoming a Google superuser, learning how to use Google App Scripts is definitely the way to go.
Beyond Google Script Basics: What Now?
As you can see from the simple Hello World example above, learning how to write a Google Script isn’t hard at all.
And since Google provides an extensive Google Apps Script library filled with guides and references, you have a solid base to start from.
If you want to get started on a slightly more advanced Google Script application, try your hand at integrating Google Forms and Gmail to quickly apply to job openings.
Read the full article: What Is Google Script? How to Write Your First Google Apps Script