This template provides a foundational structure for a Google Apps Script web application using a modular approach (Crockford-style modules). It includes a simple "Hello App" demonstrating client-server interaction.
- Modular Design: Code is organized into reusable modules located in
src/modules/.ErrorManager.js: For custom error handling.TimeManager.js: To manage script execution time (useful for longer operations).GreeterManager.js: Example business logic module.LogManager.js: A basic logging wrapper.
- Web Application:
index.html: Simple HTML frontend.webapp.js: Server-side logic (doGetandhandleHelloRequest).
- Clasp Ready: Configured for local development with the Google Clasp CLI.
- Firebase Studio / IDX: Designed to be easily used within Firebase Studio. The
.idx/dev.nixfile sets up a development environment withclaspand other common tools.
- Node.js and npm (for installing
claspglobally if not using IDX's environment). - Google Clasp CLI: Install globally with
npm install -g @google/clasp. (Included in the.idx/dev.nixenvironment). - A Google Account.
-
Clone this Template (if not already in a workspace): If you're using this template via the Firebase Studio template collection, this step is already done. Otherwise:
# git clone ... or copy this template directory # cd gas-basic-module-template # Or your template directory name
-
Authenticate Clasp: Open a terminal in this directory and run:
clasp login
Follow the prompts to log in with your Google account.
-
Create or Identify an Apps Script Project:
- Go to script.google.com and create a new project, or use an existing one.
- Note its Script ID (found in Project Settings or the URL).
-
Configure
.clasp.json:- Rename
.clasp.json.exampleto.clasp.json. - Edit
.clasp.jsonand replace"YOUR_SCRIPT_ID_HERE"with your actual Script ID. - The
"rootDir": "."setting tellsclaspthat this directory is the root of your Apps Script project files to be pushed. - Optionally, set your GCP
projectIdif you are using Stackdriver logging with a specific GCP project.
- Rename
-
Push Initial Code:
clasp push -f
The
-fflag forcesclaspto overwrite the manifest file (appsscript.json) in your online project with the local one.
- Edit files locally (e.g.,
webapp.js,index.html, modules insrc/modules/). - Use
clasp pushto upload your changes to the linked Apps Script project. - Use
clasp opento open the project in the Google Apps Script online editor. - Use
Logger.log("message")in your.jsfiles for server-side logging. View logs in the Apps Script editor (View > Executions).
-
Ensure
appsscript.jsonis configured: Thewebappsection inappsscript.jsonshould be set up correctly (see the provided file). -
Create a Deployment:
- Via Clasp (Recommended):
This command will create a new versioned deployment and output the Deployment ID and the Web App URL (ending in
clasp deploy -d "Initial deployment of Hello App"/exec). - Via Apps Script Editor:
- Run
clasp open. - In the editor, click "Deploy" > "New deployment".
- Select Type: "Web app".
- Enter a Description (e.g., "Hello App v1").
- Configure Execute as and Who has access (these should ideally match your
appsscript.jsonsettings). - Click "Deploy".
- Copy the Web app URL (the one ending in
/exec).
- Run
- Via Clasp (Recommended):
-
Access the Web App: Open the
/execURL obtained from the deployment step in your browser.
src/modules/ErrorManager.js: Provides athrowErrorfunction to create and throw standardized, enriched error objects.src/modules/TimeManager.js: Helps manage script execution time, useful for operations that might approach Apps Script quotas. IncludeshasEnoughTime().src/modules/GreeterManager.js: An example module demonstrating how to use other managers (Time, Error, Log) to perform a task (greeting names).src/modules/LogManager.js: A simple wrapper aroundLoggerorconsolefor consistent logging.
This template provides a starting point. Feel free to adapt and expand upon it!