Let’s start off by creating a Google sheet for us to read from. There’s actually a really cool way to do this, open up Chrome and type sheets.new in the address bar - Done!
Add some dummy data to the sheet to make this a little more interesting. And then when you are done, share the sheet publicly so that anybody who has the link can read the sheet:
Quick note - that long bit of text after the https://docs.google.com/spreadsheets/d/ is your sheet’s ID.
Visit https://console.cloud.google.com/apis/dashboard and select a project, then tap on enable API’s and services. Search for Google Sheets and enable the API. Click manage and then find the credentials tab.
Find the App engine default service account, click on it and then click on the keys tab to generate one - It will download a JSON file for you, this will give you access and you can download it and store it in the root of your Node project (NOTE: You wanna keep it secret, don’t upload it to Github if you check in your code).
Start by importing googleapis for use in the JavaScript file. I also like to create a spreadsheetId variable for ease of use.
Finally lets just console log the output so you can see the data come back
Here is the console output:
if(window.strchfSettings === undefined) window.strchfSettings = {}; window.strchfSettings.stats = {url: "https://itskofio.storychief.io/en/node-js-read-google-sheet?id=924879768&type=26",title: "Node JS | Read from (your own) Google sheet 2022",id: "46ac73b7-33eb-4344-b51f-39dd7bede810"}; (function(d, s, id) { var js, sjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) {window.strchf.update(); return;} js = d.createElement(s); js.id = id; js.src = "https://d37oebn0w9ir6a.cloudfront.net/scripts/v0/strchf.js"; js.async = true; sjs.parentNode.insertBefore(js, sjs); }(document, 'script', 'storychief-jssdk'))