This script creates a number of PDF files using a Google Doc template and the rows selected in a Google Spreadsheet.
The script takes the fields from each of the selected rows in the GSheet “containing” the script and, using a Google Doc template (identified by TEMPLATE_ID), creates multiple PDFs. The fields replace the place-holders in the template. The place-holders are identified by having a << or >> either side, e.g. <<Name>>.
It is invoked by the “Create PDFs” menu.
You can also:
- specify a name for the PDF file in a column called “File Name”
- automatically email the PDF by specifying a recipient in a “Email” column
- specify the folder that the PDFs are stored in using RESULTS_FOLDER_ID
- use placeholders in the template header
If it finds a “Date Sent” column the script also records the date/time the PDF was sent.
- Create a Google Doc as a template using the place-holders with << and >> around the words, e.g. <<Name>>. Make a note of the ID number (https://docs.google.com/document/d/ID NUMBER HERE/edit). Like this for example.
- Create a new Google Spreadsheet and give it header names (first row) that match up with the placeholders minus the angle brackets, e.g. “Name” or “Age” (it is case-sensitive), and optionally one called “File Name” to specify the name of the PDF file created and “Email” to specify who the PDF should be emailed to.
- Copy the script into Code.gs in the script editor (Tools > Script editor), paste the template ID number into TEMPLATE_ID value (between the ”s) and add a file name for the new PDF in PDF_FILE_NAME if you don’t want to use the template name.
- Complete EMAIL_SUBJECT and EMAIL_BODY if you are sending the PDF as an email attachment
- Update the GSheet. Put the actual values you want to use in the spreadsheet under the appropriate columns, one row for each PDF file. This could be the responses from a Google Form.
- Select the rows that you want to create and send PDFs from.
- Click the custom menu “Create PDFs > Create PDFs for selected rows” in the GSheet.