Report: Efficiently Creating Web Apps using a Google Apps Script library

Gists

This is a sample script for efficiently creating Web Apps using a Google Apps Script library.

When a Google Apps Script library is used for creating Web Apps, the following advantage can be obtained.

The sample script for explaining this is as follows.

Usage

1. Create 2 Google Apps Script projects.

In this sample, please create 2 standalone Google Apps Script projects. Please set those filenames like SampleLib and Client.

2. Prepare sample script.

For Client

In this case, the sample script of Client is very simple as follows.

Code.gs
const doGet = (e) => SampleLib.forDoGet(e);
const wrapper = (f) => SampleLib[f]();

For SampleLib

The sample script of SampleLib as follows.

Code.gs
function sample1() {
  return "ok1";
}

function sample2() {
  return "ok2";
}

function forDoGet(e) {
  return HtmlService.createHtmlOutputFromFile("index");
}
index.html
<input type="button" value="ok1" onclick="main('sample1')" />
<input type="button" value="ok2" onclick="main('sample2')" />
<div id="res"></div>
<script>
  function main(name) {
    google.script.run
      .withSuccessHandler((e) => {
        document.getElementById("res").innerHTML = e;
      })
      .wrapper(name);
  }
</script>

3. Deploy SampleLib as a library.

Please deploy SampleLib as a library. Ref

In this case, please copy the script ID. This script ID is used for installing the library to Client.

4. Install a library.

Please install a library to Client. Ref

5. Testing.

  1. Please open Client, and deploy Web Apps. Ref

    • Here, please copy the Web Apps URL like https://script.google.com/macros/s/###/exec.

    • And, here, the scopes of methods used in the library are authorized. So, when you change the scopes in the library, please authorize the new scopes at Client side. Please be careful about this.

  2. Please open the Web Apps using your browser.

  3. When the Web Apps is run, the following demonstration can be seen.

Report: Efficiently Creating Web Apps using a Google Apps Script library

References

 Share!