Google Apps Script: Templating Google Docs with Conditional Logic and List Iteration (Preserving Formatting)

124 views
Skip to first unread message

Andrea Alberto

unread,
Feb 4, 2025, 3:36:09 PMFeb 4
to google-apps-sc...@googlegroups.com
Hi everyone,

I'm working on a Google Apps Script project where I need to generate Google Docs dynamically based on data and a template document. The key requirements are:
 * Template Document: I'm using a Google Docs document itself as the template. This document contains the static text, desired formatting (headings, fonts, tables, lists, etc.), and placeholders for the dynamic data.
 * Data Input: The data will be provided to the script (e.g., from a Google Sheet, form submission, or other source).
 * Dynamic Content: I need to use conditional statements (similar to "if" logic) and list iterations (similar to "for" loops) within the template to control which parts of the document are included and how data is populated. For example, I might have a section in my template that should only be included if a certain condition is met (e.g., {{#if isPremiumUser}} ... {{/if}}), or I might need to generate a list of products from an array of data (e.g., {{#each products}} ... {{/each}}).
 * Crucially: I need to preserve the exact formatting of the template document in the generated output. This includes all styles, fonts, tables, lists, etc.

I've been experimenting with replacing placeholders in the document body using replaceText(), but this can become complex when dealing with conditional logic and lists, especially when trying to maintain the original formatting. I'm also very concerned about the performance implications of manipulating the document content in this way, especially when dealing with larger documents (hundreds of pages) and datasets (thousands of rows).

Has anyone had experience with a similar use case? Are there any recommended approaches or best practices for achieving this? 

 I'm particularly interested in:
 * Efficiently handling conditional logic and list iterations within a Google Docs template. I'd like to avoid building a full-blown templating engine within my Apps Script if possible.
 * Preserving the original formatting of the template document. This is critical.
 * Optimizing performance, especially for larger documents and datasets.
I'd be extremely grateful for any advice, code examples, links to existing solutions, or even pointers to relevant discussions on this topic.

Thanks in advance for your help!

Andrea

DimuDesigns

unread,
Feb 4, 2025, 7:27:03 PMFeb 4
to Google Apps Script Community
Given how much you need this tool to scale (100s of pages) I am skeptical that your requirements can be met given Google Apps Script's inherent limitations.

Before you sink too much time into this project, make a point of profiling your Apps Script experiments to gauge performance.

Whip up a few sample templates and data of increasing complexity and size and analyze script execution against them using the console.time() and console.timeEnd() functions to find any performance bottlenecks. 

With enough preliminary testing you can probably chart how things might scale with larger documents and datasets.

That should give you enough insight to determine if this project is worth pursuing in GAS.

Also, note that GAS is not your only option here. Google Docs has a REST API that makes it accessible to other programming languages and platforms.

So if you find that GAS is not viable you can still explore other development options.

Curious to see how this pans out for you.
Reply all
Reply to author
Forward
0 new messages