Scroll to Top
💻
Free Code
Users get ready-to-use code at no cost.
📋
Easy Copy
Copy and use the code instantly.
Quick Learning
Understand concepts fast and clearly.
📝
Step-by-Step
Follow simple instructions to implement.
📅 August 30, 2024 💻 Tutorial ⭐ Beginner Friendly

Convert Your – Google Sheet into API in 2 Minutes

Author Avatar

Ashish Dwivedi

Editorial Team • Tech Writer

About This Tutorial

Turn your Google Sheet into an API in 2 minutes using Google Apps Script. Create a simple script to fetch sheet data, publish it as a web app, and access it via a URL. Ideal for dashboards, apps, or integrations.
  1. Step 1: Click on the Copy button to copy the code snippet.
  2. Step 2: Paste the copied code into your project’s script editor.

Apps Scripts Blog

Read Blog

📂 javascript
⚡ script2.js
⚡ script2.js
function doGet(req) {
  // ✅ Get active spreadsheet and target sheet
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = doc.getSheetByName('sheet1'); // ensure sheet name is exact
  var values = sheet.getDataRange().getValues();

  var output = [];

  // ✅ Loop through each row (skip header)
  for (var i = 1; i < values.length; i++) {
    var rowObject = {};

    // ✅ Loop through columns until empty cell or 100 columns
    for (var j = 0; j < 100; j++) {
      if (!values[0][j]) break;           // stop if header cell is empty
      if (typeof values[i][j] === 'undefined') break;

      rowObject[values[0][j]] = values[i][j];
    }

    output.push(rowObject);
  }

  // ✅ Return JSON response
  return ContentService
    .createTextOutput(JSON.stringify({ data: output }))
    .setMimeType(ContentService.MimeType.JSON);
}