SheetSQL is a lightweight SQL engine for Google Sheets. It lets you run familiar SQL statements directly against sheet-backed tables.
- Demo spreadsheet: https://docs.google.com/spreadsheet/ccc?key=0AlMMHFOg-bRZdHlJSlV5VXpfbElZSHY2c05iem5mR3c&usp=sharing
SheetSQL currently supports:
CREATE TABLEDROP TABLEALTER TABLEINSERT INTODELETE FROMUPDATESELECT
- Arithmetic expressions (
+,-,/) - Aggregate functions (
MAX,MIN,COUNT,AVG,SUM) DISTINCT/DISTINCT *FROMJOIN,LEFT JOIN,RIGHT JOIN,ONUNIONWHERE,AND,OR,IN,LIKE,IS NULL,IS NOT NULLGROUP BYHAVINGORDER BYLIMIT
You can run a query against the currently selected range:
- Select a range in a table.
- Use
RANGEinFROM, for example:SELECT * FROM RANGE WHERE score > 90
There are two ways to run statements:
- Open the custom menu: SQL → Show prompt.
- Use a cell formula:
=SQL(statement).
Example:
SELECT DISTINCT a+1, b, MIN(c)
FROM table1
JOIN table2 ON table1.id = table2.id
WHERE (a = 1 AND b LIKE '%dads')
OR c IN (SELECT c FROM table4)
GROUP BY a, b
HAVING AVG(c) > 5 AND MAX(c) < 10
ORDER BY a DESC
LIMIT 20
UNION
SELECT a+1, b, MIN(c)
FROM table3
GROUP BY a, b;This project is written for Google Apps Script. A modern setup uses clasp:
- Install Node.js (LTS).
- Install clasp:
npm install -g @google/clasp
- Authenticate:
clasp login
- Create or clone a script project and push sources:
clasp create --type sheets --title "SheetSQL" - Set local clasp root to
srcin your local.clasp.json(not committed):{ "scriptId": "<YOUR_SCRIPT_ID>", "rootDir": "src" } - Push sources:
clasp push
Note: keep
.clasp.jsonout of version control unless you intentionally want to share script IDs. The manifest is tracked atsrc/appsscript.jsonto keep scopes/runtime explicit for add-on publishing.
This project is designed to run as a Sheets-bound Apps Script/add-on. Recommended operational guidance:
- Use the
onOpen+onInstalltrigger pair so menus are reliably available after install/update. - Prefer
SpreadsheetApp.getUi()prompts/alerts over legacyBrowser.*dialogs for editor add-ons. - Keep writes batched (
setValues) when appending query results to reduce API calls and improve performance. - Avoid destructive actions on arbitrary active sheets; target the dedicated SQL history sheet explicitly.
- Keep OAuth scopes minimal and explicit in
appsscript.jsonwhen publishing an add-on.
- Most query execution is implemented in
src/Select.gs,src/Insert.gs,src/Update.gs,src/Delete.gs, andsrc/Table.gs. - Parser sources in
src/SQLParser.gsandsrc/SimpleSQL.gsare vendored/generated; avoid large manual edits there unless necessary.
- simpleSqlParser for parsing
INSERT,UPDATE,DELETE - sql-parser for parsing
SELECT
MIT