Replace Slide Tokens

replaceSlideTokens(presentation, tokens) finds {{Token}} placeholders in shapes and table cells and replaces the ones you pass in. Unknown tokens are left on the slide on purpose, so a typo like {{Reveneue}} shows up in the deck instead of silently staying blank.

When to use

  • Filling a weekly deck from a summary sheet row
  • Client-ready slides where every number must be checked
  • Any template deck that should fail visibly when a key is missing

How to use this snippet

/** * Replace {{token}} placeholders in shapes and table cells. * Tokens present in `tokens` are substituted; unknown tokens stay visible. * * @param {GoogleAppsScript.Slides.Presentation} presentation * @param {Object<string, *>} tokens map of token name -> replacement value * @return {{replaced: number, missing: string[]}} */ function replaceSlideTokens(presentation, tokens) { tokens = tokens || {}; var replaced = 0; var missing = {}; var re = /\{\{\s*([^}]+?)\s*\}\}/g; function fillTextRange(textRange) { if (!textRange) return; var s = textRange.asString(); if (!s || s.indexOf('{{') === -1) return; var next = s; var m; re.lastIndex = 0; while ((m = re.exec(s)) !== null) { var key = m[1].trim(); if (Object.prototype.hasOwnProperty.call(tokens, key)) { var val = tokens[key] == null ? '' : String(tokens[key]); next = next.split(m[0]).join(val); replaced++; } else { missing[key] = true; // leave {{key}} visible on the slide } } if (next !== s) textRange.setText(next); } presentation.getSlides().forEach(function (slide) { slide.getPageElements().forEach(function (el) { var type = el.getPageElementType(); if (type === SlidesApp.PageElementType.SHAPE) { fillTextRange(el.asShape().getText()); } else if (type === SlidesApp.PageElementType.TABLE) { var table = el.asTable(); for (var r = 0; r < table.getNumRows(); r++) { for (var c = 0; c < table.getNumColumns(); c++) { fillTextRange(table.getCell(r, c).getText()); } } } }); }); return { replaced: replaced, missing: Object.keys(missing) }; }

Example

function fillDeckFromSummary() { var tokens = { Week: '2027-W09', Revenue: '$42,000', Owner: 'Ada' }; var result = replaceSlideTokens(SlidesApp.openById('YOUR_DECK_ID'), tokens); if (result.missing.length) Logger.log('Still on slides: ' + result.missing.join(', ')); }

Notes

  • Token names match after trimming inside the braces: {{ Name }} and {{Name}} are the same key.
  • Shapes and tables are updated. Images, charts, and speaker notes are not.
  • Replacements are plain text. Run Refresh Slides Charts separately for linked charts.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free snippets — plus optional Co-Pilot when you want a boost (Day Pass — $3, or 1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension