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.
- Node.js installed on your machine
- A Google account
- Enable the Google Apps Script API at https://script.google.com/home/usersettings
npm run build- Build the Svelte applicationnpm run preview- Preview the production build locallynpm run check- Run TypeScript and Svelte checks
npm installto install dependencies.npm run devto start the development server.- Open
http://localhost:5173in your browser to see the app. - Edit the Svelte components in the
srcfolder. The app will reload automatically
To 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:loginThis will open a browser window for authentication.
Option A: Create a new project
npm run clasp:createThis 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:pushThis 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
- gas/Code.js
- src/App.svelte
- src/lib/mocks.ts
- Add your Svelte components in the
src/libfolder. - Import and use them in
src/App.svelte. - If you need to call Google Apps Script functions from Svelte, use the google.script.run API. See
testInvokationFromClientexample inServerDrivenComponent.svelte,mocks.tsandCode.jsfor reference.