Navigation
  • Home
  • Work
  • About
  • Writing
  • Contact
Socials
LinkedInGitHubEmailResume
Writing
Code by AsjadAsjad Anwar
  • Home
  • Work
  • About
  • Writing
  • Contact
8 August 2026 · 7 min read

Generate Google Slides from a template with Node.js

I worked on an internal tool that builds client decks. Before it existed, someone opened last month's presentation, made a copy, retyped the client's numbers into text boxes and tables, then opened the spreadsheet behind every chart and retyped numbers there too. The layout never changed. Only the values did.

To generate a Google Slides deck from a template with Node.js, I do four things with the googleapis package. I read the template with the Slides API and collect every $:{name} placeholder and every linked chart. I copy the deck with the Drive API. I send one presentations.batchUpdate whose replaceAllText requests swap each placeholder for its value. For charts, I copy the spreadsheet each chart is linked to, write the new values into the cells the template maps, then delete the chart on the slide and create it again linked to the copy. The values can be typed in, read from a JSON file, or read from a Google Sheet.

The back end is Express with Slides v1, Sheets v4 and Drive v3, and the front end is React. A teammate built the sign-in; this article covers the template flow.

Finding placeholders and linked charts in a Google Slides deck

A placeholder is a marker in the deck's text that names a value to fill in later. Ours look like $:{client_name}. To find them, the server calls presentations.get, which returns the deck as JSON: slides, each with pageElements, each of which may be a shape with text. The scan walks those and runs a regular expression over every text run:

slideData?.data?.slides?.forEach((slide) => {
  slide.pageElements.forEach((element) => {
    if (!element?.shape?.text?.textElements) return;

    element.shape.text.textElements.forEach((textElement) => {
      const text = textElement?.textRun?.content;
      const variableRegex = /\$:{([a-zA-Z0-9_][a-zA-Z0-9_]*)\d*}/g;
      let match;
      while ((match = variableRegex.exec(text)) !== null) {
        variables.push(match[1]);
      }
    });
  });
});

The first version only looked at shapes. We later added a second pass over the same page elements for tables, going through table.tableRows, then tableCells, then each cell's text.textElements, with a slightly stricter pattern, /\$:{([a-zA-Z_][a-zA-Z0-9_]*)}/g.

The match is per text run, which is a stretch of text with one style. So a placeholder has to be typed with one style from $ to } for the scan to see it.

A linked chart is a chart on a slide that was inserted from a Google Sheet and still points at it. In the Slides JSON it is a page element with a sheetsChart object carrying a spreadsheetId and a chartId. The same walk collects one entry per linked chart: { title, id, chartId, values: [] }, where id is the spreadsheet id.

The title is generated, not read from the chart. We number charts per slide, so a title reads Chart 2 - Slide 5.

One route, POST /process-slide, returns all three results for a presentation id: the deck title, the list of placeholder names and the list of charts.

The template model: variables and cell mappings

A template is the record that says what a deck needs. It is one DynamoDB item with the presentation id as id, a name, the user_id, the slide_url, a variables array of placeholder names and a chart_data array.

Text placeholders need nothing more than their names. Charts need a cell-to-variable mapping: a list of pairs such as { cell: "B2", variable: "q3_revenue" } stored in that chart's values array. It means "when generating, put the value called q3_revenue into cell B2 of the sheet this chart reads from". In the React dialog each chart gets an "Add Field" button and two inputs per row, Cell and Variable.

Decks change, so a template can be refreshed. The refresh calls /process-slide again for the stored URL, takes the fresh variable and chart lists, carries the saved mappings over to any chart with the same title, and sends the result to PUT /template/:templateId.

Using a Google Sheet as the data source

A data source is a named set of variables. Each variable has a reference (the name that must match a placeholder or a chart mapping), a query and a resolved value. For an uploaded JSON file the query is a jq-style filter that runs in the browser. For a Google Sheet the query is a cell address.

Linking a sheet goes through POST /process-gsheet with the link and a sheet number. The server pulls the spreadsheet id out of the link with /\/spreadsheets\/d\/([a-zA-Z0-9-_]+)/, calls spreadsheets.get, checks that the sheet number is between 1 and the number of sheets, takes that sheet's title, and reads it with spreadsheets.values.get using the quoted title as the range. The response is a two-dimensional array of rows.

The front end turns a cell address into indices for that array:

const cellNumber = field.query.toUpperCase();

const columnPart = cellNumber.match(/[A-Z]+/)[0];
const rowPart = cellNumber.match(/\d+/)[0];

const columnIndex =
  columnPart.split("").reduce((acc, char) => {
    return acc * 26 + (char.charCodeAt(0) - "A".charCodeAt(0) + 1);
  }, 0) - 1;

const rowIndex = parseInt(rowPart) - 1;

Column letters are read as a base-26 number, so A is 0 and AA is 26. If the row or column falls outside the returned data, the value is an empty string.

Resolved values are stored with the source. A refresh button on each sheet-backed source fetches the sheet again, recomputes every value from its stored cell address and saves them through PUT /variables.

Copying the deck and replacing text with batchUpdate

Generation starts in a dialog where the user picks one or more data sources. For every template variable, the dialog looks for a source variable whose reference equals the placeholder name and prefills its value. Chart cells are prefilled the same way through their mapped variable. If two selected sources define the same reference, the later one in the selection wins.

On the server, the deck is copied first with Drive's files.copy, named with the template name and a timestamp. Then each variable becomes one request:

templateVariables.forEach((element) => {
  if (element.key && element.value) {
    variableRequests.push({
      replaceAllText: {
        replaceText: element.value.toString(),
        containsText: { text: `$:{${element.key}}`, matchCase: false },
      },
    });
  }
});

replaceAllText replaces every occurrence in the presentation, so a placeholder used on five slides needs one request. Variables with no value are skipped, so their placeholder stays in the output.

Filling charts by copying the spreadsheet and re-linking

A chart in the copied deck still points at the template's spreadsheet. Writing client numbers there would change the template, so each source spreadsheet is copied too, again with files.copy. We keep a list of copies made so far, so several charts that share one spreadsheet share one copy.

For each chart, the server loads the copied spreadsheet with spreadsheets.get, finds the sheet that holds a chart with the same chartId, and writes the mapped cells on that sheet:

chart.values.forEach((val) => {
  if (val.cell !== "" && val.value !== "") {
    requests.push({
      range: `${element.properties.title}!${val.cell}`,
      values: [[val.value]],
    });
  }
});

if (requests.length) {
  await sheetsApi.spreadsheets.values.batchUpdate({
    spreadsheetId: chart.newSpreadsheetId,
    resource: { valueInputOption: "USER_ENTERED", data: requests },
  });
}

The chart on the slide then has to point at the copy. The code looks through the new deck for elements whose sheetsChart.chartId matches and queues two requests for each:

chartRequests.push({
  deleteObject: { objectId: element.objectId },
});

chartRequests.push({
  createSheetsChart: {
    chartId: chart.chartId,
    spreadsheetId: chart.newSpreadsheetId,
    linkingMode: "LINKED",
    elementProperties: {
      pageObjectId: slide.objectId,
      size: element.size,
      transform: element.transform,
    },
  },
});

The new chart reuses the old element's size and transform on the same page, so it lands where the old one was. The code passes the template's chartId together with the new spreadsheet id, so it relies on a copied spreadsheet keeping its chart ids.

Text and chart requests then go out together in one presentations.batchUpdate on the new deck, as [...variableRequests, ...chartRequests].

Trade-offs and what I would improve

  • One pattern for placeholders. The shape scan and the table scan use slightly different regular expressions. I would move to a single shared pattern so a name is valid in both places or neither.
  • Fewer Sheets calls. Cell writes are sent per chart and the copied spreadsheet is fetched more than once. I would fetch each copy once and send one values update per spreadsheet.
  • A shared cell-address helper. The A1-to-index conversion lives in two React components. It belongs in one module with tests for multi-letter columns.
  • Stable chart identity. Refresh carries mappings over by generated title, so reordering charts on a slide moves a mapping. Matching on spreadsheet id plus chart id would survive that.

Takeaways

  • A Slides template needs no special format: a text marker like $:{name} plus replaceAllText is enough for text.
  • Charts are filled through their spreadsheet, so store a cell-to-variable mapping per chart in the template.
  • Copy the linked spreadsheet before writing, or every generated deck edits the template's data.
  • Re-link a chart by deleting it and creating a LINKED chart with the old element's size and transform.
  • Put text and chart requests in one presentations.batchUpdate on the copied deck.
Node.jsGoogle Slides APIGoogle Sheets APIReact

Keep reading

  • 28 November 2026 · 7 min read

    RTK Query caching for sensitive data: what never to cache

    How I set up RTK Query caching in a React admin console that shows patient data: memory only, 30-second revalidation, and reads that are never repeated.

  • 12 November 2026 · 5 min read

    Migrating 86,000 lines from Vue to React with an AI agent

    How I ran a Vue to React rewrite of a customer-facing e-commerce support app with an AI coding agent, and what review, lint and tests had to catch.

All writing

Let’s work
together

Get in touch
theasjadanwar@gmail.com+92 321 4010734Resume (PDF)LinkedInGitHub
Based inLahore, Pakistan (UTC+5)
SocialsLinkedInGitHub