Libraries

A library is a script project whose functions can be reused in other scripts.

Gain access to a library

To include a library in your project you must have at least view-level access to it. If you aren't the author of the library that you want to include, contact the author and request access.

You need the script ID of the library you want to include. When you have access to the library, you can find the script ID on the Project Settings page.

Add a library to your script project

  1. At the left of the Apps Script editor, next to "Libraries," click Add a library .
  2. In the "Script ID" field, paste in the script ID of the library.
  3. Click Look up.
  4. Click the Version dropdown and select the version of the library to use.
  5. Check to see if the default "Identifier" name is the one that you want to use with this library. This is the name that your script uses to refer to the library. For example, if you set it to Test then you can call a method of that library as follows: Test.libraryMethod().
  6. Click Add.

Use a library

Use your included library as you would use a default service. For example, if Test is the identifier for your library, type Test immediately followed by a period to see the list of methods in the library.

The reference documentation for an included library can be opened by following these steps:

At the left of the script editor, next to the library name, click More > Open in a new tab.

Remove a library

At the left of the script editor, next to the library name, click More > Remove > Remove library.

Update a library

You can change the version of the library or update its identifier.

  1. At the left of the editor, under "Libraries," click the name of the library.
  2. Make your changes and click Save.

Create and share a library

To use and share your script project as a library, follow the below steps.

  1. Create a versioned deployment of your script.
  2. Share at least view-level access with all potential users of the library.
  3. Give those users the script ID, which can be found on the Project settings page.

Best practices

Here are some guidelines to follow when writing a library:

  1. Choose a meaningful name for your project since it's used as the default identifier when your library is included by others.
  2. If you want one or more methods of your script to not be visible (nor usable) to your library users, you can end the name of the method with an underscore. For example, myPrivateMethod_().
  3. Only enumerable global properties are visible to library users. This includes function declarations, variables created outside a function with var, and properties explicitly set on the global object. For example, Object.defineProperty() with enumerable set to false creates a symbol you can use in your library, but this symbol isn't accessible by your users.
  4. If you want your library users to make use of the script editor autocomplete and the automatically generated documentation, you must have JSDoc-style documentation for all your functions. Here's an example:

    /**
     * Raises a number to the given power, and returns the result.
     *
     * @param {number} base the number we're raising to a power
     * @param {number} exp the exponent we're raising the base to
     * @return {number} the result of the exponential calculation
     */
    function power(base, exp) { ... }
    

Resource scoping

There are two types of resources when you are working with libraries: shared and not-shared. A shared resource means that both the library and the including script have a built-in access to the same instance of the resource. The following diagram illustrates a shared resource using the example of User Properties:

Shared Resource

A not-shared resource means that both library and the including script have built-in access only to their instance of the resource. However, a library can provide access to its not-shared resources by having explicit functions that operate on them. Here is an example of a function that you would include in your library to expose its Script Properties:

  function getLibraryProperty(key) {
    return ScriptProperties.getProperty(key);
  }

The following diagram illustrates a not-shared resource using the example of Script Properties:

Not-Shared Resource

This table lists the shared and not-shared resources for your reference:

Resource Shared* Not-Shared** Notes
Lock The same instance is visible to all including scripts when created in the library.
Script Properties The same instance is visible to all including scripts when created in the library.
Cache The same instance is visible to all including scripts when created in the library.
Triggers Simple triggers created in library are not triggered by the including script.
ScriptApp
UiApp
User Properties
Logger and execution transcript
Sites, Sheets and other containers A call to getActive() returns the container of the including script.
MailApp and GmailApp
* This means that the library does not have its own instance of the feature/resource and instead is using the one created by the script that invoked it.
** This means that library has its own instance of the resource/feature and that all scripts that use the library share and have access to that same instance.

Test a library

To test your library, use the head deployment. Anyone who has editor-level access to the script can use the head deployment.

Debug a library

When you use the debugger in a project that includes a library you can step into a function of the included library. The code shows up in the debugger in view-only mode and at the right version.