GithubHelp home page GithubHelp logo

isabella232 / typescript-react-google-apps-script Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 52inc/typescript-react-google-apps-script

0.0 0.0 0.0 831 KB

A React / Google Apps Script app. Clone to set up your development environment with tools including Webpack, clasp, Tern autocompletion. Modify the demo app source code, build and push :)

JavaScript 31.97% HTML 2.12% CSS 13.80% TypeScript 52.10%

typescript-react-google-apps-script's Introduction

TypeScript + React + Google Apps Script

Use this demo project as your boilerplate React app for HTML dialogs in Google Sheets, Docs and Forms.

This project is a fork of React-Google-Apps-Script (which uses labnol's excellent apps-script-starter as a starting point), adding support for TypeScript and React. It demonstrates how easy it is to build React apps that interact with Google Apps server-side scripts. Simply clone this project and modify the source code to get started developing with React for Google Apps Script client-side dialogs.

Google Apps Script / React development The demo app for Google Sheets shows insertion/deletion/activation of sheets through React-built HTML dialog.

Installation

Clone the sample project and install dependencies:

git clone https://github.com/enuchi/React-Google-Apps-Script.git
cd React-Google-Apps-Script
yarn

Then create a new Google Sheets spreadsheet. Open the Script Editor and copy the script's scriptId. [Tools > Script Editor, then File > Project properties].

Paste the scriptId into the .clasp.json file as below:

// .clasp.json
{"rootDir": "dist",
 "scriptId":"...paste scriptId here..."}

If you have not enabled Google's Apps Script API, do so by visiting https://script.google.com/home/usersettings. Log into CLASP to push code to the server from the command line:

yarn login:clasp

Modify the server-side and client-side source code in the src folder using ES6/7 and React. Change the scopes in appsscript.json if needed. When you're ready, build the app and deploy!

yarn deploy:stage

or

yarn deploy:prod

Webpack will display any linting errors, bundle your files in dist, and push your files to Google's servers using CLASP. You can run yarn build to just build.

Using deploy:stage will give an easier to debug minified version, where deploy:prod will minify it to basic obscurity.

The sample app

Insert/activate/delete sheets through a simple HTML dialog, built with React. Access the dialog through the new menu item that appears. You may need to refresh the spreadsheet and approve the app's permissions the first time you use it.

How it works

"Google Apps Script is based on JavaScript 1.6 with some portions of 1.7 and 1.8 and provides subset of ECMAScript 5 API."

That means many JavaScript tools used today in modern web development will not work in the Google Apps Script environment, including let/const declarations, arrow functions, spread operator, etc.

This project circumvents those restrictions by transpiling newer code to older code that Google Apps Script understands using Babel, and also bundles separate files and modules using Webpack.

On the client-side, there are restrictions on the way HTML dialogs are used in Google Apps (Sheets, Docs and Forms). In web development you can simply reference a separate css file:

<link rel="stylesheet" href="styles.css">

In the Google Apps Script environment you need to use HTML templates, which can be cumbersome. With this project, all files are bundled together by inlining .css and .js files. Using a transpiler and bundling tool also allows us to use JSX syntax, and external libraries such as React.

Features

  • Support for JSX syntax:
return <div>Name: {person.firstName}</div>
  • Support for external packages. Simply install with yarn or from a file and import:
$ yarn add react-addons-css-transition-group
// index.jsx
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
  • import CSS from another file:
import "./styles.css";
  • Make server calls in React with google.script.run:
componentDidMount() {
  google.script.run
     .withSuccessHandler((data) => this.setState({names: data}))
     .withFailureHandler((error) => alert(error))
     .getSheetsData()
}
  • Use newer ES6/7 code, including arrow functions, spread operators, const/let, and more:
const getSheetsData = () => {
  let activeSheetName = getActiveSheetName();
  return getSheets().map((sheet, index) => {
    let sheetName = sheet.getName();
    return {
      text: sheetName,
      sheetIndex: index,
      isActive: sheetName === activeSheetName,
    };
  });
};

Tern support

This project includes support for GAS definitions and autocomplete through a Tern plugin. Tern is a code-analysis engine for JavaScript, providing many useful tools for developing. See Tern's site for setup instructions for many popular code editors, such as Sublime, Vim and others.

Tern provides many indispensable tools for working with Google Apps Script, such as autocompletion on variables and properties, function argument hints and querying the type of an expression.

  • Autocomplete example. Lists all available methods from the appropriate Google Apps Script API: tern support

  • Full definitions with links to official documentation, plus information on argument and return type: tern support

Extending this app

  • You can split up server-side code into multiple files and folders using import and export statements.
  • Make sure to expose all public functions including any functions called from the client with google.script.run as well as onOpen. Example below shows assignment to global object:
const onOpen = () => {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
        .createMenu('Dialog')
        .addItem('Add sheets', 'openDialog')
        .addToUi();
}

global.onOpen = onOpen
  • You may wish to remove automatic linting when running Webpack. You can do so by editing the Webpack config file and commenting out the eslintConfig line in client or server settings:
// webpack.config.js

const clientConfig = Object.assign({}, sharedConfigSettings, {
  ...
  module: {
    rules: [
      // eslintConfig,
      {

Suggestions

Open a pull request!

typescript-react-google-apps-script's People

Contributors

enuchi avatar johnnyslots avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.