This demonstrates how to use Svelte with Google Apps Script using Clasp for seamless deployment. This demonstrates how to use Svelte with Google Apps Script.
npm run build - Build the Svelte applicationnpm run preview - Preview the production build locallynpm run check - Run TypeScript and Svelte checksnpm install to install dependencies.npm run dev to start the development server.http://localhost:5173 in your browser to see the app.src folder. The app will reload automaticallyTo test Google Apps Script interactions locally, the project includes a mock setup that simulates the google.script.run API.
To enable the mock setup, ensure that the mockSetup.ts file is imported and invoked in your main entry file (src/main.ts):
Then add your mock server functions in src/lib/mocks.ts.
Enable the Google Apps Script API: https://script.google.com/home/usersettings
Login to your Google account:
npm run clasp:login
This will open a browser window for authentication.
Option A: Create a new project
npm run clasp:create
This creates a new Google Apps Script project and generates a .clasp.json file with your project credentials.
Option B: Connect to an existing project
If you already have a Google Apps Script project, create a .clasp.json file in the root directory:
{
"scriptId": "YOUR_SCRIPT_ID_HERE",
"rootDir": "gas"
}
Replace YOUR_SCRIPT_ID_HERE with your script ID (found in Project Settings in the Apps Script editor).
Build your Svelte app and push to Google Apps Script:
npm run clasp:push
This command builds the project and pushes all files to your Apps Script project.
Open your Apps Script project:
npm run clasp:open
In the Apps Script editor, click Deploy > New deployment
Select type Web app
Configure access settings as needed
Click Deploy
npm run clasp:login - Authenticate with Googlenpm run clasp:logout - Logout from Googlenpm run clasp:create - Create a new Apps Script projectnpm run clasp:push - Push code to Apps Scriptnpm run clasp:open - Open the Apps Script project in browsernpm run clasp:deploy - Create a new deploymentnpm run clasp:deployments - List all deploymentsnpm run deploy - Build and push to Apps Script (recommended).
├── gas/ # Google Apps Script files
│ ├── appsscript.json # Apps Script manifest
│ ├── Code.js # Server-side Apps Script code
│ ├── Index.html # Main HTML template
│ ├── Javascript.html # Generated from build (auto-generated, do not edit directly)
│ └── Stylesheet.html # Generated from build (auto-generated, do not edit directly)
├── src/ # Svelte source files
│ ├── lib/ # Svelte components and utilities
│ │ ├── Counter.svelte # Example Svelte component
│ │ ├── ServerDrivenComponent.svelte # Example Svelte component interacting with GAS
│ │ ├── mockSetup.ts # Proxy Mock setup for local testing
│ │ └── mocks.ts # Add your mock server functions here
│ ├── App.svelte # Main Svelte component
│ ├── app.css # Global styles
│ └── main.ts # Entry point
├── .claspignore # Files to ignore when pushing
└── .clasp.json # Clasp configuration (gitignored)
The main files you will want to start with are
src/lib folder.src/App.svelte.testInvokationFromClient example in ServerDrivenComponent.svelte, mocks.ts and Code.js for reference.