メインコンテンツにスキップ

gsSQL

Use real SQL SELECT syntax to filter your Google Sheets data using a simple custom function.

AI Summary

gsSQL

A custom function / GAS library that lets you query Google Sheets data with standard SQL SELECT syntax

Target Users

Intermediate to advanced Google Apps Script or Sheets users who need concise SQL-style joins and aggregations

Problems Solved

The built-in Google Sheets QUERY function is hard to write for joins etc. and unusable directly inside Apps Script.

Tags

Main Features

1
Standard SQL syntax support

Lets you use familiar SELECT, JOIN, GROUP BY, PIVOT and more, eliminating the need to learn QUERY’s syntax.

2
Usable as sheet function and in GAS

Call gsSQL() directly in a cell or run the same query from Apps Script through the Sql class.

3
Bind variables & caching

Use ?1 style bind parameters for dynamic values and set per-table cache seconds to balance speed and freshness.

Usage Examples

Run an INNER JOIN from Apps Script

function sampleGsSQL() {
  const stmt = 'SELECT books.id, books.title, authors.last_name ' +
               'FROM books INNER JOIN authors ' +
               'ON books.author_id = authors.id ' +
               'ORDER BY books.id';

  // Fetch data from the active spreadsheet
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const booksData   = ss.getSheetByName('Books').getDataRange().getValues();
  const authorsData = ss.getSheetByName('Authors').getDataRange().getValues();

  // Execute the query with the Sql class
  const result = new Sql()
      .addTableData('books',   booksData)   // define table "books"
      .addTableData('authors', authorsData) // define table "authors"
      .enableColumnTitle(true)              // include header row
      .execute(stmt);                       // run SQL

  Logger.log(result); // [[books.id,books.title,authors.last_name], [1,'Time to Grow Up!','Writer'], ...]
}

Add this to your Apps Script project and run sampleGsSQL(); the joined data will appear in the execution log.