logo
Connect Node.js with Google Sheets: Read & Write Data Easily
Kashan Iqbal

Kashan Iqbal

Posted 2 months ago

πŸš€ Introduction: Why Integrate Node.js with Google Sheets?

Imagine you're building a simple CRM, attendance system, or order tracker in Node.js and want a Google Sheet to act as a database.

Good news: it's totally doable. In fact, Google Sheets + Node.js is a perfect lightweight backend combo for:

  • Logging form submissions
  • Building dashboards
  • Collecting IoT or webhook data
  • Non-technical stakeholders accessing structured data

Let’s walk through how to connect Node.js to Google Sheets and:

  • βœ… Write data to a sheet
  • βœ… Read data from a sheet

No rocket science, just APIs, credentials, and a few smart lines of code.


🧠 What You’ll Learn

  • πŸ” Setting up Google Cloud & API Credentials
  • πŸ“¦ Installing Node.js packages for Sheets
  • πŸ“€ Sending data to a Google Sheet
  • πŸ“₯ Reading values from a Sheet
  • πŸ›‘οΈ Bonus: Secure your credentials & app

🧰 Prerequisites

  • Basic knowledge of Node.js & JavaScript
  • Google account
  • Node.js installed (v14+)
  • Familiarity with async/await

πŸ”§ Step 1: Setup Google Sheets API in Google Cloud Console

  1. Visit https://console.cloud.google.com
  2. Create a new project
  3. Enable the Google Sheets API for that project
  4. Go to APIs & Services β†’ Credentials
  5. Click Create Credentials β†’ Service Account
  6. Download the JSON key file
  7. Share your Google Sheet with the service account email like:
[email protected]

βœ… Tip: Make sure your Google Sheet is created and shared before testing.


πŸ“¦ Step 2: Install Required NPM Packages

npm init -y npm install googleapis@105

πŸ—‚οΈ Step 3: Create Your Project Structure

google-sheets-node/
β”œβ”€β”€ index.js
β”œβ”€β”€ credentials.json  # (your downloaded service key)
└── .gitignore

Add credentials.json to .gitignore for security.


✍️ Step 4: Writing Data to Google Sheets

const { google } = require('googleapis'); const keys = require('./credentials.json'); const auth = new google.auth.GoogleAuth({ credentials: keys, scopes: ['https://www.googleapis.com/auth/spreadsheets'], }); const sheets = google.sheets({ version: 'v4', auth }); const spreadsheetId = 'your-sheet-id-here'; // from the URL const range = 'Sheet1!A2:D2'; // row where you want to insert const values = [['Wasif', '[email protected]', 'Pakistan', 'Developer']]; async function writeSheet() { await sheets.spreadsheets.values.update({ spreadsheetId, range, valueInputOption: 'RAW', requestBody: { values, }, }); console.log('βœ… Data written successfully!'); } writeSheet();

πŸ” Step 5: Reading Data from Google Sheets

async function readSheet() { const res = await sheets.spreadsheets.values.get({ spreadsheetId, range: 'Sheet1!A2:D10', }); const rows = res.data.values; console.log('πŸ“₯ Fetched rows:', rows); } readSheet();

πŸ” Bonus: Secure Your App

  • βœ… Don’t expose credentials.json to frontend
  • βœ… Use .env files for keys in production
  • βœ… Limit Sheet permissions to read/write as needed

βœ… Final Output

You now have a fully working Node.js script to:

  • Add data to a spreadsheet (like from a form or DB)
  • Read spreadsheet contents (to display or log)

πŸ“Š Use Cases

  • Contact form submissions
  • Expense tracking from IoT devices
  • Google Sheet as a mini CRM
  • Inventory logging from webhooks

πŸ“ Example Folder Recap

google-sheets-node/
β”œβ”€β”€ index.js
β”œβ”€β”€ credentials.json
β”œβ”€β”€ .gitignore
└── package.json

🧠 Conclusion

Google Sheets isn’t just a spreadsheet tool β€” it’s an accessible, cloud-powered database you can tap into using Node.js.

With this simple integration, you’ve unlocked a powerful way to:

  • Store form or bot data
  • Build dashboards without a backend DB
  • Provide non-developers access to structured data

Related Articles

No related blogs found.