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!

  1. Create a sheet
  2. Add some data
  3. Share the sheet publicly
  4. Set up your Node project (I am using Cloud Functions)
  5. Get authenticated

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).

Let’s get into the 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'))

More Stories, Tips and tricks
Solving the Gradle Error: 'Failed to calculate the value of task' in Flutter
Stuck with a persistent Gradle error in your Flutter project? Discover a quick fix for the 'Failed to calculate the value of task' error and get your project back on track.
Read more
Google Play Developer: Target SDK API Level 33 Deadline
Unlock Success: Google Play Developer Deadline Secrets Revealed! Don't miss out on crucial insights
Read more
Telegram: Get a channel Id from Telegram
Working with a Telegram bot is awesome. Here are some pointers.
Read more