Sources
Google Sheets
Load a spreadsheet's tabs or ranges as tables, with typed dates, numbers and booleans, read with a service-account key or as a signed-in Google user.
A Google Sheets source loads tabs or ranges of one spreadsheet, one table each. Values arrive typed as the sheet holds them, not as the text it shows.
[connections.sheets]kind = "google_sheets"service_account_json = "${ENV:GOOGLE_SA_JSON}"
[sources.reference]kind = "google_sheets"connection = "sheets"description = "Reference tables kept in a spreadsheet."spreadsheet_id = "${ENV:REFERENCE_SHEET_ID}"ranges = { locations = "Locations!A:F", codes = "Codes" }write_strategy = "replace"Share the spreadsheet with the service account’s email as a Viewer. SQLLoad asks only for read-only access to spreadsheets.
Connection
Section titled “Connection”| Setting | Meaning |
|---|---|
service_account_json |
The service-account key file’s JSON text, normally ${ENV:...}. |
service_account_json_base64 |
The same, base64-encoded, for secret stores that dislike newlines and quotes. |
service_account_file |
A path to the key file. |
client_id, client_secret |
An OAuth client, to read as a Google user. See Sign in as a Google user. |
refresh_token |
The Google user’s refresh token, for unattended runs. |
Set at most one of the three key settings and client_id. With none, SQLLoad uses the credentials
file GOOGLE_APPLICATION_CREDENTIALS names (see
BigQuery’s authentication). Workload identity federation
must impersonate a service account, and a gcloud user login needs the Sheets scope:
gcloud auth application-default login --scopes=https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/spreadsheets.readonly.
sqlload debug shows who reads each source and how many tabs it sees.
Sign in as a Google user
Section titled “Sign in as a Google user”When a spreadsheet can’t be shared with a service account, SQLLoad can read it as a Google user who consents once in the browser. This is tested against a simulated Google endpoint, not live Google.
-
In the Google Cloud console, enable the Google Sheets API and create an OAuth client of type Desktop app (APIs & Services → Credentials). Keep its
client_idandclient_secretout of the repository. -
Set them on the connection:
[connections.sheets]kind = "google_sheets"client_id = "${ENV:GOOGLE_OAUTH_CLIENT_ID}"client_secret = "${ENV:GOOGLE_OAUTH_CLIENT_SECRET}" -
Run
sqlload planfrom a terminal and consent. Without a browser, it prints the address: open it elsewhere, then paste the address the browser ends on. The refresh token is cached, readable only by you, insqlload/google-tokens.jsonbelow your cache folder (XDG_CACHE_HOMEor~/.cache, or%LOCALAPPDATA%on Windows), one entry per"<client_id> <connection>".
For scheduled runs elsewhere, copy the entry’s refresh_token into a secret and set
refresh_token = "${ENV:GOOGLE_REFRESH_TOKEN}". To sign in as another account, delete the entry
(or revoke access at myaccount.google.com/permissions)
and run sqlload plan again.
A run that needs a sign-in but has no terminal, or runs with --json, fails at once. Google
refuses a refresh token that was revoked, after a password change, or after 7 days for a client
whose consent screen is in testing: a refused refresh_token fails the table (sign in again with it
unset), and a refused cached token asks for consent again on a terminal. These exit 1. A token
endpoint that stays down exits 75.
Source
Section titled “Source”| Setting | Meaning |
|---|---|
spreadsheet_id |
The ID in the spreadsheet’s URL, between /d/ and /edit. |
ranges |
Tables by name: a tab’s name, or an A1 range such as "Locations!A:F" or "'Q4 Report'!B3:H". Without it, every tab loads as a table named after it (Q4 Report as q4_report); empty tabs are left out with a warning. |
header |
false when a range has no header row; its columns are then column_1, column_2, …. |
header_row |
The range’s row holding the header, counting from 1, when title rows sit above it. |
column_names |
With header = false, one name per column of the range (A:C is three), as for headerless files. Another count fails plan. |
header, header_row and column_names work on the source or a table. Table settings go under
[sources.<name>.tables.<table>], by the name in ranges or the tab’s table name. With more than
one table, a table_name template must contain {table}. Files settings such as format are
refused. A constant_columns name that a sheet column also
has, ignoring case, fails plan.
Columns and types
Section titled “Columns and types”- The header row names the columns, under
namingandon_duplicate_names. A blank header cell between named ones failsplan, naming it (Locations!C1); a merged header cell names only its first column. Cells right of the last named header are left out with a warning. - Rows empty in the header’s columns are skipped; a short row is padded with NULLs.
_sqlload_source_rowholds each row’s sheet row number, so a skipped row leaves a gap. Use it to fill a block’s first-row value down in SQL:last_value(metric_id ignore nulls) over (order by _sqlload_source_row). Leave it out withexclude_columns. A header of that name failsplan; a table with atransformdoesn’t get it.
| Cells | Load as |
|---|---|
| TRUE/FALSE | bool |
| Whole numbers in a plain format | int |
| Other numbers, and currency, percentages (as a fraction) and scientific always | float |
| Dates, dates with times, times | date, timestamp_ntz (the sheet’s wall-clock time), time |
Text, including a number typed as text ('00123) |
text |
| A mix of kinds | text as the sheet shows it, with a plan warning |
Empty text, such as IFERROR(K9/K8, "") |
NULL, not typing the column |
A formula error (#DIV/0!, #N/A) |
Fails the load, naming the cell |
- Pin a type in
columnsto load a column as another; a cell that doesn’t convert fails the load.infer_types = falseloads every unpinned column as shown text; see Loading every column as text.parsereads text as times, or wall-clock times as instants in a zone; see Parsing text into times. - A column keeps its table’s type when its values fit, such as whole numbers in a
floatcolumn. A column first loaded empty, as on a header-only tab, istext: pin a type, orload --reloadonce rows arrive. A whole-number column that later gets a fraction is refused atplan, so pinfloatwhere fractions may come.
Transforming each range
Section titled “Transforming each range”A table can take a transform, as a files table
does: an @sl.transform function on python_paths, called as f(rows, ctx) with the range’s rows
as a pyarrow.Table, at plan and at each load. It returns the rows to load as the files page
describes. ctx.unit is "range", and ctx has spreadsheet_id, sheet (the tab), range (as
in ranges) and table.
A report with title rows above its header, a KPI per block and months across, becomes tidy rows:
python_paths = ["transforms"]
[sources.kpis]kind = "google_sheets"connection = "sheets"spreadsheet_id = "1AbC..."ranges = { metrics = "KPIs" }header_row = 2description = "Service KPIs, one row per KPI or measure and month."write_strategy = "replace"transform = "kpi_sheet"import pandas as pdimport pyarrow as paimport sqlload as sl
MONTHS = ["Period 1", "Period 2", "Jan", "Feb", "Mar"]
@sl.transformdef kpi_sheet(rows: pa.Table, ctx: sl.TransformContext): df = rows.to_pandas() kpi = df["ID"].notna() df["metric_id"] = df["ID"].ffill() df["metric"] = df["KPI"].where(kpi).ffill() df["label"] = df["KPI"] df["row_kind"] = kpi.map({True: "kpi", False: "measure"}) df = df[df["metric_id"].notna()] tidy = df.melt( id_vars=["metric_id", "metric", "label", "row_kind"], value_vars=MONTHS, var_name="month", value_name="value", ) tidy["value"] = pd.to_numeric(tidy["value"], errors="coerce") return tidy.dropna(subset=["value"]).reset_index(drop=True)- Title rows above
header_row, or above a range starting at the header ("KPIs!A2:G"), never reach the function. Every column still needs a header cell, or useheader = falsewithcolumn_names. - A merged cell’s value is in its first cell only; fill it down, as
ffill()does. - A number in a
textcolumn arrives as its value,0.9, not as shown,90.0%. - A function that raises or returns something else fails the table, naming the function and the range; the table stays as it was.
- Under
mergeorappend, when the function’s file changes,planshowstransform changed: 1 range loaded under an earlier version (--reload transforms every range again), as for files.
Writing
Section titled “Writing”Every table needs a write_strategy, usually replace, since a sheet has no column to read after.
merge on named unique_key columns (a sheet has no primary key) and append (a snapshot each
run) also work. cursor_column, delete_insert and batch_size are refused at plan, as is a
merge whose key repeats in the sheet, naming both rows. A failed read leaves the table as it was.
load reads each table’s range in one request and holds it in memory, which suits reference sheets
up to a few hundred thousand cells. Reads that hit Sheets’ per-minute quota are retried for about a
minute and a half.