After happily accepting the invitation, we got together to plan and build the event site and, the tool of choice was, without much surprise, GatsbyJS. The site was pretty basic and it was used to list the speaker, date, and time of each talk. To make updating the site content easier, we decided to use a Google Spreadsheet
I will list some of the benefits of using a Google Spreadsheet
Provides a GUI to edit content from Google Sheet.
Multiplatform access the sheet from any device.
Real-time collaboration by sharing the sheet with multiple people
Backups and version control included out-of-the-box.
How was the Gatsby site implemented?
The starter
For this particular case, we used the gatsby-v2-tutorial-starter. I have to mention that I rather like to extend Gatsby implementations from a theme to be able to keep in sync with theme updates but also to simplify override features. But for a site this simple using a starter works great.
This plugin allows you to source all data from a Google Spreadsheet into a GraphQL type for build-time consumption.
The plugin is based on the node-sheets package and the Google Sheets API V4, which allows for better value types and column names than many of the other Gatsby Google Sheets source plugins.
Authentication shown in this example is using an api key you have created in the google developers console.
Plugin configuration using your gatsby-config.js file:
Note we are using a stringified version of the google-service-account data and then use the GOOGLE_SERVICE_ACCOUNT_CREDENTIALS environment variable in order to provide the data to the Gatsby site to avoid having the file deployed or committed by mistake on the repository.
Creating the ImageSharpNode from sheet data
We decided to make it simple by mapping previously uploaded speaker-avatar images with the field avatar stored on the sheet by adding code to onCreateNode at gatsby-node.js file.
Validate node.internal.owner is equal to gatsby-source-google-spreadsheet to affect only this node types.
Read avatar field value from sheet data and if null then we defaulted as avatar.
Next step, use the getImageSharp function to retrieve the ImageSharpNode from Gatsby and if not found, then retrieve the default ImageSharpNode using avatar as search value.
Finally, add the childrenFile node using the createParentChildLink function and passing current node as parent and the retrieved imageSharpNode as child.
The Index page
Add the GraphQL query to retrieve the sheet data ordered by date field.
Nothing fancy here, just usage of the map function to iterate and pass current item properties to the PostList React component.
You can click here to take a look at the PostList component code.
Deploying the site to Netlify without leaving your Google Spreadsheet
In order to accomplish this task, it was necessary to add a script to the Google Spreadsheet. This was done by selecting the menu item Tools > Script editor and adding this code.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Scripts')
.addItem('Build', 'build')
.addToUi();
}
function build() {
UrlFetchApp.fetch('https://api.netlify.com/build_hooks/<uuid>', {
'method': 'post',
})
}
This will add a new menu item on your Google Spreadsheet menu. By clicking this you will be able to trigger a new deploy to Netlify without leaving your sheet.
Do you think you have any other feature idea or comment you think could be interesting or useful. Let us know in the comments below.
Ready to embrace the JAMstack revolution?
Build fast and secure sites and apps delivered by pre-rendering files and serving them directly from a CDN, removing the requirement to manage or run web servers, databases and worry about traffic spikes.