Google Sheets helpers
Read and write Google Sheets from Python, and choose between the row and range helpers.
The Google Sheets helpers read and write spreadsheets your organization has connected. They let a sheet your team already works in become part of an automation. For example, an action can add each return request as a row, and a recurring job can pick up the rows your team has approved. Use the row helpers for tables with a header row, and the range helpers for every other layout.
This page covers setup, the two read models, limits, caching and safe recovery. Each helper's own page has its signature and examples.
Connect the spreadsheet
- Share the workbook with Octocom's Google account,
octocom-google-sheets@octocom.iam.gserviceaccount.com: Viewer access for reads, Editor access for writes. - Connect it to your organization in Integrations -> Google Sheets. Connecting takes a minute: you confirm a 4-digit code Octocom writes to a temporary tab.
Python helpers authenticate with the current execution's own credential, so your code holds no key. REST requests require X-API-Key. Never put an Octocom API key in browser JavaScript.
A helper call on a spreadsheet your organization has not connected can raise
GoogleSheetNotConnectedError (a requests.HTTPError with status 403 and code
google_resource_not_authorized). Its message and .connect_url link straight
to the Google Sheets window with the spreadsheet ID filled in; .reason is
missing-grant, or write-not-granted for a spreadsheet connected read only.
Connect it there and run the call again.
try:
add_google_sheets_row("SHEET_ID", {"Order": "#1001"})
except GoogleSheetNotConnectedError as error:
print("Connect the spreadsheet first:", error.connect_url)
raiseThe Octocom verification tab holds a pending code. Helpers and REST calls refuse to read or change a tab with that name.
Choose the read model
Use list_google_sheets_rows for tables with unique column names in row 1.
Use read_google_sheets_range for banners, duplicate labels, merged cells,
section markers and repeating row groups. Range reads never guess headers or
rename columns.
Access
Every helper authenticates with Octocom's Google account. There is no anonymous fallback.
An authenticated read can also succeed on a publicly shared workbook. Success therefore does not prove that the account was explicitly added. Check the workbook's Share dialog and add the account before disabling link-sharing. Google may report an inaccessible workbook as 404; that cannot always be separated from a nonexistent spreadsheet. Missing-tab errors list available tabs.
Discover tabs
tabs = list_google_sheets_tabs("SHEET_ID")["tabs"]
visible_tabs = [tab for tab in tabs if not tab["hidden"]]Each tab returns sheetId, title, index, hidden, rowCount and
columnCount. Dimensions are allocated grid size, not the last populated cell.
Reads are fresh; select the intended tab explicitly when several are visible.
Read by position
page = read_google_sheets_range("SHEET_ID", "B2:D100", sheet_name="October")
for offset, row in enumerate(page["rows"]):
sheet_row = page["startRow"] + offset
label = row[0]["displayText"] # column B
urls = row[1]["hyperlinks"] # column C
print(sheet_row, label, urls)The response has sheetId, sheetName, range, one-based startRow and
startColumn, rectangular rows, rowMetadata, columnMetadata and merges. Each cell has displayText
and hyperlinks, plus formula and note when present. Duplicate URLs in a
cell are removed. Cell notes are included; threaded comments, images and smart
chip metadata are not returned.
Blank cells, including trailing blanks, keep their positions. Merged values
stay in the top-left cell; other cells are not filled automatically. merges
contains intersecting Google grid ranges with zero-based, end-exclusive indices.
A merge beginning outside the requested rectangle may have its value outside it.
Hidden rows and columns are included by default. Pass include_hidden_rows=False
or include_hidden_columns=False to leave them out; see
read_google_sheets_range.
Supply a tab-local cell or bounded rectangle, such as A1 or A1:D100.
Whole columns, whole rows, named ranges and Tab!A1 are rejected. The limit is
10,000 cells per call, within the tab's current dimensions. For large sheets,
read successive row windows; there is no cursor or automatic whole-tab download.
Each window is a fresh read, not a snapshot across calls.
Write or correct a report
write_google_sheets_range(
"SHEET_ID", "A1:C2",
[["Report", "Date", "Public or DM?"], ["Weekly", "2026-10-13", "DM"]],
sheet_name="Reports",
)The values matrix must match the entire bounded rectangle. Strings, numbers
and booleans are accepted; write request bodies are limited to 2 MB; use "" to clear a cell. The default
value_input_option="RAW" stores literal values, including strings starting
with =. Pass value_input_option="USER_ENTERED" to interpret formulas and dates.
Writes do not insert rows or resize tabs. They overwrite the selected values;
use a dedicated report area and verify it before writing.
clear_google_sheets_range("SHEET_ID", "A2:C100", sheet_name="Reports")Clearing removes values and formulas while preserving formatting, notes, validation and row positions. It is not row deletion. Clearing and writing are separate operations, not a transaction. To replace a report in one operation, write its full rectangle, padding leftover rows with empty strings.
Append without replacing headers
add_google_sheets_row(
"SHEET_ID", {"Report": "Weekly", "Public or DM?": "DM"},
sheet_name="Reports", preserve_headers=True,
)The opt-in retains existing columns and appends missing headers on the right.
Empty sheets get their initial headers. Existing headers must be unique; the
operation reads the header before writing and is not atomic with concurrent
schema edits. While cell A1 is blank the call fails with a 400 and writes
nothing, because Google would add the row above the titles; put the title back
in A1 and retry. Without this option, the append replaces the row-1 headers
with the keys of the supplied dictionary, so use the option on shared tables.
Paging, caching and failures
Structured row reads return at most 100 rows; start with 50, or 100 for bulk
processing. Filtered calls scan at most 5,000 rows and continue from their scan
position. Follow nextCursor even when a filtered page is empty. Row numbers
and cursors can become stale when people sort or insert rows.
Structured reads are cached for up to 60 seconds. Writes through these helpers invalidate cached rows across backend processes; edits made directly in Google may take up to 60 seconds to appear. Range and tab reads bypass result caching.
Duplicate/blank headers return a 400 explaining the layout problem. On an empty first page, a bounded probe checks for data outside the interpreted header columns and reports a 400 when detected. This probe is not full-sheet layout validation: use positional ranges for irregular sheets, and validate expected record counts before publishing a sync.
Helpers use a 30-second HTTP timeout. Safe reads retry Google 429 and transient
5xx responses twice with short delays. Writes only retry an explicit 429 refusal;
they do not automatically retry ambiguous failures. Exhausted quota responses
use HTTP 429 with Retry-After: 60; upstream 5xx responses use HTTP 503.
A requests.HTTPError includes the backend's explanation and retains .response.
A write timeout or transient failure does not prove whether the write landed.
For appends, store a unique operation/reference ID in the row, search for it
before retrying, and repeat verification after a short interval if necessary.
Verification reduces duplicate risk but is not exactly-once delivery. Range
writes also require verification when their outcome is unknown; concurrent edits
can make a blind retry overwrite someone else's change.
REST endpoints
All require the organization API key in X-API-Key.
| Operation | Endpoint | Body |
|---|---|---|
| List tabs | POST /rest/v1/sheets/tabs | spreadsheetId |
| Read range | POST /rest/v1/sheets/range/read | spreadsheetId, range, optional sheetName, includeHiddenRows, includeHiddenColumns |
| Write range | PUT /rest/v1/sheets/range | Above plus values, optional valueInputOption |
| Clear range | POST /rest/v1/sheets/range/clear | spreadsheetId, range, optional sheetName |
Row operations have REST endpoints too, such as POST /rest/v1/sheets/rows/search.
The append endpoint accepts preserveHeaders: true.