Skip to content

Instantly share code, notes, and snippets.

@CodingDoug
Last active December 17, 2022 10:23
Show Gist options
  • Save CodingDoug/ffc4f050cc489a0280eb7f4cbe36af07 to your computer and use it in GitHub Desktop.
Save CodingDoug/ffc4f050cc489a0280eb7f4cbe36af07 to your computer and use it in GitHub Desktop.
Copying data from Firebase Realtime Database to a Google Sheet in real time via Cloud Functions

Copying data from Firebase Realtime Database to a Google Sheet in real time via Cloud Functions

If you're trying to do this, you came to the right place!

Watch this code work in real time: https://twitter.com/CodingDoug/status/940022568089554944

See also this gist for copying in the other direction: https://gist.github.com/CodingDoug/44ad12f4836e79ca9fa11ba5af6955f7

Setup

  1. Follow step 1 to enable Google Sheets API in your Firebase project: https://developers.google.com/sheets/api/quickstart/nodejs

  2. Create a service account in your project; save the json file in the functions folder with the file name serviceAccount.json.

  3. Create a spreadsheet in Drive; rename the first worksheet 'Scores'; add Player and Score headers in row 1, columns A and B.

  4. Share it with edit access to the email address in your service account.

  5. Copy the spreadsheet id (from its URL) to the spreadsheetId string in the TypeScript source.

  6. npm install firebase-admin firebase-functions googleapis@34 lodash

  7. Deploy this (TypeScript) code.

  8. Update the keys/values in your database under /scores and watch them get updated in the sheet!

Helpful documentation

// Copyright 2017 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as functions from 'firebase-functions'
import * as _ from 'lodash'
import { google } from 'googleapis'
const sheets = google.sheets('v4')
const spreadsheetId = 'YOUR_SPREADSHEET_ID_HERE'
const serviceAccount = require('../serviceAccount.json')
const jwtClient = new google.auth.JWT({
email: serviceAccount.client_email,
key: serviceAccount.private_key,
scopes: [ 'https://www.googleapis.com/auth/spreadsheets' ], // read and write sheets
})
const jwtAuthPromise = jwtClient.authorize()
type Scores = { string: number }
export const copyScoresToSheet = functions.database.ref('/scores').onUpdate(async change => {
const data: Scores = change.after.val()
// Sort the scores. scores is an array of arrays each containing name and score.
const scores = _.map<Scores, [string, number]>(data, (value, key) => [key, value])
scores.sort((a,b) => { return b[1] - a[1] })
await jwtAuthPromise
await sheets.spreadsheets.values.update({
auth: jwtClient,
spreadsheetId: spreadsheetId,
range: 'Scores!A2:B7', // update this range of cells
valueInputOption: 'RAW',
requestBody: { values: scores }
}, {})
})
@demirholo
Copy link

Any idea about the above error, it does not like the script. Because of the cli version or ts version?

image

@Drewan-25
Copy link

Hellow, i got problem after firebase deploy
Screenshot_77

@guillegregoret
Copy link

I have the same problem as @Drewan-25

@Abrar112
Copy link

Abrar112 commented Mar 13, 2021

Any idea about the above error, it does not like the script. Because of the cli version or ts version?

image

@guillegregoret and @Drewan-25

i think you must install this npm install firebase-admin firebase-functions googleapis lodash –save. in functions folder

@guillegregoret
Copy link

Any idea about the above error, it does not like the script. Because of the cli version or ts version?
image

@guillegregoret and @Drewan-25

i think you must install this npm install firebase-admin firebase-functions googleapis lodash –save. in functions folder

Thanks!!! It deployed correctly in the first try

@seanho96
Copy link

Is this only possible with Firebase's blaze plan? Anyone has experience with how the charges are like for pretty small scale projects (< 1000 calls)?

image

@chirag9027
Copy link

chirag9027 commented Jul 12, 2021

can i do vice versa?

have you find any solution , if yes than please suggest

@iamuseful
Copy link

Any idea about the above error, it does not like the script. Because of the cli version or ts version?
image

@guillegregoret and @Drewan-25
i think you must install this npm install firebase-admin firebase-functions googleapis lodash –save. in functions folder

Thanks!!! It deployed correctly in the first try

I had the same challenge...
I realized that it was caused by dependencies versions..... in package.json

I used the following versions, and it worked afterwards

{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1",
"googleapis": "^39.2.0",
"lodash": "^4.17.21"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6"
},
"private": true
}

@loobeet
Copy link

loobeet commented Jul 22, 2022

Hi all, thanks for all these inputs. I succeeded to create an automatic sync with one database. I have a second data base with more than 26 columns, does anyone know how we can create a log to replicate the Google Sheets algorithm of range generation ?
Thanks a lot !

@skfaisal93
Copy link

Functions deploy had errors with the following functions:
        SyncToSheets(us-central1)

I am getting this error while deploying the function. Here is my package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "12"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment