How to create a self-made library with GAS and reuse scripts ~Part 3: How to make a self-made library~

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

By the way, I have introduced the usage of external libraries and major external libraries so far.

If you would like to confirm, please check the following.

How to create a self-made library with GAS and reuse scripts ~ Part 2: GAS major external 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 are introducing major GAS libraries.

This time, I will talk about how to finally create and publish your own library.

Advertisements

Basic flow for publishing a library

To publish the library, follow the basic flow below.

step1: Create a function you want to commonly use in a .gs file
step2: Link project to cloud platform(GCP) project
step3: Set version in Manage version
step4: Publish as "Introduced as an executable API"

Script side to be used: Add the script ID in the library. Others refer to the first article in the series

Let’s create a simple script and publish it.

Create a callee library1 and a caller library2 in the folder with the following structure. It is a form to use the function of library1 in library2.

folder-
       |--- library1.gs(callee .gs)
       |--- library2.gs(caller .gs)

step1: Create a function you want to commonly use in a .gs file

Well, first, let’s easily create the called side, library1, as follows. A simple function that just returns hello world and a string.

#library1
function helloWorld() {
  return "hello world";
}

step2: Link project to cloud platform(GCP) project

By the way, it would be nice if the created project could be published as a library as it is, but in fact there is a relationship with Google’s update a while ago, so when publishing the library, it is necessary to link it with the GCP project.

To try it out, try “Publish” -> “Install as an executable API”. The following caution message is displayed.

So, first of all, it is necessary to link with the project on the GCP side. Now, let’s go to GCP and check the project number. Go to the GCP console from the link below.

Cloud Computing Services  |  Google Cloud
Meet your business challenges head on with cloud computing services from Google, including data management, hybrid & multi-cloud, and AI & ML.

After transitioning to the console, use the Project Number marked with a red arrow in the image below.

Attention:If you don’t have any projects, you need to launch a project.

Select “Resource” -> “Cloud Platform Project” from the project number noted above, enter it in the window that appears, and link it.

This step is complete when you click Modify Project to complete the association.

It may be necessary to set the OAuth consent screen. If you see a comment that says you need it, please follow the instructions to create it.

step3: Set version in Manage version

Now, and when you publish a library, you also need to understand the concept of version control (so-called version control).

As you saw in Part 1 and Part 2, you could select the version to use by importing each library, right? If you do not remember, please refer to the past article.

Make sure to upgrade the version of your own library as it is updated. As a major premise, I will explain how to make a version.

This is simple, click “File” → “Manage version”, update the script each time, add the version and set the version. By the way, even if it is published, the contents will not change as an externally usable library unless the version is updated.

step4: Publish as “Introduced as an executable API”

After completing the settings up to this point, select “Publish” -> “Install as an executable API” and select the version/accessible user as shown below to publish.

The accessible range can be set by the following 3 categories.

・only me
・Within the same domain (image within the company)
・Everyone (image open to the public)

Execution result

Let’s put the following script in library2 and run it.

function myFunction() {
  Logger.log(library1.helloWorld());
}

The execution result is displayed as “hello world” in logger.log because the return is the string “hello world” in the helloWorld function of library1.

It worked well, but it’s a library that just returns a string that doesn’t matter, but now you can create and use your own library.

SUMMARY

A library that returns a string does not have a basic meaning, but it is very convenient for the library itself to group together functions that can be used in common.

Let’s make it and make coding in the team easier.

Next time, I will introduce the problems that occur when using the active method as points to note when publishing the library.

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

コメント