8 Things You Must Know to Get a Head Start on Google Apps Script
Google Apps Script is a revolutionary way to get most out of Google services. You may want to write your own script to publish as a Google Add-on or for your own custom use. Either way, knowing several things can help you a lot in getting up and running quickly. They are listed below,
1. Storage System
Most often you would like to store some data that you'll access later within the script. An app may need 2 types of storage, temporary and persistent. Apps script has both. Persistent one is called Properties service and temporary one is called Cache service.
Let's take Drive Explorer add-on as an example. It stores user configuration permanently (Properties Service). When it batches the file fetching due to script run time limitations, it stores the intermediate data in Cache service as it'll be fetched and used within the next 5 minutes. Cache service can hold the data for up to 10 minutes.
Both services have 3 scopes namely Document
, Script
and User
, you have to choose one based on your needs.
2. Recurring Tasks and Triggers
In your scripts, you may want to run some functions at a specific point in time repeatedly or every n minutes or hours or days. You can also run some custom functions written by you upon document or spreadsheet open, edit. You can make use of Installable triggers.
For instance, an installed trigger of Google Drive direct links generator add-on fetches direct links of your Google Drive files and updates the sheet whenever you open the spreadsheet.
Simple triggers are another types of triggers you must know about. They are basically used to set add-on menus. Their main limitation is you can't perform actions involving services that require authorization.
3. Services
Usually you use Apps script services to access various Google products. Every Google service has its own classes and methods that let you do various operations on behalf of users. For instance, you'll use DriveApp
for Google Drive, GmailApp
for Gmail.
4. Know when to use Advanced APIs - Script optimization
Your script may start to consume a lot of time to complete its execution. One of the reasons could be you make a lot of queries to services in your code. To explain this better take a look at Direct Drive Links add-on spreadsheet, it has the parent folder, name, share details of every file on Google Drive.
If you try to obtain all the information using standard services, on File object, you have to call getParents()
to get parent folders, getName()
for name, getEditors()
to obtain who has edit access, getViewers()
to know who has view access, getId()
to get the file id. That means you've to make 5 queries for each files.
Let's just say you've 5000 files on your Google Drive. So your script will make 25000 queries to complete execution. This will ultimately make your script too slow. You can make use of advanced APIs to eliminate this trouble.
Single Drive.Files.list
API call can fetch you up to 1000 files with all the above-mentioned details, but the max limit depends on the fields you require. To use advanced APIs, you've to enable it beforehand. Within your script editor go to Resources → Advanced Google Services.
Knowing the best practices will also come in handy.
5. Different Auth modes
There are several types of auth mode an user can be in. You can read more - Add-on authorization life cycle. Each auth mode has its own limitations. Every action you perform checks whether you're authorized to do it or not. Without proper authentication, script execution fails.
6. UI guidelines
If you're not planning to publish your script as an add-on, you don't have to bother about UI. Your script may have dialogs, buttons, forms etc. Whenever you build any UI components, make sure you adhere to UI style guide.
This is mainly to boost user experience and consistency in look and feel of Add-ons. If you fail to meet the requirements, your publish request will be rejected and you'll be asked to fix things before submitting again.
7. Quotas
Though Google Apps Script is totally free to use, each service comes with rate limits. Knowing this will help your script perform consistently.
8. Publish Guidelines
Your script must go through a review process if you want to publish it as an add-on. In my experience, it'll take at least 4 days to complete the review process. Make sure you follow each and every instructions mentioned in this guide. If you fail to do so, your add-on will fall into rejected state and you have to wait at least 4 long days to get it verified again before publishing.