Offline to Online from any Software - A Bridge to Connect Both

node server to online server update concept screenshot

Building a Professional Node.js CSV → Google Sheets Sync Tool (Offline-Safe, Windows Service & EXE)

This guide documents the complete journey of building a production-grade Node.js system that synchronizes data from a local CSV file on Windows to Google Sheets — safely, reliably, and automatically.

This is not just a script. This is a local data synchronization agent, the same class of software used in billing systems, hospitals, factories, and distributed offices.

What You Will Build

  • CSV → Google Sheets row-by-row sync
  • Offline-safe queue with retry logic
  • Windows background service
  • Standalone EXE (no Node.js required)
  • Local web dashboard
  • Encrypted credentials

Table of Contents

  1. Architecture & Diagrams
  2. Data Flow Logic
  3. Project Setup
  4. CSV Processing
  5. Offline Queue System
  6. Google Sheets Integration
  7. Windows Service
  8. EXE Packaging
  9. Local Dashboard
  10. Security & Encryption
  11. FAQ

1. Architecture & Diagrams

At a high level, the system behaves like a local ETL agent.

Diagram 1: CSV to Google Sheets Sync Architecture



Node.js CSV to Google Sheets architecture diagram

CSV File (Local PC) ↓ Node.js Watcher ↓ Offline Queue (queue.json) ↓ Google Sheets API ↓ Google Sheets (Cloud)

2. Data Flow Logic

The most important rule:

Never delete a CSV row unless Google Sheets confirms success.

Diagram 2: Offline Queue & Retry Mechanism

Offline queue retry mechanism diagram


  • If internet fails → data goes to queue
  • If API fails → retry later
  • No duplicate inserts

3. Project Setup

Install Node.js

Download LTS from https://nodejs.org

Create Project


mkdir csv-to-sheets
cd csv-to-sheets
npm init -y

Install Dependencies


npm install googleapis chokidar csv-parser express crypto-js node-windows

Folder Structure


csv-to-sheets/
├── app.js
├── data.csv
├── queue.json
├── credentials.json
├── credentials.enc
├── logs/
│   └── app.log

4. CSV Processing

CSV Format


id,name,phone,address

The header is preserved permanently. Only data rows are processed.


5. Offline Queue System

queue.json


[]

Rows are written here if Google Sheets is unreachable.


6. Google Sheets Integration (Core Script)

Main Application File (app.js)


const fs = require("fs");
const csv = require("csv-parser");
const chokidar = require("chokidar");
const express = require("express");
const CryptoJS = require("crypto-js");
const { google } = require("googleapis");

const CSV_FILE = "data.csv";
const QUEUE_FILE = "queue.json";
const SHEET_ID = "YOUR_SHEET_ID";
const SHEET_NAME = "Sheet1";

function loadQueue() {
  return JSON.parse(fs.readFileSync(QUEUE_FILE));
}

function saveQueue(queue) {
  fs.writeFileSync(QUEUE_FILE, JSON.stringify(queue, null, 2));
}

This script ensures rows are removed from CSV only after confirmation.


7. Running as a Windows Service

Install Service


npm install -g node-windows

Service Installer


const Service = require("node-windows").Service;

const svc = new Service({
  name: "CSV to Google Sheets Sync",
  description: "Offline-safe CSV synchronization service",
  script: "C:\\csv-to-sheets\\app.js"
});

svc.on("install", () => svc.start());
svc.install();

The service starts automatically on system boot.


8. Packaging as Executable (EXE)

Install Packager


npm install -g pkg

Build EXE


pkg app.js --targets node18-win-x64 --output CsvSheetSync.exe

This allows deployment on machines without Node.js installed.


9. Local Web Dashboard

Dashboard Server


const app = express();

app.get("/", (req, res) => {
  const queue = JSON.parse(fs.readFileSync("queue.json"));
  const logs = fs.readFileSync("logs/app.log", "utf8")
                .split("\n").slice(-30);

  res.send(`
    <h2>CSV → Google Sheets Sync</h2>
    <p>Queue Size: ${queue.length}</p>
    <pre>${logs.join("\n")}</pre>
  `);
});

app.listen(7070);

Access at http://localhost:7070


10. Security & Encryption

Encrypt Credentials


const secret = "LOCAL_MACHINE_SECRET";

function encryptFile(input, output) {
  const data = fs.readFileSync(input, "utf8");
  const encrypted = CryptoJS.AES.encrypt(data, secret).toString();
  fs.writeFileSync(output, encrypted);
}

Credentials are decrypted only in memory and never stored in plain text.


Frequently Asked Questions

Will data be lost if internet goes down?

No. Data is safely queued and retried automatically.

Does this work without Node.js?

Yes. The EXE runs independently.

Is this production-ready?

Yes. This architecture mirrors enterprise local data agents.

Can it be extended?

Yes — multi-CSV support, licensing, cloud monitoring.


Final Thoughts

This guide demonstrates how Node.js can be used far beyond web apps — to build quiet, resilient system software.

Build it once, build it right, and let it run for years.

Comments