Created
September 5, 2021 05:07
-
-
Save vaibhavgehani/98a1ada6d28b3c1085689bb3feee6e9e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const functions = require('firebase-functions'); | |
| const { google } = require('googleapis'); | |
| const sheets = google.sheets('v4') | |
| const spreadsheetId = 'your_spread_sheet_id' | |
| const serviceAccount = require('./serviceAccount.json') | |
| // Required for authentication | |
| 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() | |
| // functions for updating sheet through database collection | |
| exports.updateSheet = functions.database.ref('/your_collection_name').onUpdate(async change => { | |
| const data = change.after.val() | |
| let feedbacks = []; | |
| Object.keys(data).forEach((key) => { | |
| feedbacks.push([data[key].questionId, data[key].feedback]); | |
| }); | |
| await jwtAuthPromise | |
| 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' | |
| } | |
| }, {}).then((sheetRes) => { | |
| console.log('Susscess', sheetRes); | |
| }).catch((err) => { | |
| console.log('Error', err); | |
| }) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment