-
Star
(109)
You must be signed in to star a gist -
Fork
(23)
You must be signed in to fork a gist
-
-
Save CodingDoug/ffc4f050cc489a0280eb7f4cbe36af07 to your computer and use it in GitHub Desktop.
| // 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. | |
| // Watch this code in action in this tweet: | |
| // https://twitter.com/CodingDoug/status/940022568089554944 | |
| // 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; require() it to "serviceAccount". | |
| // | |
| // 3. Create a spreadsheet in Drive; add Player and Score headers in row 1. | |
| // | |
| // 4. Share it with edit access to the email address of your service acct. | |
| // | |
| // 5. Copy the spreadsheet id (from its URL) to the spreadsheetId string. | |
| // | |
| // 6. `npm install firebase-admin firebase-functions googleapis lodash` | |
| // | |
| // 6. Deploy this (TypeScript) code. | |
| // | |
| // 7. Update the keys/values in your database under /scores and watch them | |
| // get updated in the sheet! | |
| import * as functions from 'firebase-functions' | |
| import * as google from 'googleapis' | |
| import * as _ from 'lodash' | |
| const sheets = google.sheets('v4') | |
| const spreadsheetId = '1_Ohp_hbH_615R21z7v9DIt4cYHwgzFcg7rRE47A3dmg' | |
| const serviceAccount = require('../firesheetz-yo-60d1f72013a1.json') | |
| const jwtClient = new google.auth.JWT( | |
| serviceAccount.client_email, | |
| null, | |
| serviceAccount.private_key, | |
| ['https://www.googleapis.com/auth/spreadsheets'], // read and write sheets | |
| null | |
| ); | |
| const jwtAuthPromise = new Promise((resolve, reject) => { | |
| jwtClient.authorize((err, tokens) => { | |
| console.info("Tokens", tokens) | |
| if (err) { | |
| console.error(err) | |
| reject(err) | |
| return | |
| } | |
| resolve(tokens) | |
| }) | |
| }) | |
| export const copyScoresToSheet = functions.database.ref('/scores').onUpdate(event => { | |
| const data = event.data.val() | |
| console.info(data) | |
| // Sort the scores. scores is an array of arrays each containing name and score. | |
| const scores = _.map<any, [string, number]>(data, (value, key) => [String(key), value]) | |
| scores.sort((a,b) => {return b[1] - a[1]}) | |
| return jwtAuthPromise.then(tokens => { | |
| return new Promise((resolve, reject) => { | |
| sheets.spreadsheets.values.update({ | |
| auth: jwtClient, | |
| spreadsheetId: spreadsheetId, | |
| range: 'Scores!A2:B7', // update this range of cells | |
| valueInputOption: 'RAW', | |
| resource: { values: scores } | |
| }, (err, result) => { | |
| if (err) { | |
| console.log(err) | |
| reject(err) | |
| return | |
| } | |
| console.log(result) | |
| resolve(result) | |
| }) | |
| }) | |
| }) | |
| }) |
@ArtosProjets do you want to use service accounts or want a different solution?
https://github.com/googleapis/google-api-nodejs-client link is expired. use this one.
I am getting error `SyntaxError: Unexpected
Where and When are you exactly getting this error?
Can you help with some code snippets?
Does anyone get error No key or keyFile set at GoogleToken.getTokenAsync ~~ ?
Is anyone facing error requested entity was not found?
I've done this tutorial so I'm gonna scribble some of problems I have faced woking out.
- In step two, it says to create a service in your project.
To make a project, in your terminal, make a directory and
npm install -g firebase-tools
firebase login
firebase init functionsand 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.
- (if npm install firebase-tools gives error, try
npm install firebase-toolsand when firebase returns command not found, typealias firebase="npm config get prefix/bin/firebase")
- Now, you will see
functionsfolder. This is where you should saveserviceAccount.jsonfile.
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.
- In your directory, from functions/src/index.ts, copy & paste the file firesheetz.ts from above.
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/.
- Now, to Deploy,
firebase deploy
- if you get errors about googleapis not existing, try
cd functions
npm install googleapis
and it worked well.
-
Also, don’t forget to share your document with Service account.
( my service account looked likefirebase-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.
I've done this tutorial so I'm gonna scribble some of problems I have faced woking out.
- In step two, it says to create a service in your project.
To make a project, in your terminal, make a directory and
npm install -g firebase-tools
firebase login
firebase init functionsand 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.
- (if npm install firebase-tools gives error, try
npm install firebase-toolsand when firebase returns command not found, typealias firebase="npm config get prefix/bin/firebase")
- Now, you will see
functionsfolder. This is where you should saveserviceAccount.jsonfile.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.
- In your directory, from functions/src/index.ts, copy & paste the file firesheetz.ts from above.
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/.
- Now, to Deploy,
firebase deploy
- if you get errors about googleapis not existing, try
cd functions
npm install googleapisand it worked well.
- Also, don’t forget to share your document with Service account.
( my service account looked likefirebase-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.
If you are facing error with realtime database you can use firestore.
Here is a link to the gist.
I've done this tutorial so I'm gonna scribble some of problems I have faced woking out.
- In step two, it says to create a service in your project.
To make a project, in your terminal, make a directory and
npm install -g firebase-tools
firebase login
firebase init functionsand 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.
- (if npm install firebase-tools gives error, try
npm install firebase-toolsand when firebase returns command not found, typealias firebase="npm config get prefix/bin/firebase")
- Now, you will see
functionsfolder. This is where you should saveserviceAccount.jsonfile.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.
- In your directory, from functions/src/index.ts, copy & paste the file firesheetz.ts from above.
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/.
- Now, to Deploy,
firebase deploy
- if you get errors about googleapis not existing, try
cd functions
npm install googleapis
and it worked well.
- Also, don’t forget to share your document with Service account.
( my service account looked likefirebase-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.
If you are facing error with realtime database you can use firestore.
Here is a link to the gist.
Thanks for the link but I've already made it with RealtimeDB and that comment was guide for others.
Here are detailed steps on syncing Google Sheets and Firebase: https://github.com/alexandermckay/sync-firebase-with-google-sheets
im already lost in step 2, can you explain more specific?
im already lost in step 2, can you explain more specific?

You can create the service account by following this link
I've done this tutorial so I'm gonna scribble some of problems I have faced woking out.
1. In step two, it says to create a service in your **project**. 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. * (if npm install firebase-tools gives error, try `npm install firebase-tools` and when firebase returns command not found, type `alias firebase="`npm config get prefix`/bin/firebase"`) 1. Now, you will see `functions` folder. This is where you should save **`serviceAccount.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.1. In your directory, from **functions/src/index.ts**, copy & paste the file **firesheetz.ts from above**. 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/.
1. Now, to Deploy,
firebase deploy* if you get errors about googleapis not existing, try
cd functions
npm install googleapisand it worked well.
1. Also, **don’t forget to share your document with Service account.** ( my service account looked like `firebase-adminsdk-somekeyword@jsceno-93906.iam.gserviceaccount.com` ) 2. 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.
Thank you 👍
I created a detailed post out this, i have also improved the code to automatically flatten the deep JSON place it in the sheet. This works with nested json documents as well.
https://websiddu.com/blog/sync-data-from-firebase-to-google-sheets.html
I created a detailed post out this, i have also improved the code to automatically flatten the deep JSON place it in the sheet. This works with nested json documents as well.
https://websiddu.com/blog/sync-data-from-firebase-to-google-sheets.html
I
I created a detailed post out this, i have also improved the code to automatically flatten the deep JSON place it in the sheet. This works with nested json documents as well.
https://websiddu.com/blog/sync-data-from-firebase-to-google-sheets.html
I got stuck in step-4 pls help websiddu
i am working android studio i dont know node js pls help me wht to do
Seems like you don't have the uptodate version of the firebase-tools try updating the firebase tools and rerun the command.
websiddu i need your help. im following your step but, i got stuck when deploy it. igot messege " Functions deploy had errors with the following functions:
SyncToSheets" how i can solve this messege?
The error says update your node version. In you package.json try changing to "node": 12
I have the same problem as @Drewan-25
Any idea about the above error, it does not like the script. Because of the cli version or ts version?
@guillegregoret and @Drewan-25
i think you must install this npm install firebase-admin firebase-functions googleapis lodash –save. in functions folder
Any idea about the above error, it does not like the script. Because of the cli version or ts version?
@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
can i do vice versa?
have you find any solution , if yes than please suggest
Any idea about the above error, it does not like the script. Because of the cli version or ts version?
@guillegregoret and @Drewan-25
i think you must install this npm install firebase-admin firebase-functions googleapis lodash –save. in functions folderThanks!!! 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
}
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 !
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
}





Sure I can try that but it might take time !!