Google Sheets loader plugin for Datamog. Populates extensional predicate tables from Google Sheets using the google-spreadsheet library.
Given a Datamog program that declares an extensional predicate:
extensional parent(name: string, child: string).Point it at a Google Sheet with --extensional:
# Public sheet (API key)
GOOGLE_API_KEY=... bun run datamog --extensional parent=https://docs.google.com/spreadsheets/d/SPREADSHEET_ID program.dl
# Private sheet (service account)
GOOGLE_SERVICE_ACCOUNT_EMAIL=... GOOGLE_PRIVATE_KEY=... bun run datamog --extensional parent=https://docs.google.com/spreadsheets/d/SPREADSHEET_ID program.dlThe first row of the sheet must contain headers that match the declared column names (e.g. name, child).
Two methods are supported. If both are set, service account takes precedence.
For spreadsheets shared as "Anyone with the link can view".
- Create a Google Cloud project and enable the Google Sheets API.
- Create an API key under APIs & Services > Credentials.
- Set the
GOOGLE_API_KEYenvironment variable.
export GOOGLE_API_KEY=AIza...For spreadsheets that are not publicly shared. This is the recommended approach for production use.
- Create a Google Cloud project and enable the Google Sheets API.
- Create a service account under IAM & Admin > Service Accounts.
- Generate a JSON key for the service account and note the
client_emailandprivate_keyfields. - Share the spreadsheet with the service account's email address (as Viewer or Editor).
- Set the environment variables:
export GOOGLE_SERVICE_ACCOUNT_EMAIL=my-service@my-project.iam.gserviceaccount.com
export GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"Each predicate is mapped to a spreadsheet via the sheets option:
{
spreadsheetId: "...", // Google Sheets spreadsheet ID (required)
range: "Sheet1", // Sheet/tab name (default: "Sheet1")
}When using the CLI with --extensional, the spreadsheet ID is extracted from the Google Sheets URL automatically. To select a specific tab, there is currently no CLI flag — the default tab Sheet1 is used.
import { DatamogExecutor } from "datamog-engine";
import { GSheetLoader } from "datamog-gsheet";
const loader = new GSheetLoader({
auth: { apiKey: process.env.GOOGLE_API_KEY! },
// or: auth: { serviceAccountEmail: "...", privateKey: "..." },
sheets: {
parent: { spreadsheetId: "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms" },
edge: { spreadsheetId: "1abc...", range: "Edges" },
},
});
const executor = new DatamogExecutor(backend, [loader]);