Share


How to Make a React Web App Powered by Spreadsheets

by Astha Sharma on 2020-06-09

Quick Notes About React

If you are the kind of person who learns best by doing, go through this tutorial first THEN go through the React docs.

Otherwise, if you are a theoretical person, read the React docs first.



Start by Creating Your Spreadsheet

Create a spreadsheet that contains the data you want to show on your website.

You can make it on Google Sheets, upload it to Dropbox, or make it on Excel. I created mine on Google Sheets with information on Academy Award Winning Films in 2019.


spreadsheet example
With APISpreadsheets, you don’t need to worry about the format of your sheet because it’ll all get converted the same way.

Our data will power our front-end which will ultimately look like this

final card




Upload or Connect your Spreadsheet on APISpreadsheets.com


Make an account if you don’t have one already (there's a free tier!) and choose the file you want. Once you have uploaded the file, checkout the quick tour to get a deeper understanding of all the features!



api spreadsheets
You can upload a Google Sheet, Local File, or a File on Dropbox.


For this tutorial, we will only be reading our spreadsheet so click on the “Read” tab and Javascript Code Sample


quickstart code
ApiSpreadsheets Quickstart menu

We will explain what to do with all this in a bit, but for now let’s start up our React App.




Open up your Terminal on Mac or Command Prompt on Windows


Before installing React we have to make sure we have node.js downloaded which you can do from here: nodejs.org

Now, to create a project run the following commands on your Terminal or Command Prompt:

  1. $ npx create-react-app award-winning-movies this creates a folder with all the files you need for your react app. You can change award-winning-movies to any other name.
  2. $ cd award-winning-movies this puts you in the folder you made so you can work on your app
  3. $ npm start you should see your app open up on your localhost and it looks like this

react page
How your app should first look



Open the React App you created in your favorite IDE or Text Editor

An IDE stands for Integrated Development Environment where you can edit the React app we just created. I’m using WebStorm. You can also use a simple text editor like Atom.


react src
The left shows the files under src imported from React


On the left you can see all the files we get from React but we will mainly be focusing on our App.js component in the folder “src”.

When we are done with this tutorial, the App.js component will have the following code.



Don’t worry if all this looks confusing. We are going to walk you through it step by step below.

What is a Component?

A component is a block of code that renders your given HTML elements or other components. Components are the building blocks of React and you can read more about them in the React documentation.

For now all you need to know is components display the visuals for our app.



Our App.js Component

Our App.js component is the entry way to our React app.

Import Statements

We are going to import React and the Component class from ‘react’. We will also be importing the App.css file and another Component: FilmCard. We will be walking through these individually later on.

Initialize Our Component

We begin by declaring that our component is a class that extends the component class

Don't forget to close this bracket at the end :)

We initialize the component by setting the component’s state and inheriting props.


this.state = {} initializes the local state of the component. This is where we will be including "filmInfo", an empty array right now. Fetching and error are set to false because nothing is happening for them.

State is the component’s brain. Any changes in the state render the component again so we always think carefully on what the state should contain

Add componentDidMount Method and immediately change fetching state to be true


componentDidMount is a method that is part of React’s component lifecycle. It signals that the component and it’s sub-components have rendered properly and it’s where we will be making our API call.


Go back to APISpreadsheets to copy our Javascript Code from earlier and paste in the componentDidMount() method

By the way, this API URL is public so feel free to use it yourself

We are going to modify this code a bit so it handles all the different scenarios. Depending on the status of the fetch request, we will be changing the state using the this.setState function.

For example, if the fetch is successful (i.e. we have obtained our spreadsheet data), then we will set the filmInfo state to contain all our film data. Our film data will look like the following.


We will also be changing states for when the page is loading or if there is an error. Our final fetch and componentDidMountcode looks like the following:


If successful, our filmInfo state will contain an array of all the films and their attributes. The ones we put into our spreadsheet.

Now we will need another Component that can take each element of that array (aka the info for a particular film) and create a Card to display.


Create New Component FilmCard.js

Create a new file and name it FilmCard.js and put in the same directory as App.js.

img7
FilmCard.js

This is where we will be displaying the info about the film. We will be passing props to this component that are equal to the film’s info.

If you go back to APISpreadsheets and click on Sample Response you can see what we would be naming each prop.

img7
ApiSpreadsheets Sample Response

I gave each prop its own <div> and it looks like this:


We’re almost done! Now it’s time to work on our render() function on App.js

Your code will first run its constructor(props) method, then render(), then componentDidMount() and then render() anytime the state is changed, for example when we call the this.setState function in componentDidMount and fetch.

In the render we have if else statements that determine what should be displayed given a particular scenario. In our case we have 3 scenarios.

  1. if the component is fetching
  2. if there is an error
  3. if the data was successfully obtained.

The first and second if else blocks are for when the screen is loading and the third is if there are errors.

The last else is for getting your film info mapped to your FilmCard component to display the card. This is where we pass the film’s attributes as props to the FilmCard component. Our final render() code will look like this:


Very unformatted and non-abstract code. Feel free to simplify but we have found this is better for learning


We have to have a return() something from the render() method because this is what React shows to our browser. Since we are setting what should be displayed to the body variable, we will be returning, aka telling React to display it, in the return().

Finally, we also need to export each component.

When you run your code, it will display like this on your localhost.

img7
How Your Browser Should Look


Here you have all your data and now you can add some CSS to make it look nice! You can reference my full code on GitHub and use guides on W3Schools to see what you can do!

Congratulations! You have now made a React Web app powered by Spreadsheets!

img123


Time to Deploy Your Code

After you created a nicer looking front-end you can use Heroku to host your site for free.

I followed this tutorial if you want to go more in depth but you want to get started right away, copy and run each step in your terminal.

First, make an account on heroku

$ npm install -g create-react-app
$ create-react-app my-app
$ cd my-app
$ git init
$ heroku create -b https://github.com/mars/create-react-app-buildpack.git
$ git add .
$ git commit -m "react-create-app on Heroku"
$ git push heroku master
$ heroku open

This gave me a randomly worded name but now my site can be shared and seen to anyone! Note, that free sites sometimes take a while to boot up on Heroku.

https://sheltered-eyrie-23738.herokuapp.com/


And there you have it, a simple React web app powered by Spreadsheets.

The best part is, if you add or modify any data to your Google Sheets, it will automatically show up on your web app! Even if you are working on an Excel file you can upload that in the same section you originally did on APISpreadsheets and your app will update! No additional code would be required.

If you have any further questions, feel free to email us at info@lovespreadsheets.com! We can’t wait to see what you build.

Checkout our other tutorials!

Let us know what you think