Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Problems with Google Apps Script API JavaScript "How to... Execute function"
As this is the first question about Google Apps Script, here is a very brief description: it is a platform that helps people easily get programmatic access to Google apps data like Gmail messages, Google Drive files, etc. and also to certain elements that can't be accessed through one of the Google APIs. It offers a web-based IDE and has its own API.
The Google Apps Script API docs include a quickstart section, having subsections for different languages / platforms including JavaScript.
I thoroughly reviewed the quickstart overview and the JavaScript subsection and got it to work, but when trying the "How too... Execute a function" I got stuck; more specifically, I'm getting 404 errors.
References:
- Google Apps Script REST API overview page: https://developers.google.com/apps-script/api/concepts.
- JavaScript quickstart instructions: https://developers.google.com/apps-script/api/quickstart/js
- The How To... Execute a function: https://developers.google.com/apps-script/api/quickstart/target-script
This is my environment:
- Google Workspace account. I'm the domain administrator.
- New Google Cloud project, created following the quickstart instructions
- New Google Apps Script project, created using the quickstart instructions
Hardware and Installed Software
- Mac Mini Apple M1, MacOS Ventura 13.4.1.
- Google Chrome
- Visual Studio Code
Steps to reproduce the problem
- Read the links included above.
- Create the target script.
- Create the file from the JavaScript quickstart. Default name
index.html
. - Create the file from the How to... Execute function. Default name index.js
- Modify
index.html
to loadindex.js
. - Modify
index.html
in such a way that instead of callingcreateScript()
callcallScriptFunction()
. - Create the Google Cloud standard project.
- Enable the Apps Script API.
- In the API section of the Cloud project, go to credentials.
- Add the ApiKey. Take note of it.
- Add the Web client
- Add the allowed URI. If you will run this locally add
http://localhost:8080
. - Create the OAuth consent screen
- Grab the Google Cloud project id and add it to the Google Apps Script project on Project settings.
- Deploy Google Apps Script as API executable
- Get the script Id.
- Add the ApiKey, Client Id to the
index.html
file. - Add the script Id to the
index.js
file. - Open
index.html
using Google Chrome. - Click the button Authorize and follow the OAuth authorization flow.
- Open the Google Chrome DevTools Console.
Here you will find the error message.
1 answer
TL;DR: Use the versioned deployment id instead of the script id.
After repeating the procedures, step by step multiple times, I tried adding the resource.devMode
property to the Apps Script REST API call and setting it to true
. This call is in index.js
file and refers to gapi.client.script.scripts.run
. The modified call looks as shown below
gapi.client.script.scripts.run({
'scriptId': scriptId,
'resource': {
'function': 'getFoldersUnderRoot',
'devMode': true
}
Surprisingly, with this parameter, the code work. For those new to Apps Script, the development mode only works for the project owner for projects stored in My Drive units and for users having edit access for projects stored in Shared Drives, so this "solution" can't be used on "production".
After sleeping one night, I tried replacing the script id with the deployment id, without the devMode
parameter... Voila! It worked!.
So the documentation on the following page looks to be incomplete.
https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run#path-parameters
Current content:
Path parameters
Parameters
scriptId | string | The script ID of the script to be executed. Find the script ID on the Project settings page under "IDs." |
The description should say that the script ID, the head deployment id, works when using resource.devMode
, and it's set to true
. When omitted or set to false
, use the versioned deployment id instead of the script id.
The versioned deployment id is shown when creating a new deployment. Can be found by opening Google Apps Script, opening the project, and clicking on Deploy > Manage Deployments. The dialog shows the versioned deployment id and a button to copy it to the clipboard.
Notes:
I found this question "Requested entity was not found." - Apps Script Execution API error. I got the idea of using resource.devMode
property from Tanaike's answer.
1 comment thread