Skip to content

Pull Google Sheets Data Into an Eleventy Site #1864

@crockwave

Description

@crockwave

I figured out how to have a Google Sheet be used as a data source to populate an Eleventy rendered page. Once it is setup, you just need to edit the Google Sheet, run a bash script to download the Google Sheet JSON data, then run the Eleventy build.

  1. Create a Google Sheet, publish it to the web, and expose it as JSON, using this article as a reference: https://medium.com/@scottcents/how-to-convert-google-sheets-to-json-in-just-3-steps-228fe2c24e6

image

  1. Create a bash script to download the Google Sheet JSON and save it to the Eleventy site folder:
#!/bin/sh

# a shell script used to download links spreadsheet in json form

DIR=/www/my_site_folder

# wget output file
FILE=my_data.json

# wget log file
LOGFILE=wget.log

# wget download url
URL=spreadsheets.google.com/feeds/list/Your_Sheet_ID/od6/public/values?alt=json

cd $DIR
wget $URL -O $FILE -o $LOGFILE
  1. Create an eleventyComputed.js file in the Eleventy data folder (Note, the Google Sheet JSON file is a nested object, so you should extract the entry object before passing to the Eleventy data array to reduce the data complexity in the page template)
module.exports = {
  eleventyComputed: {
    myData: data => {
      return data.my_data.feed.entry;
    }
  }
};
  1. Create a page template that uses the data.my_data data (njk file example from Skeleventy repo). This example assumes you have a Google Sheet with column header values of Location, Name, Description, URL. The JSON data presents the key values as all lowercase. You can uncomment the {{ myData | log }} in the page template if you want to see the myData object in the console output during the build process. You can also access the data.my_data object from the template if you want to use that method.)
---
layout: default
meta_title: sheetsdata
meta_description: Data from Google Sheets
body_class: sheetsdata
title: Data from Google Sheets
---

{# {{ myData | log }} #}

<main id="main" class="main inner flex flex-1 flex-col py-10 lg:py-20 focus:outline-none" tabindex="-1">

    <section id="container-centre" class="column centre flex-1">

        <h1 class="page-title">{{ title }}</h1>

        <div class="content">

            <p>Data from Google Sheets.</p>

            <ul class="mb-12 list-disc pl-4">
                {% for item in myData %}
                    <li>location: {{ item.gsx$location.$t }}, name: {{ item.gsx$name.$t }}, description: {{ item.gsx$description.$t }}, link: {{ item.gsx$url.$t }}</li>
                {% endfor %}
            </ul>

        </div>

    </section>

</main>

This example renders the Google Sheet data on the page as follows:

image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions