-
Notifications
You must be signed in to change notification settings - Fork 1
Dev Google Spreadsheets
Go to Create Database
Go to Create Database
The first row contains your keys. The first key should be set to “id” and each row should be labeled with the corresponding number, starting with “1”. An easy way to set the id for each row in column A is to enter this formula “=COUNTA($B$2:B2)” into cell A2 and then apply that to all rows. You can add as many rows or columns as you need.
In the menu, go to Tools -> Script editor
It will take you to a code editor with the following file open: Code.gs. Replace the contents with this snippet. Search for this code at the top of the file:
Replace the “spreadsheetID” placeholder with your own. The ID is the bolded part in the full spreadsheet URL (e.g. https://docs.google.com/spreadsheets/d/spreadsheetID/edit#gid=0) Replace the “firebaseUrl” placeholder with your database URL from Step 2. Make sure to include the trailing slash (e.g. https://sheets-sample-test.firebaseio.com/) otherwise it will throw an error. In your menu, go to View -> Show manifest file, which will add a file called appsscript.json.
This will add an appsscript.json file to your project. Replace the contents with the following snippet.
In the menu, go to Run -> Run function -> initialize. You’ll see a prompt to review and accept the permissions. This allows the App Script project to access the spreadsheet and upload data to Firebase. Click “Review Permissions” and then click “Allow.”
Congrats! Your Firebase Realtime database has now been populated with the data from your spreadsheet! Any further edits will sync seamlessly and you can even share your spreadsheet with other people.
If you are feeling fancy, you can add a little more security to the Firebase database. Go to the Firebase Console -> Database and change your rules to the following.
You’ll have to implement Firebase Authentication on your prototype, so that your users can read the data. Setting the write method to false means only your spreadsheet can write to the database.
In the sheet if your id starts with 0 and increments by 1, then the script will generate an array instead of a key value pairs.
When converting a table to JSON, it’s only possible to do one level of nesting. But what if you need nested data? The script provided above has a special function that lets you create a nested object. Simple name the title of the column with the path of the object join key using a double underscore __. If you want to nest street under address you can simply say ‘address__street’, this means you cannot have a column named just address in your sheet.
This will generate an object like the one below, and you can also nest at any level.
More details Here
A BIMSOCKet team Original Production