Turn Google Sheets into a NoSQL Database (Step-by-Step)

This approach converts a Google Sheet into a lightweight NoSQL-style database using Google Apps Script and exposes it as a Web API.

No servers.
No database setup.
Just a Google Sheet acting like a backend.


Step 1: Create a Google Sheet

  1. Open Google Sheets
  2. Create a new blank sheet
  3. Give it a meaningful name (example: NoSQL_DB)

This sheet will act as your database container.


Step 2: Open Apps Script

  1. Click Extensions → Apps Script
  2. A new tab opens with a file named Code.gs
  3. Delete existing content
  4. Paste the following core backend code

Step 3: Core NoSQL Engine (Code.gs)

Paste everything below into Code.gs and save.

/***********************
 * CORE HELPERS
 ***********************/
function getOrCreateSheet(sheetName) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getSheetByName(sheetName);

  if (!sheet) {
    sheet = ss.insertSheet(sheetName);
    sheet.appendRow(["_id", "_createdAt"]);
  }
  return sheet;
}

function ensureColumns(sheet, data) {
  const headers = sheet
    .getRange(1, 1, 1, sheet.getLastColumn())
    .getValues()[0];

  let changed = false;

  Object.keys(data).forEach(k => {
    if (!headers.includes(k)) {
      headers.push(k);
      changed = true;
    }
  });

  if (changed) {
    sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
  }

  return headers;
}

/***********************
 * INSERT
 ***********************/
function insertDoc(sheetName, data) {
  const sheet = getOrCreateSheet(sheetName);
  const headers = ensureColumns(sheet, data);
  const row = new Array(headers.length).fill("");

  const id = Utilities.getUuid();

  headers.forEach((h, i) => {
    if (h === "_id") row[i] = id;
    else if (h === "_createdAt") row[i] = new Date();
    else if (data[h] !== undefined) row[i] = data[h];
  });

  sheet.appendRow(row);
  return { success: true, _id: id };
}

/***********************
 * FIND
 ***********************/
function findDocs(sheetName, query) {
  const sheet = getOrCreateSheet(sheetName);
  const data = sheet.getDataRange().getValues();
  const headers = data.shift();

  return data
    .map(row => {
      let obj = {};
      headers.forEach((h, i) => (obj[h] = row[i]));
      return obj;
    })
    .filter(obj =>
      Object.keys(query).every(k => obj[k] == query[k])
    );
}

/***********************
 * WEB APP ENTRY
 ***********************/
function doPost(e) {
  try {
    const body = JSON.parse(e.postData.contents);
    const sheet = body.collection || "default";
    const data = body.data || {};
    const res = insertDoc(sheet, data);
    return jsonResponse(res);
  } catch (err) {
    return jsonResponse({ error: err.toString() });
  }
}

function doGet(e) {
  try {
    const sheet = e.parameter.collection || "default";
    let query = { ...e.parameter };
    delete query.collection;

    const res = findDocs(sheet, query);
    return jsonResponse({ count: res.length, data: res });
  } catch (err) {
    return jsonResponse({ error: err.toString() });
  }
}

/***********************
 * RESPONSE HELPER
 ***********************/
function jsonResponse(obj) {
  return ContentService
    .createTextOutput(JSON.stringify(obj))
    .setMimeType(ContentService.MimeType.JSON);
}

Step 4: Deploy as Web App (Your DB Endpoint)

  1. Click Deploy → New deployment
  2. Select Web app
  3. Set:
    • Execute as: Me
    • Who has access: Anyone
  4. Click Deploy
  5. Authorize permissions

🎯 You will receive a URL
This is your connection string / database endpoint.

Example:

https://script.google.com/macros/s/AKfycbxXXXX/exec

API Design (How It Works)

ActionMethodDescription
InsertPOSTAdds a document
FindGETFetch documents
CollectionSheet nameActs like a collection
DocumentRowActs like JSON

Usage Example – PHP Backend (cURL)

Insert Document

<?php

$url = "YOUR_WEB_APP_URL";

$data = [
  "collection" => "users",
  "data" => [
    "name" => "Anil",
    "role" => "student",
    "status" => "active"
  ]
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);
curl_close($ch);

echo $response;

Find Documents

<?php

$query = http_build_query([
  "collection" => "users",
  "status" => "active"
]);

$url = "YOUR_WEB_APP_URL?" . $query;
$response = file_get_contents($url);

echo $response;

Usage Example – Node.js Backend (Axios)

Install Axios

npm install axios

Insert Document

const axios = require("axios");

const URL = "YOUR_WEB_APP_URL";

axios.post(URL, {
  collection: "users",
  data: {
    name: "Anil",
    role: "student",
    status: "active"
  }
})
.then(res => console.log(res.data))
.catch(err => console.error(err));

Find Documents

const axios = require("axios");

const URL = "YOUR_WEB_APP_URL";

axios.get(URL, {
  params: {
    collection: "users",
    status: "active"
  }
})
.then(res => console.log(res.data))
.catch(err => console.error(err));

What You Just Built

✔ Google Sheets as a NoSQL DB
✔ Auto schema expansion
✔ JSON-based documents
✔ REST API backend
✔ Zero infra cost

This setup is perfect for students, internal tools, prototypes, and automation systems

Leave a Reply

Your email address will not be published. Required fields are marked *