From 836ecfe7ce016db5a136def104a3f8bab69ab11b Mon Sep 17 00:00:00 2001 From: Dilwoar Hussain Date: Mon, 4 Feb 2019 14:01:50 +0000 Subject: [PATCH] Getting data from Google Sheets tutorial and example added --- .../google-sheets-integration.md | 69 +++++++++++++++++++ docs/documentation_routes.js | 14 ++++ .../views/examples/google-sheets-example.html | 48 +++++++++++++ docs/views/tutorials-and-examples.html | 3 + package.json | 2 + 5 files changed, 136 insertions(+) create mode 100644 docs/documentation/google-sheets-integration.md create mode 100644 docs/views/examples/google-sheets-example.html diff --git a/docs/documentation/google-sheets-integration.md b/docs/documentation/google-sheets-integration.md new file mode 100644 index 0000000000..b5094aae14 --- /dev/null +++ b/docs/documentation/google-sheets-integration.md @@ -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 + + + + + + + + + + + + + + + + {% for item in googleSheetsData %} + + + + + + + + + + + {% endfor %} + +
Tunbridge Wells Open Data - Polling Districts
OBJECTIDLetterPolling_DiWardParliamentADDRESSCounty_DivShapeSTArea
{{ item["OBJECTID"] }}{{ item['Letter'] }}{{ item['Polling_Di'] }}{{ item['Ward'] }}{{ item['Parliament'] }}{{ item['ADDRESS'] }}{{ item['County_Div'] }}{{ item['ShapeSTArea'] }}
+``` + +See [Demo](/docs/examples/google-sheets) diff --git a/docs/documentation_routes.js b/docs/documentation_routes.js index 4163499008..7dba1b47a3 100644 --- a/docs/documentation_routes.js +++ b/docs/documentation_routes.js @@ -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') @@ -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 diff --git a/docs/views/examples/google-sheets-example.html b/docs/views/examples/google-sheets-example.html new file mode 100644 index 0000000000..6b25b818eb --- /dev/null +++ b/docs/views/examples/google-sheets-example.html @@ -0,0 +1,48 @@ + +{% extends "layout.html" %} + +{% block pageTitle %} + GOV.UK Prototype Kit +{% endblock %} + +{% block content %} + +
+
+

+ Google Sheets example +

+ + + + + + + + + + + + + + + + + {% for item in googleSheetsData %} + + + + + + + + + + + {% endfor %} + +
Tunbridge Wells Open Data - Polling Districts
OBJECTIDLetterPolling_DiWardParliamentADDRESSCounty_DivShapeSTArea
{{ item["OBJECTID"] }}{{ item['Letter'] }}{{ item['Polling_Di'] }}{{ item['Ward'] }}{{ item['Parliament'] }}{{ item['ADDRESS'] }}{{ item['County_Div'] }}{{ item['ShapeSTArea'] }}
+
+
+ +{% endblock %} diff --git a/docs/views/tutorials-and-examples.html b/docs/views/tutorials-and-examples.html index b42cd759eb..5e3a9e7496 100644 --- a/docs/views/tutorials-and-examples.html +++ b/docs/views/tutorials-and-examples.html @@ -102,6 +102,9 @@

Advanced usage

  • Storing data in session
  • +
  • + Getting data from Google Sheets +
  • Using GOV.UK Verify
  • diff --git a/package.json b/package.json index 7bcc5e71fa..ce31c8f298 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",