How to generate images from a Google Sheet or an Airtable table
Name the columns of your sheet after the layer keys of a Snapsbrew template, then export the sheet as CSV. Upload the CSV on the template page, and Snapsbrew renders one image or PDF for each row. For a repeat job, a script reads the CSV, sends batches of 100 rows to the API, and writes the image URLs back.
By Alejandro Medina, maintainer of Snapsbrew. Published . Last updated .
Most bulk image jobs start in a spreadsheet. A list of event speakers, a product feed, the names on 300 course certificates, a table of real estate listings. Someone keeps the list in Google Sheets or Airtable, and someone else makes the images by hand in a design tool, one at a time.
This guide connects the two. Each row of the sheet becomes one image or one PDF, rendered from one Snapsbrew template. Part 1 needs no code. Part 2 is a script for a job that runs every week.
Before you start
You need a Snapsbrew instance and a published template. Each part of the design that changes from row to row is a layer with a key, for example name, role, photo and accent. Read Layers and variants.
Then make the header row of your sheet match the layer keys exactly. A sheet for speaker cards looks like this:
| name | role | photo | accent |
|---|---|---|---|
| Ada Okafor | Staff engineer, Lumen | https://example.com/ada.jpg | #ff5a36 |
| Rui Tanaka | CTO, Fieldnote | https://example.com/rui.jpg |
The empty accent cell keeps the color of the template.
Part 1: Upload a CSV, no code
Get the exact columns
- Open the template on your instance.
- In the Imports panel, click Start an import.
- Click Download Excel template.
The file has one column for each layer key in row 1, and the current value of each layer in row 2. Copy row 1 into your sheet, or paste your data under it.
Export the sheet
- Google Sheets: open the tab, then click File > Download > Comma-separated values (.csv). A CSV file holds one tab only. Read the Google help page about downloads.
- Airtable: open the view, click the arrow next to the view name, then click Download CSV. The filters of the view limit the rows of the file. Hide the fields that are not layer keys first. Read the Airtable help page about views.
Import
- On the New Snaps Import page of the template, type a title.
- If another system needs to know when the import ends, type a webhook URL.
- Upload the file.
The instance queues one snap for each row and shows the progress. The rules for the file are strict on purpose:
- A column that is not a layer key stops the import, with an error that names the column.
- A layer key with no column also stops the import.
- The maximum file size is 10 MB.
Which cell changes what
The instance picks the change from the type of the layer:
| The layer has | The cell becomes |
|---|---|
| Text | The new text |
| A QR code | The content of the QR code |
| A barcode | The value of the barcode |
| A background image | The image URL |
| A background color | The color |
An image cell must hold a public http or https URL. A file name such as ada.jpg does not work. If your photos are attachments in Airtable or files in Google Drive, put them at a public URL first, for example in a storage bucket.
Part 2: A script for a repeat job
The upload is fine for one list. For a list that changes every week, a script saves the export and the upload. It reads the sheet as CSV, sends the rows to the batch API, waits, and writes the image URL of each row to a file.
Get a CSV URL of the sheet
In Google Sheets, File > Share > Publish to web publishes one tab at a fixed URL, and you can choose the format of the file. Choose CSV. Everyone with the URL can read the tab, so use it only for data that is not private. Read the Google help page about publishing. For private data, download the CSV with the Google Sheets API or the Airtable API and a key, and pass the file to the script.
The script
Install the JavaScript SDK and a CSV parser:
npm install github:amedina068/snapsbrew-js csv-parse
// sheet-to-images.mjs
import { writeFileSync } from 'node:fs'
import { parse } from 'csv-parse/sync'
import { Snapsbrew } from 'snapsbrew'
const sb = new Snapsbrew({ baseUrl: process.env.SNAPSBREW_URL, token: process.env.SNAPSBREW_TOKEN })
const templateId = process.env.TEMPLATE_ID
// Which variant property each column sets. Columns that are not here are not sent.
const columns = { name: 'text', role: 'text', photo: 'image_url', accent: 'background_color' }
const csv = await (await fetch(process.env.SHEET_CSV_URL)).text()
const rows = parse(csv, { columns: true, skip_empty_lines: true })
const lines = ['row,status,media_url']
for (let start = 0; start < rows.length; start += 100) {
const chunk = rows.slice(start, start + 100)
const batch = await sb.batches.create({
templateId,
title: `Sheet import, rows ${start + 2} to ${start + chunk.length + 1}`,
items: chunk.map((row, i) => ({
identifier: `row-${start + i + 2}`,
variants: Object.entries(columns)
.filter(([key]) => row[key])
.map(([key, property]) => ({ layer: key, [property]: row[key] })),
})),
})
const done = await sb.batches.wait(batch.uuid, { signal: AbortSignal.timeout(600_000) })
for (const item of done.items) lines.push(`${item.identifier},${item.status},${item.media_url ?? ''}`)
console.log(`Rows ${start + 2}+: ${done.status}, ${done.failed} failed`)
}
writeFileSync('images.csv', lines.join('\n') + '\n')
Run it:
SNAPSBREW_URL=https://snapsbrew.example \
SNAPSBREW_TOKEN=<api-token> \
TEMPLATE_ID=<template-uuid> \
SHEET_CSV_URL='https://docs.google.com/spreadsheets/d/e/<id>/pub?output=csv' \
node sheet-to-images.mjs
The row number in identifier is the row number in the sheet, so you can paste images.csv back next to your data.
What the script relies on
- 100 items for each batch. A batch with more items gets a
422error and the instance creates nothing. The script splits the rows. - Batches run in the background.
batches.wait()reads the batch every 5 seconds until the status iscompleted,failedorcanceled. - A
completedbatch can have failed items. Read thefailedcount and thestatusof each item, not only the status of the batch. - The rate limit is 60 API requests a minute for each user. One batch and its status checks stay far under it.
- Safe retries. If the script can run twice for the same rows, send an
idempotency_keywith each batch. A retry with the same key returns the same batch for 24 hours.
Images or PDFs for print
For certificates, tickets and badges, a PDF is often better than a PNG. Send type: 'pdf' with the batch. Each item of one batch has the same type and format. Read How to generate custom certificates with the API for a full example.
Questions
- How many rows can one import have?
- The upload limit is a 10 MB file in
.xlsx,.xlsor.csvformat. The API takes 100 items for each batch, so a script splits a long sheet into batches of 100. - What happens with an empty cell?
- The layer keeps the value of the template. So you can leave a column empty in the rows that need the default.
- Can a row make a PDF instead of an image?
- Yes. With the API, send
"type":"pdf"with the batch. Each item of a batch has the same type and format, so put PNG rows and PDF rows in two batches. - Do I get a notice when the import ends?
- Yes. The instance emails the person who started the batch. If the batch has a
webhook_url, the instance also posts asnap-import.completed,snap-import.failedorsnap-import.canceledevent to it.