Query workbook rows with the API
What it does
Section titled “What it does”POST /sheets/:sheetId/query filters rows by conditions, sorts matches and returns selected columns. The key needs Read access to a visible sheet. Send the request from a server.
How to use it
Section titled “How to use it”curl -X POST "$BASE/sheets/$SHEET/query" -H "Authorization: Bearer $KEY" \ -H 'Content-Type: application/json' --data-binary '{ "columns": ["Product", "Price"], "where": [ {"column":"Price","op":"between","type":"number","values":[100,500]}, {"column":"Status","op":"eq","value":"On sale"} ], "sort":[{"column":"Price","direction":"desc","type":"number"}], "limit":100 }'headerRow selects the header row; it defaults to 1. Pass up to 64 header names in columns, in the order you want them returned. If you omit columns, the response includes every header column. Names are matched without regard to letter case or leading and trailing spaces. A row is included even when its selected columns are empty.
All where conditions must match. You can use up to 10 conditions and up to three sort keys. Rows with equal sort values keep their original order. Empty cells, formula errors and values of the wrong type sort last in either direction.
| Condition | Fields |
|---|---|
eq, ne, gt, gte, lt, lte |
One value |
contains, startsWith |
A string value |
in |
Up to 50 items in values |
between |
Two bounds of the same type in values, lower bound first; both included |
isEmpty, notEmpty |
No value; a formula error counts as non-empty |
By default, value distinguishes numbers, strings and booleans. In ascending order, numbers come first, then strings, then booleans. number compares numeric cells and JSON numbers. text uses the displayed text, or the scalar value as text when there is no separate display. Text comparisons are case-sensitive.
The date type accepts YYYY-MM-DD conditions. Cells can contain the same date format or a spreadsheet serial date. Comparisons and sorting use whole days; the fractional time part is ignored.
The response contains revision, sheet, header, rows and next. row is the original sheet row number; offset and next count sorted matches after filtering. For the next page, pass next as offset and the first page’s revision. When next is null, there are no more pages.
Limitations
Section titled “Limitations”- The page size defaults to 1,000 rows and can be up to 5,000. An empty page is possible when
offsetis beyond the number of matches. - The query uses data within the header width. Displayed values, formula errors, links and photos are considered even if their columns are not in the projection.
- A request is limited to a 64 KiB body, 250,000 stored cells, 32 MiB of scan input and an 8 MiB response. For a workbook stored as one document, the initial check counts the whole snapshot and journal.
- A single-document workbook also has a shared conservative budget of 250,000 calculation work units, including formula dependencies and array creation.
- Each key can send up to 30 queries per minute. A server process admits up to four simultaneous queries. Upload each request within 10 seconds.
If something goes wrong
Section titled “If something goes wrong”| Response | What to do |
|---|---|
400 invalid_request |
Check condition types, dates and column names. |
403 forbidden, 404 not_found |
Use a key with Read access to a visible sheet listed by /workbook. |
409 revision_conflict |
The workbook changed. Restart from the first page. |
422 unknown_column, 422 ambiguous_column |
Check the column name and header row. |
422 query_volatile |
A formula in a single-document workbook can change results without a revision change. Use the regular GET rows request. |
413 too_large |
Reduce the selected columns or page size if the response is too large. If scanning exceeds its limit, read separate ranges; if the calculation budget is exceeded, narrow ranges used by formulas. |
429 rate_limited, 503 unavailable |
Retry after the Retry-After delay. |
