Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Getting data from Google Sheets tutorial and example added #682

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions docs/documentation/google-sheets-integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Getting data from Google Sheets

**Advanced topic**


## How to use

1. Follow step 1 & 2 of the following blog post:

[https://medium.com/dilwoar-hussain/use-google-sheets-as-a-database-in-3-easy-steps-d0330a4bb3b3](https://medium.com/dilwoar-hussain/use-google-sheets-as-a-database-in-3-easy-steps-d0330a4bb3b3)

2. In app/routes.js add:

```javascript
const request=require('request')
const csv=require('csvtojson')

```

```javascript

router.get('/', function (req, res) {
var googleSheetsUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-1vQ8oXOIqewtlNTyJvplT-QYSlX9UoB8XlV0gSTYBFHxtlF3HwdkVp-vJP7FIVgHhTheL8nKYxcaNu2t/pub?output=csv"; //this is the url for your CSV

csv()
.fromStream(request.get(googleSheetsUrl))
.then((googleSheetsData)=>{
res.render('google-sheets-example', { googleSheetsData: googleSheetsData } )
});

})

```

2. In the view add:

```HTML
<table class="govuk-table">
<caption class="govuk-table__caption">Tunbridge Wells Open Data - Polling Districts</caption>
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="col">OBJECTID</th>
<th class="govuk-table__header" scope="col">Letter</th>
<th class="govuk-table__header" scope="col">Polling_Di</th>
<th class="govuk-table__header" scope="col">Ward</th>
<th class="govuk-table__header" scope="col">Parliament</th>
<th class="govuk-table__header" scope="col">ADDRESS</th>
<th class="govuk-table__header" scope="col">County_Div</th>
<th class="govuk-table__header" scope="col">ShapeSTArea</th>
</tr>
</thead>
<tbody class="govuk-table__body">
{% for item in googleSheetsData %}
<tr class="govuk-table__row">
<td class="govuk-table__cell">{{ item["OBJECTID"] }}</td>
<td class="govuk-table__cell">{{ item['Letter'] }}</td>
<td class="govuk-table__cell">{{ item['Polling_Di'] }}</td>
<td class="govuk-table__cell">{{ item['Ward'] }}</td>
<td class="govuk-table__cell">{{ item['Parliament'] }}</td>
<td class="govuk-table__cell">{{ item['ADDRESS'] }}</td>
<td class="govuk-table__cell">{{ item['County_Div'] }}</td>
<td class="govuk-table__cell">{{ item['ShapeSTArea'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
```

See [Demo](/docs/examples/google-sheets)
14 changes: 14 additions & 0 deletions docs/documentation_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const path = require('path')
const express = require('express')
const marked = require('marked')
const router = express.Router()
const request = require('request')
const csv = require('csvtojson')

// Local dependencies
const utils = require('../lib/utils.js')
Expand Down Expand Up @@ -67,6 +69,18 @@ router.post('/examples/branching/over-18-answer', function (req, res) {
}
})

// Example - Google Sheets integration
router.get('/examples/google-sheets', function (req, res) {
var googleSheetsUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-1vQ8oXOIqewtlNTyJvplT-QYSlX9UoB8XlV0gSTYBFHxtlF3HwdkVp-vJP7FIVgHhTheL8nKYxcaNu2t/pub?output=csv";

csv()
.fromStream(request.get(googleSheetsUrl))
.then((googleSheetsData)=>{
res.render('examples/google-sheets-example', { googleSheetsData: googleSheetsData } )
});

})

module.exports = router

// Strip off markdown extensions if present and redirect
Expand Down
48 changes: 48 additions & 0 deletions docs/views/examples/google-sheets-example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

{% extends "layout.html" %}

{% block pageTitle %}
GOV.UK Prototype Kit
{% endblock %}

{% block content %}

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">
Google Sheets example
</h1>

<table class="govuk-table">
<caption class="govuk-table__caption">Tunbridge Wells Open Data - Polling Districts</caption>
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="col">OBJECTID</th>
<th class="govuk-table__header" scope="col">Letter</th>
<th class="govuk-table__header" scope="col">Polling_Di</th>
<th class="govuk-table__header" scope="col">Ward</th>
<th class="govuk-table__header" scope="col">Parliament</th>
<th class="govuk-table__header" scope="col">ADDRESS</th>
<th class="govuk-table__header" scope="col">County_Div</th>
<th class="govuk-table__header" scope="col">ShapeSTArea</th>
</tr>
</thead>
<tbody class="govuk-table__body">
{% for item in googleSheetsData %}
<tr class="govuk-table__row">
<td class="govuk-table__cell">{{ item["OBJECTID"] }}</td>
<td class="govuk-table__cell">{{ item['Letter'] }}</td>
<td class="govuk-table__cell">{{ item['Polling_Di'] }}</td>
<td class="govuk-table__cell">{{ item['Ward'] }}</td>
<td class="govuk-table__cell">{{ item['Parliament'] }}</td>
<td class="govuk-table__cell">{{ item['ADDRESS'] }}</td>
<td class="govuk-table__cell">{{ item['County_Div'] }}</td>
<td class="govuk-table__cell">{{ item['ShapeSTArea'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>

{% endblock %}
3 changes: 3 additions & 0 deletions docs/views/tutorials-and-examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ <h3 class="govuk-heading-s">Advanced usage</h3>
<li>
<a href="/docs/session">Storing data in session</a>
</li>
<li>
<a href="/docs/google-sheets-integration">Getting data from Google Sheets</a>
</li>
<li>
<a href="/docs/using-verify">Using GOV.UK Verify</a>
</li>
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"client-sessions": "^0.8.0",
"cookie-parser": "^1.4.3",
"cross-spawn": "^6.0.5",
"csvtojson": "^2.0.8",
"del": "^3.0.0",
"dotenv": "^6.0.0",
"express": "4.16.3",
Expand All @@ -41,6 +42,7 @@
"nunjucks": "^3.1.3",
"portscanner": "^2.1.1",
"prompt": "^1.0.0",
"request": "^2.88.0",
"require-dir": "^1.0.0",
"sync-request": "^6.0.0",
"universal-analytics": "^0.4.16",
Expand Down