Apps Script to Merge a Google Sheet into a GDoc and Create a PDF

This is a Google Apps Script to create a PDF file using a Google Doc template and the values in a Google Spreadsheet.

It takes the fields from the active row in your Google Spreadsheet (the one with a highlighted cell or row) and using a Google Doc template (identified by GDOC_TEMPLATE_ID) creates a PDF doc with these fields replacing the placeholders in the template. The place-holders are identified by having curly brackets around them, e.g. {{Name}}. It is run using the custom “Create PDF” menu that is created in the sheet.

There are many variations on the PDF creation theme.

And here is a function for formatting various number and date types properly into your final document.

Get in touch now if you need any help with this or any other GAS scripts or have a look through some other free scripts and snippets.
So to use:

  1. Create a Google Doc as a template using the placeholders with curly brackets around the words, e.g. {{Name}}. Make a note of the ID number ( NUMBER HERE/edit).
  2. Create a new Google Spreadsheet, or using an existing one, and use placeholders which are the names of the columns with double curly brackets, e.g. Name or Age (it is case-sensitive).
  3. Copy the script into in the script editor (Tools > Script editor).
  4. Paste the template ID number into GDOC_TEMPLATE_ID value (between the ”s)
  5. Set the various configuration settings:
    1. Whether you want to produce a GDoc or PDF
    2. What you want the name of the file to be
    3. Whether you want it sent in an email
    4. Where you would like the new file to be stored
  6. Put the actual values you want to use in the spreadsheet under the appropriate columns, one row for each PDF file.
  7. Click a cell on a row of values you would like to use to select that row and use the “Create PDF > Create PDF”.

The new PDF will be created after a few moments.

If you are looking for a “Gmail merge” rather than a “GDoc merge”, take a look at this great article from Martin Hawksey.

There is a Gist where you can also see the code and A demo spreadsheet.

If you found this script useful why not buy me a coffee to support me create more free content list this.