This is a template to create google appscript project locally with react js.
It uses
- "Parcel" to compile down whole source code into a single .html file.
- "Clasp" for appscript support
- "gas-client" to run server funtions in a js promise syntax in the project.
- "Tailwind css" built in support
-
Clone this repsitory, open folder in terminal and install dependencies as usual.
git clone "https://github.com/SandipLow/React-Appscript.git" cd React-Appscript npm install
-
Enable the Google Apps Script API: https://script.google.com/home/usersettings
-
Login to Clasp
npm run glogin
-
Setup an Appscript project
npm run setup
-
Start development by starting react
npm start
glogin
: Login to claspglogout
: Logout to claspgcreate
: create a new appscript project.gpull
: pull drive appscript code to './appscript' directorygpush
: push './appscript' directory to appscript filegstart
: push project to appscript whenever a file changes everytime (Though it is recommended to push optimised html file during production, Use it to test appscript server functions)setup
: all in one command that create an appscript project in Google Drive.start
: Start react code server locally.build
: build a single optimized html file.deploy
: build a single optimized html file then push to appscript.
Now this project has two directory :
-
'/appscript'
=> This is the directory which is going to be pushed to appscript file. As you can see It containsmain.js
,index.html
andappscript.json
. The main.js
is going to be compiled to main.gs
. Thus it is the Script file here which contains server functions.index.html
is the all in one compile .html file.appscript.json
contains all deployment, timezone, exceptionLogging settings which neither we've to set or modify. -
'/src'
=> It is the folder where all our react source code will be there.
Now Lets make some changes to react project. Then build this into a single html file the push
npm run gpush
But, The size of html file willbe much large (apx - 1.5MB). Thus it is recommended to compile it to a minified version before.
npm run build
There is command to run both of this during deployment
npm run deploy
Now to push the changes autometically whenever a file modify
npm run gstart
!!! IMPORTANT : Whenever you start development after making any changes to Appscript project in the online editor, pull the changes from the appscript file...
npm run gpull
This project uses the gas-client package to more easily call server-side functions using promises.
// Google's documentation syntax.
google.script.run
.withSuccessHandler(res=> setHello(res))
.withFailureHandler(e=> alert(e))
.hello();
// The Syntax used here.
const [hello, setHello] = useState("Loading...")
useEffect(()=>{
server.hello()
.then(res=> setHello(res))
.catch(e=> alert(e))
}, [])
!!! This works in appscript deployment only... Thus it is recommended to use npm run gstart
to test the server function call functionality.