This package provides a Google Sheets loader for Astro. It allows you to load and parse publicly viewable Sheets, and use the data in your Astro site.
Data is fetched using the Google Visualization API (gviz).
In Google Sheets, create a document, give it a name and write some tabular data.
Recommended: Click Format > Convert to Table, choose the appropriate column type and make sure that there are no different data types within the same column.
Edit the Share settings of the document, so that "Anyone on the internet with the link can view".
From the URL bar, copy the document ID and optionally the gid (grid ID) of the sheet you want to fetch data from.
npm install astro-sheet-loaderThis package requires Astro 6 or later.
You can then use the feed loader in your content configuration:
// src/content.config.ts
import { defineCollection } from "astro:content";
import { sheetLoader } from "astro-sheet-loader";
// optionally import a transform function
import { camelCase, snake_case } from "astro-sheet-loader";
const crm = defineCollection({
loader: sheetLoader({
document: "1wb2TbwRE-McOA663PGgf0InTsXC6b07ThEy_j6_MCDw",
}),
// if you don't define a schema yourself, it will be automatically generated
// schema: z.object({
//
//})
});
export const collections = { crm };You can then use these like any other content collection in Astro:
---
import type { GetStaticPaths } from "astro";
import { getCollection, type CollectionEntry } from "astro:content";
const entries: CollectionEntry<"crm">[] = await getCollection("crm");
---
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Astro Sheet Loader</title>
</head>
<body>
<h1>Table generated from Astro Sheet Loader</h1>
<table>
<tr id="columns">
{Object.keys(entries[0].data).map((column) => (
<th>{column}</th>
))}
</tr>
{entries.map((row) => (
<tr id={row.id}>
{Object.values(row.data).map((value) => (
<td>{value}</td>
))}
</tr>
))}
</table>
</body>
</html>The sheetLoader function takes an options object with the following properties:
document(mandatory): The Google Sheet document ID, found in the URLgid(optional, default: first sheet): Number identifying the sheet, found in the URLsheet(optional, default: first sheet): The name of the sheet, found at the bottom of the pagerange(optional, default: select all): Range of cells to delimit the tablequery(optional, default: select all): SQL-like query to filter results in the provided range, using the original column names. Query documentationheaders(optional, default:1): Number of header rows. If you set it to0, Google guesses it instead, which can leave column names blank or merge data into themallowBlanks(optional, default: all fields are mandatory): Allow for blank cells, either in the whole table (true) or in specific columns (e.g.["notes", "mobile"], named aftertransformHeaderis applied)transformHeader(optional, default:false): Pass a function likecamelCaseorsnake_caseor a custom defined one to transform column namesidColumn(optional, default: positionalrow_NIDs): Use the values of this column as entry IDs, so that they stay stable when rows are inserted or deleted. The name must match the column aftertransformHeaderis applied, and its values must be unique and non-empty
For SSR sites, the sheetLiveLoader function fetches the Sheet at request time instead of build time. It takes the same options as sheetLoader, except for allowBlanks, as live collections do not validate entries against a generated schema:
// src/live.config.ts
import { defineLiveCollection } from "astro:content";
import { sheetLiveLoader } from "astro-sheet-loader";
const crm = defineLiveCollection({
loader: sheetLiveLoader({
document: "1wb2TbwRE-McOA663PGgf0InTsXC6b07ThEy_j6_MCDw",
idColumn: "Name",
}),
});
export const collections = { crm };---
import { getLiveCollection, getLiveEntry } from "astro:content";
const { entries } = await getLiveCollection("crm");
const { entry } = await getLiveEntry("crm", "Alice");
---Live collections require an on-demand rendering adapter.
The schema will be generated automatically if not user-specified when defining the collection in Astro.
However, you should be aware of the following:
- numbers, booleans and strings are imported with their original type
- currencies, percentages and monetary values are imported as raw numbers
- dates, times, datetimes and durations are imported as strings as they appear in the sheet
- checkboxes are imported as booleans
- smart chips are imported as strings
- functions are imported as their return value
- if the provided sheet or gid do not exist, Google will silently return data for the default sheet (the one with gid 0)
- if the range or query do not return any entries, the loader will throw a warning
- if Google returns a warning, e.g. when a query truncates the data, the loader will log it
- if the document is not publicly viewable or does not exist, the loader will throw an error
- if an entry does not respect the user-provided or auto-generated schema, the loader will throw an error
When it is not told how many header rows a sheet has, Google guesses it, and may return empty column names (example) or put all entries in the column names (example).
The loader avoids this by asking for one header row by default: change it with the headers option.
Google also returns unnamed, completely blank columns for formatted but empty cells (example): the loader drops them.
The project uses pnpm and Vite+, which bundles Vitest for testing:
pnpm checkchecks formatting, lint and typespnpm testbuilds the package and runs the offline Vitest suite against recorded fixturespnpm test:onlinealso builds the demo and runs the tests hitting the live Google Sheets API
Kudos to Matt Kane for his collection of Astro Loaders


