Conditional Content in Google Docs
With Document Studio, you can set conditional sections in Google Document templates that will only show when the specified criteria are met.
Conditional Sections
You can define paragraphs, tables, images, and other sections of text in your Google Docs template as conditional content. You can then use the <<if>>
expression to display or hide these sections in the generated document based on conditions specified by the user.
You can also use the <<if>>
expression to display or hide a specific sentence within a paragraph based on the user's answers.
Use If and EndIf Expressions
The <<If>>
and <<EndIf>>
expressions are used to define conditional sections in your Google Docs template. The <<If>>
expression is used to define the start of a conditional section, and the <<EndIf>>
expression is used to define the end of a conditional section.
The <<If>>
expression must be followed by a condition that evaluates to either true
or false
. If the condition evaluates to true
, the conditional section will be displayed in the generated document. If the condition evaluates to false
, the conditional section will be hidden in the generated document.
How to Use Conditional Sections
Say you have a question in your Google Form, titled Age
where the form respondent has to provide their age. We have two paragraphs in our Google document template and we want to display or hide them based on the user's age.
<<If: ({{Age}} < 18)>>
You are responsible for obtaining the permission of a parent or legal guardian before making any purchases on this app.
<<EndIf>>
<<If: ({{Age}} >= 18)>>
By purchasing this app, you confirm that you are at least 18 years old. Your purchase entitles you to use this app on one device only.
<<EndIf>>
You can use the AND, OR or NOR operators to combine multiple conditions. For instance, if you want to display a paragraph only when the user's age is between 18 and 25, and live in the UK.
<<If: AND(({{Country}} = 'UK'), ({{Age}} >= 18), ({{Age}} <= 25))>>
You are eligible for a 10% discount on your next purchase.
<<EndIf>>
Things to Remember
- The correct syntax for the
<<If>>
expression is<<If: (condition)>>
. - Enclose the condition in parentheses and use the
AND
,OR
orNOR
operators to combine multiple conditions. - Ensure that all of your
If:
andEndIf
expressions begin with<<
and end with>>
. - The condition supports the following operators:
=
,!=
,>
,<
,>=
,<=
,~
(contains) and!~
(not contains).
Contains and Not Contains Operators
For instance, if you wish to display a paragraph only when the user's email address contains @gmail.com
, you can use the following condition.
<<If: ({{Email Address}} ~ '@gmail.com')>>
You are a Gmail user!
<<EndIf>>
Conditional Formatting in Google Docs
You can use conditional sections in Google Docs to apply conditional formatting to the generated documents. For instance, if the user's age is less than 18, you can add a background color to the text to highlight it and make it stand out.