Reader small image

You're reading from  Learning Google Apps Script

Product typeBook
Published inMar 2016
Publisher
ISBN-139781785882517
Edition1st Edition
Right arrow
Author (1)
Ramalingam Ganapathy
Ramalingam Ganapathy
author image
Ramalingam Ganapathy

Ramalingam Ganapathy is an independent computer software professional with more than 15 years of working experience of JavaScript and Google Apps Script. In 1985, he started his career as a digital electronic circuit designer and service engineer. Highly interested in reading technical books and building electronic projects, he is a detail-oriented and logical person. Since 2001, he has been freelancing with Elance and Upwork (formerly oDesk). He earned a good reputation on the Upwork portal, and most of his clients are satisfied.
Read more about Ramalingam Ganapathy

Right arrow

Chapter 9. More Tips and Tricks and Creating an Add-on

In the previous chapter, you built an order processing workflow application. In this chapter, you will learn:

  • To overcome script maximum execution time restriction

  • To use script codes from other script files or libraries

  • Create add-ons that use the OAuth2 external library

Overcoming the "script exceeded maximum execution time" error


What if one of your script functions has a bug that causes endless (not terminating) execution, for example, an endless for loop and/or while loop. There are no remedies other than carefully examining the loop terminating statements.

Sometimes, your script may be flawless or bug free, but if it needs to handle a large spreadsheet or external data, it may take a long time to complete the execution. The maximum allowed time for your script to run continuously is 6 minutes. If it exceeds that limit, GAS would throw the "Exceeded maximum execution time" exception.

To overcome this bottleneck, you can follow these steps. For example, if your doLengthyProcess function takes a long time to finish, then manually create a minute's trigger for the doLengthyProcess function so that it executes every 10 minutes...

Configuring your script project to use external libraries


Sometimes you would like to reuse code from other script project(s) or other programmer's code in your projects. You can import external code as it resides in the current project. You need to make a simple configuration in your current project.

For an example, we will explain how to import the previous chapter's code into the current project.

  1. Open any one of your previously created scripts (for example, Chapter 8, Building a Workflow Application) in the script editor, save a version if you haven't already.

  2. Now, click on the File menu and then Project properties. The Project properties dialog will open as shown here:

  3. Copy the Project key value (this value should be different for your project).

  4. Open the current script, navigate to Resources | Libraries…, and then the Included Libraries dialog will open as shown here:

  5. Paste Project key (you already copied in step 1) into the Find a Library textbox and click on the Select button.

Now, the Chapter...

Using JSDoc annotations


In the preceding test function, you can see that the code hint became active as soon as you type a '.' next to the identifier name (Chapter8). This shows all the functions and global variables available in the external library as shown here:

The preceding code hints are generic, for example, index shown as Object. For detailed code hints, you should use the JSDoc style documentations (annotations or comments at top lines of function definitions).

For example, if you used the following annotations to the getPrice function in Chapter 8, Building a Workflow Application:

/**
 *  Returns price list data from the Stock tab/sheet
 *
 *  @param {number} index 
 *  @return {array}
 *
 */
function getPrice(index){
          …
}

Then the code hint would be as shown here:

Now, you can notice how the code hint returns with useful information for the getPrice function.

Using the OAuth open source library


If your application interacts with external libraries other than Google's basic services, then it should be authenticated. In other words, if your application runs on behalf of a user, then that user should authorize your application to grant access to his/her data. GAS does not provide any built-in authentication service, but you can use an open source OAuth library.

Creating, testing, and publishing add-ons


If you need to use other external libraries in your current project, you need to know the project key and you should have at least read access to that project. At the same time, every new version of the master project will not reflect in the client project unless the client selects the current version. Add-ons override this configuration hassle.

Add-ons are installable scripts by the click of a button, no configuration required. You can install add-ons in Sheets, Docs, and/or Forms published by the other programmer or from the Google Chrome Web Store.

Installing add-ons from Chrome Web Store

To install an add-on from Chrome Web Store, open the document (Sheets, Docs, or Forms) and click on Get add-ons… from the Add-ons menu. Select any one of your favorite add-ons from the Add-ons dialog (if you hover your mouse over any add-on, then a plus symbol will appear on the application; click on it and authorize if required). A sample Add-ons dialog is shown...

Summary


In this chapter, you learned how to overcome script maximum run time restrictions, how to import external libraries, how to use OAuth, and how to create an add-on. We hope you enjoyed reading this book, learning, and gathering hands-on skills on most aspects of the Google Apps Script. Happy coding and enjoy!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Google Apps Script
Published in: Mar 2016Publisher: ISBN-13: 9781785882517
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ramalingam Ganapathy

Ramalingam Ganapathy is an independent computer software professional with more than 15 years of working experience of JavaScript and Google Apps Script. In 1985, he started his career as a digital electronic circuit designer and service engineer. Highly interested in reading technical books and building electronic projects, he is a detail-oriented and logical person. Since 2001, he has been freelancing with Elance and Upwork (formerly oDesk). He earned a good reputation on the Upwork portal, and most of his clients are satisfied.
Read more about Ramalingam Ganapathy