Automate tasks across Google services
If you use Google services like Google Docs, Google Sheets, Google Analytics, Gmail, and others, you could connect and automate those services using Google Apps Script.
Think of Google Apps Script like Google’s version of Microsoft’s VBA script. Just like you can automate actions and tasks or customize macros in Microsoft Word and Excel with VBA, you can automate tasks and actions across Google services. In services like Sheets and Docs, you can even write your own custom menus.
In this article you will learn how to access and enable Apps Script in your various Google services, how to navigate the Apps Script Editor, and how to connect services. You won’t learn specific script functions, but Google has excellent documentation and apps script tutorials available to learn how to write Apps Script.
How to Access Google Apps Script Editor
You can open the Google Apps Script code editor from within a number of Google services. For example, you’ll find Apps Script in the Extensions menu inside Google Sheets.
In other services, you can open the Google Apps Script editor in the following ways:
- Google Docs: Select Script editor in the Tools menu.
- Google Slides: Select Script editor in the Tools menu.
- Google Forms: Select Script editor in the three-dot menu.
- Google Drive: Right-click any blank space, select More, and select Google Apps Script.
Using any of these methods, you’ll see the Apps Script code editor open in a new tab. This is the window where you’ll write each of the functions that make up your entire script. By default, you’ll see an empty function named myFunction() that’s ready for you to start filling in your code.
Note: Code formatting is very important in order to avoid errors. Use commenting as shown in the following code to remind yourself what you were trying to do inside sections of code. This is very similar to how commenting works in HTML code with web programming.
As you navigate the code editor, you can return to this section by selecting Code.gs in the left navigation pane in the Editor window. To see other available windows, hover over the icons in the far left pane and the main navigation pane will open.
The Overview section is where you can find statistics about your script like how many errors have occurred, how many times it’s executed, and more.
We’ll cover each of the other sections of the Google Apps Script editor in each section below.
Navigating the Google Apps Script Editor
As you edit your code in the editor, it’s a good idea to select the disk (Save) icon often so that you don’t lose your work.
Once saved, you’ll see the other menu options light up.
These include:
- Run: Attempt to run your entire script from beginning to end.
- Debug: Step through your script one line at a time.
- Function dropdown: Browse through and navigate to each of the functions you’ve created.
- Execution log: See any status or error messages from each attempt you’ve made to run your script.
The Libraries option in the left navigation menu is where you can access libraries that other people have written (or you’ve written and saved elsewhere). This is useful if you have a friend who already wrote a feature you’d like to use in Google Sheets or Google Docs but you’d like to add additional features on top of that.
All you need to add those libraries into your project is the Script ID. You can find this in the project settings section, which we’ll show you how to find toward the end of this article.
Google Apps Script Services Add-Ons
The Services section is the most useful. It’s where you can integrate your current script with other Google services you may use.
When you select it, you’ll see the Add a service window open. Scroll down to the service you want to use as an add-on to your existing project.
For example, if you’d like to pull data from your Google Analytics account into this script, you can select the Google Analytics API, and select Add.
If you want to find details about what functions are available to use for that new add-on service and how to use them, select the three dots to the right of the API and select See documentation.
This will open the Google Apps Script documentation in a new tab, opened automatically to the section for that Google service.
Explore the documentation for function syntax, tutorials, and code examples that you can use in your own script.
Also, note that you can navigate to other sections of the documentation to see what general functions are available in your script based on the service you were using when you originally opened the Apps Script code editor.
For example, if you opened the editor in Google Sheets, check the Sheets section in the documentation menu for Google Sheets functions you’re able to use in your script.
Setting Up and Using Apps Script Triggers
Another useful feature in Google Apps Script is the ability to set triggers based on a number of events or schedules.
To configure a new trigger for your script, select Triggers from the far left navigation menu. In the new Triggers window that opens, select the Add Trigger button.
The Add Trigger window has a long list of options that help you customize exactly how and when you want your script to run.
Note: Many of these options depend on the service you’re writing your script for or the APIs that you’ve added.
To set up your trigger, you’ll need to choose:
- Which function to initially launch
- The event source such as a specific time, date, or an event in your service like when a Google spreadsheet cell changes or a document is initially opened
- The event type such as when something in your Google spreadsheet or document is opened or edited, or the specific setting for date or time
- Notification frequency for how often you want to get updates about when your scripts have failed
Once you select Save, you may see a “Script authorization failed” message if it’s the first time you’ve saved a new trigger.
This is usually triggered if you have a pop-up blocker enabled in your browser. If you’re using Google Chrome, just select the small window icon with a red “X” over it. Change the setting to Always allow pop-ups and select Done.
When you select Save again, you’ll need to step through the process to authorize the script you’ve written to run under your Google account or Google Workspace.
First, select the Google account that you want to allow your script to run under.
You’ll see a warning that the custom function or script you wrote isn’t “verified” by Google. If you’re the person who wrote the script, then this doesn’t matter and it’s safe to run under your own Google Account or Google Workspace.
To bypass this warning, just select Advanced and then select the Go to <project> (unsafe) link at the bottom.
Finally, in the permissions window, select Allow to allow your custom functions and script to run under your Google account or Google Workspace.
You won’t have to repeat this process again, only the first time you save or run your custom Google Apps script projects.
Accessing Your Google Script ID
One final note – you may want to provide your script to friends or colleagues so they can either use your script or add it as a library to their own script.
You can find your Script ID under the Settings icon in the far left navigation pane.
The Script ID can be found under the IDs section, to the right of Script ID.
As you can see, the Google Apps Script editor is fairly straightforward if you know how to navigate to each feature you want to use. Just make sure to study Google’s Apps Script documentation well so that you can start learning how to write your scripts and all functions that are available to use.