How to create a self-made library with GAS and reuse scripts ~Part 4: Tips and points to note when creating a library~

GAS
この記事は約7分で読めます。

Well, last time I made my own library and tried using it.

If you would like to confirm, please check this article.

How to create a self-made library with GAS and reuse scripts ~Part 3: How to make a self-made library~
This is an introduction of how to use an external library with GAS, and how to publish your own library and reuse scripts. If you think of a function that can be used in common, you can reuse it by creating a library and saving it in a public file. This time we will finally create and use our own library.

By the way, this time I will introduce some tips and points to keep in mind when making your own library.

Please refer to it when creating your own library.

Advertisements

Tips List

Tips Project name is library name

The project name becomes the library name. Therefore, it is good to use a name that allows the user to know what the library can do.

For example, if you give the following abstract names, you will not be able to determine what the library is.

❌ usefull_library
❌  general_method_library

Therefore, it is good to use a name that you can understand what kind of library to do, as introduced in Convenient library.

⭕️Slack API Library for Google Apps Script
⭕️OAuth2 for Apps Script

Tips How to create a function that is not public

When the library is created, the functions for internal use only or the functions that you do not want to access may be included in the library target.

There is a way to make a hidden function in such a case. The following is how to name the function.

function AAA_(){
  ....
}

The method is to add _ (underscore) at the end of the function name.

Note that general function privatization is preceded by _ (underscore) before the function name, but when hidden in the library, the underscore is added after the function name.

Tips Support auto-complete function

You need to create a JS Doc. Paste the js closure complier on github.

google/closure-compiler
A JavaScript checker and optimizer. Contribute to google/closure-compiler development by creating an account on GitHub.

However, unfortunately, not all JSDoc annotations can be used in GAS, and it is stated in the document that only the following can be used.

To be honest, I haven’t tried everything yet, but it is necessary to form a Doc assuming that only basic @param and @return can be used.

Tips Keep all functions in the local scope (use of immediate functions)

As you can see from the major libraries, it’s all wrapped in () from the beginning.

This is a device to prevent unnecessary operation by confining it locally without using the global scope.

Attention List

Attention point 1 Active methods

The library is called in the environment of the side to be used. Therefore, the active method (activespreadsheet/ activesheet/ activecell, etc.) will get the active version of the calling environment’s spreadsheet.

Of course, if it is a method aiming at that, it is good, but please note that you can not use the active method to send the data of the library source.

Attention point 2 diff of runtime version

The fact that the library is used in the environment of the import destination means that the base of the script that works naturally is also set in the environment of the import destination.

Therefore, if you try to import a library created in the rhino environment base into a V8 base environment and use it, an error may occur.

Attention point 3 Is using the library slower?

Although it cannot be said absolutely, using the library may be slower.

This may be because call management is involved, but well JS brings in unnecessary things such as CDN calls at the end, and I think that the library is slow at least in some way.

It depends on the library how much GAP comes out, so it seems to be too slow, and if the library itself is open to the public, I think it should be directly incorporated into it.

SUMMARY

There was some information that I would like to explain about the use of debug mode and various explanations, but since it is getting longer, it is up to this time.

Everyone, please make a library and promote common use of functions.

When you show the library to others, you have to think about how to write it, which is a very good study.

Related articles: Library ~Using external library and creating your own library~

We will introduce how to use external libraries and how to create your own libraries. Easier development by utilizing an external library and how to reuse common functions by creating a self-made library.

  1. Part 1: How to use libraries
  2. Part 2: GAS major external libraries
  3. Part 3: How to make a self-made library
  4. Part 4: Tips and points to note when creating a library

コメント