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
-
Follow step 1 to enable Google Sheets API in your Firebase project: https://developers.google.com/sheets/api/quickstart/nodejs
-
Create a service account in your project; save the json file in the
functions
folder with the file nameserviceAccount.json
. -
Create a spreadsheet in Drive; rename the first worksheet 'Scores'; add Player and Score headers in row 1, columns A and B.
-
Share it with edit access to the email address in your service account.
-
Copy the spreadsheet id (from its URL) to the
spreadsheetId
string in the TypeScript source. -
npm install firebase-admin firebase-functions googleapis@34 lodash
-
Deploy this (TypeScript) code.
-
Update the keys/values in your database under
/scores
and watch them get updated in the sheet!
I've done this tutorial so I'm gonna scribble some of problems I have faced woking out.
To make a project, in your terminal, make a directory and
npm install -g firebase-tools
firebase login
firebase init functions
and I've chosen Use an existing project since I already had project. Select your project, select TypeScript, TSLint no, install dependencies yes and you are all set.npm install firebase-tools
and when firebase returns command not found, typealias firebase="
npm config get prefix/bin/firebase"
)functions
folder. This is where you should saveserviceAccount.json
file.Get your json file from APIs & Auth > Credentials in the Google Developers Console and select Service account from the Add credentials dropdown, not from the link in step1. This is the mistake I’ve made. It will result in giving out errors
No key or keyFile set at GoogleToken.getTokenAsync
.You should fill out spreadSheetId in index.js file. If your docs link looks like this,
https://docs.google.com/spreadsheets/d/As1ksdflk8QL7XCXMnS3rpQ/edit#gid=12345678 (This is made up but your link will look like this anyway)
spreadSheetId is behind /d/ and before /edit/.
firebase deploy
cd functions
npm install googleapis
and it worked well.
Also, don’t forget to share your document with Service account.
( my service account looked like
firebase-adminsdk-somekeyword@jsceno-93906.iam.gserviceaccount.com
)Also, this example adds only up to 6 rows. Filling out more than 6 items in firebase realtime db also causes error.
Hope this helps many people and saves time... I spent a lot of time figuring these out.