Extract Data from Google Sheets
Tags: google, spreadsheet, jamstack, javascript
Published: 2022-03-25 04:48:30

As a JAMStack developer, I have often found a situation where all I need is a way to get data and display it on a website. There are a lot of options out there, like creating a database with an API, using serverless technology like Firebase, or many other options. But that often seems a little overkill. 

Sometimes, all I need is a spreadsheet as a read-only data source. Google Sheets is an appealing option because it is familiar to many people, is available as a web application, and makes storing and organizing data a breeze. Wouldn't it be nice to use this as a data source?

One solution I have used to get Google Sheet data was [Tabletop.js](https://github.com/jsoma/tabletop) which was written several years ago and no longer works. Other solutions out there cost money. Some seem a little questionable when you hand over your Google credentials allowing them access to your Google Drive. 

## Introducing: Extract GSheet

Since I wasn't happy with any of these solutions, I decided to write and publish my own library to extract data from a Google Sheet. 

Extract GSheet is a front-end JavaScript library that extracts your published Google Sheet data making it available as a JavaScript object. It can even pull data from multiple tabs within the same document! From there, you can display the content however you would like. Unlike many other libraries, this library does not require an API key.

### Example

Take a look at the following CodePen to see how easy it is to extract data from [this published Google Sheet data](https://docs.google.com/spreadsheets/d/e/2PACX-1vTRI47Ifj6D_US-HpggfPwSGU1nBSz81IaQ3FL3eqqjr2m8ZIdr6ia9L_FqsJqYVeUI_A2SDjm25FjG/pubhtml).

{% codepen https://codepen.io/ngblaylock/pen/KKvGqvw %}

## Features

1. Extract GSheet will pull out all text data in the table and convert it as needed. For example, "1" ⇒ 1, "TRUE" ⇒ true, and will convert checkboxes to a true/false value.
2. It will use the first row in the sheet as the key for the key/value pairs.
3. It will remove empty rows and columns to keep the data clean.
4. If there is no column labeled `id`, it will add an id for each non-empty row.

## What This Won't Do

1. Extract GSheet is intended to only read data from a Google Sheet. It cannot write data to a Google Sheet. If you need to update information on your website, you will need to edit the Google Sheet. Any changes on your Google Sheet should automatically publish once changed. However, some changes may take up to 5 minutes to reflect on your page.
2. Extract GSheet relies on `DomParser` and `fetch` which are not native to Node.js. Because of this, it will only work in a browser.
3. Extract GSheet will not extract non-text items such as images, comments, and charts.
4. It does not work in Internet Explorer.

## Documentation and Examples

You should have a pretty good idea of how easy it is to implement based on the CodePen above, but if you still need additional help, you can find more information in the [documentation](https://ngblaylock.github.io/extract-gsheet/documentation/).

I also have examples of formatting data using different libraries and frameworks such as jQuery, Vue, React, and more. You can find those examples at [https://ngblaylock.github.io/extract-gsheet/](https://ngblaylock.github.io/extract-gsheet/).

## Questions?

If you have questions, drop them in the comments here, or let me know on [GitHub](https://github.com/ngblaylock/extract-gsheet). I hope you find this useful.

## Links

- [Documentation and Examples](https://ngblaylock.github.io/extract-gsheet/)
- [extract-gsheet on NPM](https://www.npmjs.com/package/extract-gsheet)
- [CodePen Example](https://codepen.io/ngblaylock/pen/KKvGqvw)