Skip to main content

Overview

SGD-MCS backend runs on Google Apps Script (GAS), a serverless JavaScript platform that provides direct integration with Google Workspace services including Sheets and Drive.
The backend uses Apps Script V8 runtime for modern JavaScript support including ES6+ features.

Prerequisites

Google Account

A Google Workspace or Gmail account with access to Google Drive and Sheets

Node.js & clasp

Optional: Install clasp CLI for command-line deployment
npm install -g @google/clasp

Source Code

Clone the repository and navigate to the Backend directory

Google Sheets Database

A properly configured Google Sheets database (see Database Structure)

Apps Script Manifest

The appsscript.json file configures the Apps Script project:
{
    "timeZone": "America/Bogota",
    "dependencies": {},
    "exceptionLogging": "STACKDRIVER",
    "runtimeVersion": "V8",
    "oauthScopes": [
        "https://www.googleapis.com/auth/spreadsheets",
        "https://www.googleapis.com/auth/drive",
        "https://www.googleapis.com/auth/script.external_request"
    ],
    "webapp": {
        "executeAs": "USER_DEPLOYING",
        "access": "ANYONE_ANONYMOUS"
    }
}
Location: Backend/appsscript.json

Configuration Options

timeZone
string
default:"America/Bogota"
Sets the default timezone for date/time operations. Uses IANA timezone identifiers.
runtimeVersion
string
default:"V8"
JavaScript runtime version. V8 enables modern ES6+ syntax, arrow functions, and more.
exceptionLogging
string
default:"STACKDRIVER"
Error logging destination. STACKDRIVER logs to Google Cloud Logging for better debugging.
oauthScopes
array
required
OAuth 2.0 scopes required by the application:
  • spreadsheets - Read/write Google Sheets data
  • drive - Create and manage Drive folders/files
  • script.external_request - Make external HTTP requests (if needed)
webapp.executeAs
string
default:"USER_DEPLOYING"
Execution context. USER_DEPLOYING runs code with deployer’s permissions.
webapp.access
string
default:"ANYONE_ANONYMOUS"
Access control. ANYONE_ANONYMOUS allows public access without authentication.
For production, consider changing to ANYONE (requires login) or DOMAIN (organization only) for better security.

Deployment Methods

1

Install clasp

npm install -g @google/clasp
2

Login to Google

clasp login
This opens a browser window for Google authentication.
3

Create New Project

Navigate to the Backend directory and create a new Apps Script project:
cd source/Backend
clasp create --title "SGD-MCS Backend" --type webapp
This generates a .clasp.json file with your script ID.
4

Configure Script ID

Edit .clasp.json and verify the script ID:
{
  "scriptId": "YOUR_SCRIPT_ID_HERE",
  "rootDir": "."
}
5

Push Code

Deploy all files to Google Apps Script:
clasp push
Use clasp push --watch to enable auto-deployment on file changes.
6

Deploy Web App

Create a deployment:
clasp deploy --description "Initial deployment"

Method 2: Manual Upload

1

Create New Project

  1. Go to script.google.com
  2. Click “New Project”
  3. Rename to “SGD-MCS Backend”
2

Configure Manifest

  1. Click the gear icon (Project Settings)
  2. Check “Show appsscript.json manifest file”
  3. Edit appsscript.json with the configuration above
3

Create Files

Create each file from the Backend/ directory structure:
  • config.js
  • core/Main.js
  • core/EntityManager.js
  • core/controller.js
  • services/DataService.js
  • services/DriveManager.js
  • services/DriveFileManager.js
  • services/SearchService.js
  • utils/Utils.js
  • utils/DataUtils.js
4

Copy Code

Copy the code from each source file into the corresponding Apps Script file.
5

Save and Deploy

  1. Save all files (Ctrl+S)
  2. Click “Deploy” → “New deployment”
  3. Select type: “Web app”
  4. Set access as needed
  5. Click “Deploy”
Manual deployment is tedious for updates. Use clasp for production workflows.

File Structure

The backend follows a modular architecture:
Backend/
├── appsscript.json          # Project manifest
├── core/
│   ├── config.js            # Global configuration
│   ├── Main.js              # Entry points (doGet, API proxies)
│   ├── EntityManager.js     # CRUD operations
│   └── controller.js        # Dashboard statistics
├── services/
│   ├── DataService.js       # Data retrieval services
│   ├── DriveManager.js      # Drive folder management
│   ├── DriveFileManager.js  # File operations
│   └── SearchService.js     # Universal search
└── utils/
    ├── Utils.js             # ID generation, normalization, audit logging
    └── DataUtils.js         # Sheet data conversion utilities

Key Functions

Entry Point: doGet()

Serves the web application:
function doGet() {
    return HtmlService.createHtmlOutputFromFile('web/index')
        .setTitle('SGD-MCS v3.0')
        .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
        .addMetaTag('viewport', 'width=device-width, initial-scale=1');
}
Location: Backend/core/Main.js:6-11

API Proxies

Apps Script requires global function declarations for client-side calls via google.script.run. The Main.js file provides these proxies:
// Dashboard
function getStats() { return getDashboardStats(); }

// Entity Lists
function getStudents() { return listStudents(); }
function getTeachers() { return listTeachers(); }
// ... etc
Location: Backend/core/Main.js:22-42

OAuth Scopes Explained

Scope: https://www.googleapis.com/auth/spreadsheetsRequired for:
  • Reading student, faculty, thesis, and event data
  • Writing new records and updates
  • Auto-incrementing ID counters in Config sheet
  • Audit logging to Historial_Documentos
Scope: https://www.googleapis.com/auth/driveRequired for:
  • Creating entity folders (Students, Thesis, etc.)
  • Uploading documents and certificates
  • Organizing files into structured directories
  • Moving files to trash on deletion
Scope: https://www.googleapis.com/auth/script.external_requestRequired for:
  • Making external API calls (if needed)
  • Webhook integrations
  • Future extensibility

Testing Deployment

1

Test Configuration

In the Apps Script editor, run getAppConfig():
Logger.log(getAppConfig());
Verify it returns:
{
  "appVersion": "3.0.0",
  "environment": "Production",
  "sheets": {...},
  "colors": {...}
}
2

Test Database Connection

Run getDB() and verify it connects:
var ss = getDB();
Logger.log(ss.getName());
3

Test CRUD Operations

Test creating a student record:
var result = createItem('estudiante', {
  Nombre1: 'Test',
  Apellido1: 'User',
  Email: '[email protected]'
});
Logger.log(result);
4

Test Web App

Deploy as web app and visit the deployment URL to ensure the frontend loads.

Updating the Script

Using clasp

# Pull latest code from Apps Script
clasp pull

# Make changes locally
# ...

# Push updates
clasp push

# Create new version
clasp deploy --description "Updated entity manager"

Manual Update

  1. Open the script at script.google.com
  2. Edit the relevant files
  3. Save changes (Ctrl+S)
  4. Create new deployment or update existing one
Always test changes in a development environment before deploying to production.

Permissions and Security

Service Account

The script runs with the permissions of the deploying user. Ensure this account has:
  • Owner or Editor access to the Google Sheets database
  • Access to the Drive folder specified in ROOT_FOLDER_ID
  • Appropriate organizational permissions

Public Access Warning

The default configuration allows anonymous public access. For production:
  1. Change webapp.access to "DOMAIN" (organization only)
  2. Or change to "ANYONE" (requires Google login)
  3. Implement additional authentication in doGet() if needed

Monitoring and Logs

View Execution Logs

  1. Open your Apps Script project
  2. Click “Executions” in the left sidebar
  3. View recent runs, errors, and execution times

Cloud Logging

With exceptionLogging: "STACKDRIVER", errors are logged to Google Cloud:
  1. Go to console.cloud.google.com
  2. Select your Apps Script project
  3. Navigate to “Logging” → “Logs Explorer”
  4. Filter by severity and timestamp

Troubleshooting

Error: “Authorization required to perform that action”Solution: Run any function manually in the editor to trigger OAuth consent flow.
Error: “Unable to deploy”Solution: Check that all file syntax is valid. Run “Debug” → “Check for errors” in the editor.
Error: “Access not granted”Solution: Verify all required scopes are in appsscript.json. Re-authorize the script.
Error: “Function not found” when calling from frontendSolution: Ensure functions are declared in global scope (Main.js proxies).

Next Steps

Configuration

Configure database connection and settings

Database Structure

Understand the Google Sheets schema

Permissions

Set up user roles and access control

Build docs developers (and LLMs) love