Running Functions by Specifying Function Names with Web Apps for Google Apps Script

Gists

In this report, I would like to introduce the method for running functions by directly specifying the function names with Web Apps for Google Apps Script.

Description

It has already been known that the directly specified functions in the project can be run from the outside by enabling “API executable” and using the method of scripts.run in Google Apps Script API. In this case, the installation for using Apps Script API is a bit complicate. I think that this is making users difficult to use Apps Script API, although the important settings are including in the installation when the importance of the security is considered. As one of workarounds for making easy to run the directly specified functions in the project, I would like to introduce the method using Web Apps. When Web Apps is accessed, the functions of doGet() and doPost() are used. These are the reserved function name. But, when this method is used, the functions in the project can be run by directly selecting from the outside.

This method is also used at RunAll which is a library for running the concurrent processing using only native Google Apps Script.

Usage

1. Create new project

Please create new project. In this case, you can use both the standalone script type and the container-bound script type. Here, as a sample case, the standalone script type is used. In order to create new standalone project, please access to https://script.google.com, and click “New script”. By this, the script editor is opened as new project. Then please give the project name. By this, new standalone project is created.

2. Copy and paste script for Web Apps

Please copy and paste the script to the created project on the script editor for using with Web Apps. The script is as follows.

Script

// This function is used for running the script of Web Apps.
function doGet(e) {
  if ("function" in e.parameter) {
    var f = e.parameter["function"];
    var a = e.parameter["arguments"];
    if (f in this && typeof this[f] == "function") {
      return ContentService.createTextOutput(this[f](a));
    }
    throw new Error(f + " was not found.");
  }
  throw new Error("Function name was not given.");
}

// Sample function that you want to run using Web Apps.
function sample(s) {
  return "Received arguments are " + s;
}
  • doGet(e) is used for Web Apps.
  • sample(s) is a test function that you want to run using Web Apps. - You can add the other functions you want to run in the project.

Deploy Web Apps

Please deploy Web Apps as follow.

  1. On the script editor, Open a dialog box by “Publish” -> “Deploy as web app”.
  2. Select “User accessing the web app” or “Me” for “Execute the app as:”.
  3. Select “Anyone, even anonymous” for “Who has access to the app:”.
    • This setting is used for testing this method. In this settings, all people who know the URL of Web Apps can be accessed. So please be careful.
    • When you use at the actual situation, please select “Only myself”. By this, only you can access to Web Apps.
  4. Click “Deploy” button as new “Project version”.
  5. Automatically open a dialog box of “Authorization required”.
    1. Click “Review Permissions”.
    2. Select own account.
    3. Click “Advanced” at “This app isn’t verified”.
    4. Click “Go to ### project name ###(unsafe)”
    5. Click “Allow” button.
  6. Copy “Current web app URL:”.
    • It’s like https://script.google.com/macros/s/#####/exec.
  7. Click “OK”.

IMPORTANT: In above settings, when you modified the script, please redeploy Web Apps as new version. By this, the latest script is reflected. Please be careful this.

Run function

After above settings were done, you can run the function of sample(s) as follows.

Curl sample

$ curl -L "https://script.google.com/macros/s/#####/exec?function=sample&arguments=foo"

Result

Received arguments are foo

From this result, you can see the function of sample(s) was run by accessing to Web Apps.

References

 Share!