Get data from Google spreadsheets as Javascript objects using this simple API and take the advantage of having a ready made CMS and readonly data store for your project.
- In a browser:
<script src="./dist/google-spreadsheets-db.min.js"></script>
- Using npm:
$ npm i --save google-spreadsheets-db
- In Node.js:
const GoogleSpreadsheetsDb = require('google-spreadsheets-db')
- DB instance:
const db = new GoogleSpreadsheetsDb(
'YOUR-API-KEY-HERE',
'SPREADSHEET-ID-HERE'
)
- Retrieving all entries of a sheet:
db.getAll('sheet-name', (err, entries) => {
/**
* entries will be an array of objects
* the object's keys are the column names of your sheet, ie:
* [{ name: "First item", description: "some desc." },
* { name: "First item", description: "some desc." }]
*/
})
Use the first row for column names, like:
id | name | description | images |
---|---|---|---|
1 | First item | some description goes here | ["https://somedomain.jpg"] |
2 | Second item | some description goes here | ["https://somedomain.jpg"] |
See example
The spreadsheet ID is the value between the "/d/" and the "/edit" in the URL of your spreadsheet. Use it to instanciate GoogleSpreadsheetsDb.
https://docs.google.com/spreadsheets/d/spreadsheetId/edit#gid=0
Setup the sheets names by clicking in the tabs at the bottom of the spreadsheet. By default the first sheet is named Sheet1.
Follow this wizard in order to generate an API key.
You will also need to update permissions in your spreadsheet:
- Click the Share link in the upper right-hand corner
- Change access to "On - Anyone with a link"