Integrating Google Sheets with Firebase Realtime Database


Integrating Google Sheets with Firebase Realtime Database

In this tutorial, we will learn how to integrate Google sheets with Firebase Realtime Database. We will go through bidirectional integration: -

  1. When you update Google Sheet, Firebase Realtime DB will be updated automatically.
  2. When you update Firebase Realtime DB, Goggle Sheets will be updated automatically (We will need firebase functions to implement this integration).

Now here is the question… why do we need Google sheets to update Firebase realtime database or vice versa?

In many of the mobile/web-apps with a Firebase back-end, situation arises where your application has no option to update the database, like adding new data. The only way you can update the database is either by having an admin panel, OR updating database directly. Both of these are time consuming and costly endeavors. This is where Google sheet comes into the picture and acts as a tool to update the database. Below is one example that will make it clear -

  1. Adding Data to Database through Google Sheets :-
    There is a quiz application and you are using firebase realtime database to update the quiz data. If you want to add some more questions to the database, you have to manually enter those into the database. As an alternative, Google sheets can help add more questions to the Database.
  2. Updating Google Sheets from Realtime Database :-
    In the same quiz game you need to get feedbacks and results of players. Checking database manually is a difficult task. Again, you can make an admin panel to check data but that takes time and cost. So to make it easy we can update sheets based on database update (firebase functions will be used to get updates in realtime database).

Post Structure

To implement both methods, we will follow below simple steps :- 

  1. Setup Firebase project and setup realtime database. Also, populating data into realtime database
  2. Creating a Google sheet and writing sheet scripts in to the Sheets platform.
  3. Creating a Google service account, enabling Google sheets and getting service account JSON file.
  4. Enabling Firebase functions and Integrating Google API (for sheets) into Firebase functions.
  5. Providing permissions to the Google sheets and testing it.

Ok let’s start writing some real damm code.



No more blaa blaa…!

Step 1 — Setup Firebase realtime database

(If you know how to setup firebase, you can skip this section)

Setting up a new Firebase project is covered in step 2 of this Blog

After Setting up the Firebase project you can enable realtime database by going into realtime database tab :- 



Firebase Realtime Database

Once you enable the Realtime Database, you can see the below screen with empty database.



Empty Realtime Database

Now to test, we have to import some data to the Database. There is an import JSON option in the tab, which can help import JSON data into it. 



Importing JSON in Firebase realtime database

If you don’t already have the data in your database, you can import it using above option.

Step 2 — Create Google sheet and write Sheet scripts 

We will start with a fresh Sheet and later will add some data to it. And check whether it got reflected in Realtime database or not. To create the Google sheet 1) Login with your Google account 2) Go to Google Sheets and create a new sheet. You probably already know that 😁



Creating new Google sheet.

After creating new sheet, we have to link the Realtime Database with the Google Sheet. Google Sheet itself provides an option to add some code to the Sheet. To add the Code, go to Tools → Script editor. Check the below screenshot for that :-



Choosing Script editor in Google Sheets

In Script editor, you will see empty script. Before adding any code to it, we have to enable appsscript.json file from the settings, shown in below settings page.



Project Setting for Google Sheet Script

After enabling this option, you will find appsscript.json file in you code editor. This file is same as package.json file which maintains dependencies in your javascript project. Now you have to replace your appsscript.json code with the below code.


Now we will add some code to the Code.gs file, which will handle all the logic for getting the sheet data and adding it into the Firebase realtime database. We will divide the implementation under various functions. Below is the code for Code.gs file.


In the above code 



Firebase Realtime database URL
  • Replace your_collection_name with the collection name you want to save that data with in Google Sheets. Once you are done with above replacements, let’s run the code by selecting a specific method.
Note :- Before running the code, add some rows in the Google Sheet.


Run the Code.gs file in Google Sheets

So you have to run the Code.gs file and select the initialize method. This will do everything for you. It will read all the rows from the Sheets and put that into the firebase realtime DB.

After running the code, you might get some permissions issue. So you can just go with flow and enable all the required things asked. Once execution of the code completes, you can check the realtime database for the entries. (And fun part is, it effects the Realtime database instantly)

This completes the first part i.e. Updating Google sheet changes in Realtime database.

Step 3 :- Create Service account & enable Google Sheets

To implement the second flow (i.e. Saving database changes to the Google Sheets), we need a Google Cloud project with a service account. In this section we will cover how to create a Google Cloud project, get a service account, create a key for the service account and enable Google Sheets for the project.

1. Creating Google Cloud Project 

To create a project go to Google Cloud console and click on projects tab, then you can create a new project from there or choose an existing one.



Creating Google Cloud project

2. Creating a service account and generating the key 

After creating a project, we have to create a service account and enable the key for the service account. This key will be provided in the from of a JSON file that we will use later in the firebase functions.



Creating Credentials for Google Cloud

Go to API and Services → Credentials → Create Credentials → Service account



Creating Service Account in Google Cloud

You will be required to fill some of the information regarding the service account you are creating. Just go with flow and fill all the required details in the form.

After setting up the service account, select the Keys tab in the service account and add a key to the account. This will prompt for the JSON file option and you can download and save it (Later it will be used in the Firebase functions implementation).



Adding key to the Google Cloud Service account

We have the required JSON file, now the Firebase functions can be implemented and we can add the logic for getting updates for particular collection and save them into the sheets.

Step 4 :- Integrating Google Sheets API in Firebase functions

Before going through this step, if you want to setup or know more about the Firebase cloud functions, you can go and check this Cloud Functions Blog. Remember, to implement Firebase function in production, you have to enable Billing on that project.

Once you are done with setup, we can start writing some logic in firebase functions. Below is the structure for the firebase functions module :- 



Firebase Cloud functions’ Folder Structure

In the above folder structure, service account JSON file is added to base project structure. And all the logic related to the sheets and database will be added to index.js file. 

Before adding the logic to the index.js file, you have to install googleapis package.

$ npm i googleapis

Once package is installed you can add the below code to the index.js file.


In the above code, JWT is used to authenticate the user before doing any update into the sheets. This authentication will take place using the service account JSON file.

functions.database.ref(‘/your_collection_name’) — This provides the reference for the collection and to get the updates, update() method is used in the code. (update() method will provide the complete data from the collection including the updated part).

await sheets.spreadsheets.values.update({
auth: jwtClient,
spreadsheetId: spreadsheetId,
range: `A1:B${feedbacks.length}`,  // update this range of cells
valueInputOption: 'RAW',
requestBody: {
values: feedbacks,
majorDimension: 'ROWS'
}
}, {})

The above code will check for authentication, the required spreadsheetId, range up to which you want to save data in the sheet and the request body (containing the database updated data). Finally it updates the data into the sheet. But before testing we have to provide permission to the service account in the required spreadsheet.

Step 5 — Provide permissions to Google Sheet & Test

To provide the permissions to the service account, you just have to share the spreadsheet with the service account client email. Now where will you find your client email ? Client Id can be taken from service Account JSON file or Google cloud project settings. (It will be in below defined format)

service_account_name@google_cloud_project-any_number.iam.gserviceaccount.com

Now share the spreadsheet with this client email. There will be an option to share the spreadsheet on left top corner in Google Sheet.



Share the sheet with service account

After clicking, it will ask for mail id. Just enter/paste the client email to that and it will be shared to the service account. Now we are all set to check the results.

Question — When will the sheet be updated ? Does it update automatically ?

Google Sheet will be updated as soon as the selected collection updates. For example — Once a user updates the feedback in the quiz app, it will be updated in the Google Sheet instantly. 😲

Conclusion

In this tutorial, we learnt how to manage Firebase realtime database using Google sheet. Many of the time we want to have a dashboard to update the data into our application, but due to some constraints we were not able to do so. Using Google sheets with Firebase can be a great option in such situations. This is especially relevant for Mobile apps made with Ionic and React Native, where Firebase database is a popular choice with users.

If you want to know more about Ionic, React Native or Firebase features, you can visit Enappd blogs.


Next Steps

If you liked this blog, you will also find the following blogs interesting and helpful. Feel free to ask any questions in the comment section

Ionic Capacitor

Ionic Cordova


Ionic React Full App with Capacitor

If you need a base to start your next Ionic 5 React Capacitor app, you can make your next awesome app using Ionic 5 React Full App in Capacitor


Ionic 5 React Full App in Capacitor from Enappd

Ionic 5 React Full App in Capacitor from Enappd

Ionic Capacitor Full App (Angular)

If you need a base to start your next Angular Capacitor app, you can make your next awesome app using Capacitor Full App


Capacitor Full App with huge number of layouts and features

Capacitor Full App with huge number of layouts and features

Ionic Full App (Angular and Cordova)

If you need a base to start your next Ionic 5 app, you can make your next awesome app using Ionic 5 Full App


Ionic Full App with huge number of layouts and features

Ionic Full App in Cordova, with huge number of layouts and features



Title
Subtitle
Kicker


integrating-google-sheets-with-firebase-realtime-database
Vaibhav Gehani

Full-stack Developer

SHARE BLOG

PUBLISHED BY

integrating-google-sheets-with-firebase-realtime-database

Vaibhav Gehani

Full-stack Developer
ENAPPD