Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created December 7, 2022 06:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/e46def22cf106b012dfa5ad359b93d24 to your computer and use it in GitHub Desktop.
Save tanaikech/e46def22cf106b012dfa5ad359b93d24 to your computer and use it in GitHub Desktop.
Workaround: Reflecting Latest Script to Deployed Web Apps Created by Google Apps Script without Redeploying

Workaround: Reflecting Latest Script to Deployed Web Apps Created by Google Apps Script without Redeploying

This report is a workaround for reflecting the latest Google Apps Script to the deployed Web Apps without redeploying.

Pattern 1

Of course, when the developer mode of https://script.google.com/macros/s/###/dev is used, the latest script can be used without redeploying.

But, in this case, only the permitted users can use it using the access token. when you want to achieve this using the endpoint of https://script.google.com/macros/s/###/exec without the access token, in order to reflect the latest script to Web Apps, it is required to redeploy. As another pattern, I would like to introduce a workaround for this situation.

Pattern 2

In this pattern, I would like to introduce a workaround that the latest script is reflected to the endpoint of https://script.google.com/macros/s/###/exec.

Usage

1. Create 2 Google Apps Script projects

Please create the following 2 Google Apps Script projects.

  1. For Web Apps. It supposes that the filename is "project1".

  2. For Google Apps Script library. It supposes that the filename is "project2".

2. Prepare sample script

Please copy and paste the following script to the script editor of "project1".

const doGet = (e) => Lib.sample(e);

Please copy and paste the following script to the script editor of "project2".

var sample = (obj) =>
  ContentService.createTextOutput(
    JSON.stringify({ obj, message: "Updated sample message." })
  );

3. Deploy library

Please deploy "project2" as a library. Ref Here, please copy the script ID. This script ID is the same as the library key for installation.

Please install the library to "project1" using the retrieved library key. Ref In this case, please install the library as follows. The following JSON data is the manifest file (appsscript.json).

{
  "timeZone": "### your timeZone ###",
  "dependencies": {
    "libraries": [
      {
        "userSymbol": "Lib",
        "version": "0",
        "libraryId": "### your library key ###",
        "developmentMode": true
      }
    ]
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "webapp": {
    "executeAs": "USER_DEPLOYING",
    "access": "ANYONE_ANONYMOUS"
  }
}

4. Deploy Web Apps

Please deploy Web Apps at "project1" as Execute as: Me and Who has access to the app: Anyone. Ref And, please retrieve the Web Apps URL like https://script.google.com/macros/s/###/exec.

5. Testing

As a test, a curl command is used. The sample curl command is as follows. Please use your Web Apps URL.

$ curl -L "https://script.google.com/macros/s/###/exec"

When this curl command is run, the following value is returned.

{
  "obj": {
    "contextPath": "",
    "queryString": "",
    "parameters": {},
    "parameter": {},
    "contentLength": -1
  },
  "message": "Sample message."
}

Here, please modify "project2" as follows. "Sample message." was modified to "Updated sample message." Please save the script.

var sample = (obj) =>
  ContentService.createTextOutput(
    JSON.stringify({ obj, message: "Updated sample message." })
  );

And, please run the above curl command again. By this, the following value is returned.

{
  "obj": {
    "contentLength": -1,
    "parameters": {},
    "queryString": "",
    "contextPath": "",
    "parameter": {}
  },
  "message": "Updated sample message."
}

You can see the value of "message":"Updated sample message." is changed from "message":"Sample message.". By this, the latest script could be reflected in the Web Apps with the endpoint of https://script.google.com/macros/s/###/exec.

Note

I posted this to the Japanese site on March 30, 2017. Ref But, I noticed that this has never been posted as my blog. So, I posted it here.

@cclambie
Copy link

cclambie commented Dec 7, 2022

Nice one @tanaikech :)
This is a great way around the need to deploy, and is good.

@tanaikech
Copy link
Author

@cclambie Thank you for checking this post. I have posted this on a Japanese site before. But, I didn't notice that I have never posted this on my blog. From your comment, I could notice this. Thank you, too. If this post was useful, I'm glad.

@cclambie
Copy link

Hey @tanaikech ,
I have run into a couple of issues.

When I add the library file appsscript.json to Project 1, it appends .html to the file in Google Script editor
If I try to "run" a test function in Project 2, I get "obj not defined"
Maybe related?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment