To ingest data from a public Google Sheet, you first need to enable sharing on the file. Open the sheet in Google Sheets, click Share → Change to anyone with the link, and set the role to Viewer.
The share link will look like this:
https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms/edit?usp=sharing
Google Sheets supports a direct CSV export URL. Extract the spreadsheet ID from the link and use the following export format:
Typically, a spreadsheet URL will contain an access token. It is advised not to hard-code the ID into the string but rather use the secrets store to inject the ID during the build.
Single Sheet Extraction (Private Sheets via Service Account)
If your Google Sheet is private, you will need to authenticate using a Google Service Account.
import polars as pl
from google.oauth2 import service_account
from googleapiclient.discovery import build
import io
# Spreadsheet ID and target sheet/tab name
SPREADSHEET_ID = "<REPLACE_WITH_SPREADSHEET_ID>"
SHEET_NAME = "Sheet1"
# Path to your service account credentials
SERVICE_ACCOUNT_FILE = "./mcp.json"
# Sheets API scope
SCOPES = ["https://www.googleapis.com/auth/spreadsheets.readonly"]
def transform():
# Authenticate using the service account
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES
)
service = build("sheets", "v4", credentials=creds)
# Read all values from the target sheet
result = (
service.spreadsheets()
.values()
.get(spreadsheetId=SPREADSHEET_ID, range=SHEET_NAME)
.execute()
)
rows = result.get("values", [])
# First row is treated as the header
headers = rows[0]
data = rows[1:]
df = pl.DataFrame(data, schema=headers, orient="row")
return df