Skip to main content

How to Customize the Generated PDFs from Google form responses through Logical Constructs

Google Form To Document add-on allows you to generate professional looking documents (Invoices , Certificates , Business Documents etc).

Logical constructs

Introduction

This feature allows you to customize the content of PDF generated.

This should be used inside Template Document to be able to embed the Image in the Final Generated document.

You can define conditions that will determine which parts of the Document Template are included in the final PDF.

Structure of codeblocks

The code used to customize email content is structured using code blocks with the following format:

{%codeblock%}
{%condition%}
<!--This is where you will write the JavaScript code that defines the condition for including the content-->
{%endcondition%}
{%result%}
<!--This is where you will write the content that you want to include in the email.-->
{%endresult%}
{%codeblock%}

Meaning of each section of code blocks

  • {%codeblock%}: This marks the beginning of a code block.
  • {%condition%}: This section defines the condition that will be evaluated to determine whether to include the following content in the PDF.
  • {%endcondition%}: This marks the end of the condition section.
  • {%result%}: This section contains the content that will be included in the PDF if the condition evaluates to true.
  • {%endresult%}: This marks the end of the result section.
  • {%endcodeblock%}: This marks the end of the code block.

Sample code blocks

Here is a sample code block that checks if the answer to the question "Age" is greater than 18 and includes a specific message in the PDF if the condition is true:

{%codeblock%}
{%condition%}
{{Age}} > 18
%endcondition%}
{%result%}
Age is greater than 18 so you are seeing this result.
{%endresult%}
{%endcodeblock%}

Here is a sample code block that checks if the answer to the question "Name" has a lnegth greater than 5 and includes a specific message in the PDF if the condition is true:

{%codeblock%}
{%condition%}
var x = '{{Name}}';
if(x.length> 5) return true ;
else return false;
{%endcondition%}
{%result%}
Name length is greater than 5
{%endresult%}
{%endcodeblock%}

Here is a sample code block that read a check box Question "CB Answer" It reads the answers in a string and evaluates the answers.If the answers are CB1 and CB3 it evaluates to true

{%codeblock%} 
{%condition%}
var x = '{{CB Answer}}'; let cbAnswerArray =x.split(','); if(cbAnswerArray.at(0) == 'CB1' && cbAnswerArray.at(1) == 'CB3' ) return true ;else return false;
{%endcondition%}
{%result%}
two check boxes with answers CB1 and CB3 are present
{%endresult%}
{%endcodeblock%}

Things to Note about condition block

  • Condition block must evaluate to a boolean value .
  • You can use single line statement like {{Age}} > 18 and it will be true if Age is greater than 18.
  • In multi line code you must return a boolean value.
  • You can write javascript syntax.
  • We impose certain restrictions on what functions can be used which is mentioned below.
  • Only if/else and for loops are supported.

Content inside result block

  • You can have any content between the {%result%} and {%endresult%} .
  • The formatting will be preserved
  • You can also insert images inside the result blocks.

Supported Question Types and how Answers are read

  • Only short Answer,Linear Scale ,Multi Choice and Checkboxes are supported.
  • Becareful about empty or non answered questions.
  • If you want to use a Question for condition ,it's a good idea to make it mandatory.

Supported Global Functions

The following global functions are currently supported for use in conditions:

  • parseInt(): Parses a string argument and returns a whole number.
  • parseFloat(): Parses a string argument and returns a floating-point number.
  • isNaN(): Determines whether a value is an NaN (Not a Number).

Supported String Functions

The following string functions are currently supported for use in conditions:

  • split(): Splits a string into an array of substrings.
  • repeat(): Returns a new string repeating a specified value a number of times.
  • reverse(): Reverses the order of the characters in a string.
  • trim(): Removes whitespace from the beginning and end of a string.
  • concat(): Joins two or more strings.
  • toLowerCase(): Converts a string to lowercase letters.
  • toUpperCase(): Converts a string to uppercase letters.
  • substr(): Extracts a section of a string.
  • substring(): Extracts a section of a string.
  • indexOf(): Returns the position of the first occurrence of a specified substring in a string.
  • lastIndexOf(): Returns the position of the last occurrence of a specified substring in a string.
  • startsWith(): Checks whether a string starts with a specified substring.
  • endsWith(): Checks whether a string ends with a specified substring.
  • length: Returns the length of a string.
  • includes(): Checks whether a string contains a specified substring.

Supported Array Functions

The following string functions are currently supported for use in conditions:

  • join(): Joins the elements of an array into a string.
  • at(): Returns the character at a specified index in a string.

Important Notes

  • For security reasons, the use of certain Java Script APIs and functions is restricted.