Skip to content

Dev Google Spreadsheets

Valentin Noves edited this page Jul 22, 2020 · 15 revisions

Step 1: Create your Firebase project

Go to Create Database

Step 2: Create your Realtime database

Go to Create Database

Step 3: Create your spreadsheet and populate it

Conections logo

The first row contains your keys. The first key should be set to “id” and each row should be labeled with the corresponding number, starting with “1”. An easy way to set the id for each row in column A is to enter this formula “=COUNTA($B$2:B2)” into cell A2 and then apply that to all rows. You can add as many rows or columns as you need.

Step 4: Create your Apps Script project

In the menu, go to Tools -> Script editor

script logo

It will take you to a code editor with the following file open: Code.gs. Replace the contents with this snippet. Search for this code at the top of the file:

script logo

Replace the “spreadsheetID” placeholder with your own. The ID is the bolded part in the full spreadsheet URL (e.g. https://docs.google.com/spreadsheets/d/spreadsheetID/edit#gid=0) Replace the “firebaseUrl” placeholder with your database URL from Step 2. Make sure to include the trailing slash (e.g. https://sheets-sample-test.firebaseio.com/) otherwise it will throw an error. In your menu, go to View -> Show manifest file, which will add a file called appsscript.json.

script logo

This will add an appsscript.json file to your project. Replace the contents with the following snippet.

Step 5: Start the sync

In the menu, go to Run -> Run function -> initialize. You’ll see a prompt to review and accept the permissions. This allows the App Script project to access the spreadsheet and upload data to Firebase. Click “Review Permissions” and then click “Allow.”

script logo

script logo

Congrats! Your Firebase Realtime database has now been populated with the data from your spreadsheet! Any further edits will sync seamlessly and you can even share your spreadsheet with other people.

script logo

Tip & Tricks

Add Security

If you are feeling fancy, you can add a little more security to the Firebase database. Go to the Firebase Console -> Database and change your rules to the following.

script logo

You’ll have to implement Firebase Authentication on your prototype, so that your users can read the data. Setting the write method to false means only your spreadsheet can write to the database.

Generating an Array

In the sheet if your id starts with 0 and increments by 1, then the script will generate an array instead of a key value pairs.

script logo

Nested data

When converting a table to JSON, it’s only possible to do one level of nesting. But what if you need nested data? The script provided above has a special function that lets you create a nested object. Simple name the title of the column with the path of the object join key using a double underscore __. If you want to nest street under address you can simply say ‘address__street’, this means you cannot have a column named just address in your sheet.

script logo

This will generate an object like the one below, and you can also nest at any level.

script logo

More details Here

Clone this wiki locally