π 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
- Visit https://console.cloud.google.com
- Create a new project
- Enable the Google Sheets API for that project
- Go to APIs & Services β Credentials
- Click Create Credentials β Service Account
- Download the JSON key file
- 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