Actions

ExpressionScript examples/zh: Difference between revisions

From LimeSurvey Manual

(Updating to match new version of source page)
(Updating to match new version of source page)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
<languages />
<languages />
__TOC__
__TOC__


{{Note|'''请通过添加自己的示例帮助扩展这篇文章'''。这将有助于其他人开始使用表达式管理器。}}
{{Note| '''Please help extending this article''' by adding your own examples. This will help others getting started using the Expression Script.}}




= 结构 =
=Structure=




本文的第一部分将介绍可以使用哪些表达式以及在何处查找/添加表达式。第二部分提供了有关如何使用表达式的示例。
The first part of the article will describe what Expressions can be used for and where to find/add an Expression. The second part provides examples on how to use Expression.




=表达式的类别=
=Categories of Expressions=




LimeSurvey使用表达式用于以下目的:
LimeSurvey uses Expressions for the following purposes:
* [[Expression_Manager_examples/zh#相关性|相关性]]
* [[ExpressionScript_examples#Relevance|Relevance]]
* [[Expression_Manager_examples/zh#验证|验证]]
* [[ExpressionScript_examples#Validation|Validation]]
* [[Expression_Manager_examples/zh#文本输出|文本输出]]
* [[ExpressionScript_examples#Text output|Text output]]
* [[Expression_Manager_examples/zh#答案存储|答案存储]]  
* [[ExpressionScript_examples#Answers storing|Answers storing]]  




== 相关性 ==
==Relevance==




'''目的:隐藏问题组/问题'''
'''Purpose: Hiding question groups/questions'''


相关性控制是显示还是隐藏问题组或问题。如果表达式的结果为“真”(即1),则将显示该元素。否则(如果表达式的结果为“假”(即0)),则元素将被隐藏。
Relevance controls whether a question group or question is shown or hidden. If the result of the Expression is "true" (i.e. 1), the element will be shown. Otherwise (if the result of the Expression is "false" (i.e. 0)), the element will be hidden.


{|class="wikitable"
{|class="wikitable"
Line 42: Line 40:




== 验证 ==
==Validation==




'''目的:验证问题/子问题/答案的输入/操作'''
'''Purpose: Validate input/actions on questions/subquestions/answers'''


验证控制受访者对问题或行动的输入是否有效。如果表达式的结果为“true”(即1),则“good”类将应用于元素和验证消息。否则,如果表达式的结果是“假”(即0)),则“错误”类将被用于元素和验证消息。您可以在'''template.css'''中为这些类声明样式。
Validation controls whether the respondent's input into a question or action is valid or not. If the result of the expression is "true" (i.e. 1), the "good" class will be applied to the element and validation message. Otherwise, if the result of the expression is "false" (i.e. 0)), the "error" class will be apllied to the element and validation message. You can declare a style for these classes in the '''template.css'''.


<div class="mw-translate-fuzzy">
{|class="wikitable"
{|class="wikitable"
!类型!!验证!!在哪里找!!屏幕截图
!Type!!Validates!!Where to find!!Screenshot
|-
|-
|问题验证||问题||创建/编辑问题 - >“逻辑选项卡” - >“问题验证方程式”||[[File:Question validation equation.png|30px]]
|Question Validation||question||Create/Edit question -> "Logic tab" -> "Question validation equation"||[[File:Question validation equation.png|30px]]
|-
|-
|子问题验证||子问题||创建/编辑问题 - >“逻辑选项卡” - >“子问题验证方程“||[File:Question validation equation.png|30px]]
|Subquestion Validation||subquestions||Create/Edit question -> "Logic tab" -> "Subquestion validation equation"||[[File:Subquestion validation screenshot.png|30px]]
|}
|}
</div>


==文本输出==
==Text output==




'''目的:(做点什么)并写一些文本'''
'''Purposes: (Do something) and write out some text'''


表达式可用于写出一些文本。例如,你可以测试或计算某些内容,并根据测试或计算的结果指定输出。它还可用于插入使用表达式之前使用的问题的问题文本、答案、问题类型等。文本输出可以在显示文本的任何地方使用,并且可以返回各种结果。
An expression can be used to write out some text. For example, you can test or calculate something and specify the output depending on the result of the test or calculation. It can also be used to insert the question text, answers, question types, ... of a question used before the Expression is used. Text output can be used everywhere where a text is shown and can return every kind of results.


{|class="wikitable"
{|class="wikitable"
!用例!!说明
!Use Case!!Description
|-
|-
|微剪裁||调整文字,例如根据之前提出的性别问题写“先生”或“女士”
|Micro tailoring||Adjust a text, e.g., write "Mr" or "Mrs" depending on a gender question asked before
|-
|-
|计算||计算一个值并将其写出来,例如“你的家庭收入是xxx”
|Calculation||Calculate a value and write it out, e.g., "Your family income is xxx"
|-
|-
|参考||插入元素中的文本,例如, “你的名字是xxx。你多大了?”
|Reference||Insert a text from an element, e.g., "Your name is xxx. How old are you?"
|}
|}




==答案存储==
==Answers storing==




'''目的:(做点什么)(写一些文本)并在数据库中存储一些东西'''
'''Purposes: (Do something,) (write out some text) and store something in the database'''


[[Question type - Equation|方程式问题类型]] 可用于在数据库中存储内容。你可以使用方程式执行某些操作,显示方程式问题(类似于[[Question type - Text display|文本显示]])并将表达式的结果存储在数据库中。它将答案存储在响应表中。然后,结果可以在统计功能中使用,也可以导入到LimeSurvey导出功能提供的格式之一。
[[Question type - Equation|The Equation Question Type]] can be used to store something in the database. You can do something with an Expression, show the Equation question (which is similar to a [[Question type - Text display|text display]]) and store the result of the Expression in the database. It stores the answer into the responses table. Then, the result can be used within the statistics feature or it can also be imported into one of the formats provided by the LimeSurvey export functionality.


=表达式的例子=
=Examples for Expressions=




==插入上一个问题的答案==
==Insert an answer from a previous question==




{| class="wikitable"
{| class="wikitable"
|目的||插入上一个问题的答案
|Purpose||Insert an answer from a previous question
|-
|-
|类型||参考
|Type||Reference
|-
|-
|示例||问题一(问题代码“ Q00“):你住在哪个城市? <br />问题二(问题代码“Q01”)你在CITY住多久了? <br />要做的:第二个问题应该使用问题1的答案而不是“CITY”。
|Example||Question one (question code "Q00"): What city do you live in? <br /> Question two (question code "Q01") How long have you lived in CITY? <br /> To do: The answer from question one should be used instead of "CITY" from the second question.
|-
|-
|表达式|| {QOO}
|Expression||{QOO}
|-
|-
|说明 || Q00中提供的答案取代了第二个问题的Q00字段
|Description|| The answer provided in Q00 replaces the Q00 field from the second question
|-
|-
|步骤||创建/编辑问题二<br />插入“你在{QOO}生活多久了?”进入问题文本字段
|Steps||Create/Edit question two <br /> Insert "How long have you lived in {QOO} (years)?" into the question text field
|-
|-
|colspan="2" align="center"|[[File:Simple expression city name.png]]
|colspan="2" align="center"|[[File:Simple expression city name.png]]
|}
|}


==使用隐藏的方程式问题自动填充答案==
==Using hidden equation question to autofill an answer==




'''问题:'''
'''Problem:'''
想象一下你有两个问题Q1和Q2。 Q1询问受访者的年龄。 Q2将受访者分为三组:年龄低于20岁,年龄为20岁,年龄大于20。
Imagine you have two questions Q1 and Q2. Q1 asks respondents for their age. Q2 divides respondents into three groups: age is below 20 years, age is 20, age is greater than 20.
因此,Q2应使用值“1”,“2”,“3”,这对应于以上条件。此外,我们不应忘记使Q2不可见(问题不会显示在调查中,但值将通过响应表中的“后台进程”)。
So, Q2 should make use of the values "1", "2", "3", which correspond to the above conditions. Also, we should not forget to make Q2 invisible (the question will not be displayed in the survey, but the values will be via the "background process" within the responses table).


'''使用公式问题类型填写数据库:'''
'''To fill in the database using the Equation question type:'''
*首先,创建一个问题,将代码Q1作为数字输入问题。
* First, create a question with code Q1 as a numerical input question.
*然后用代码Q2创建另一个问题作为方程问题。
* Then create another question with code Q2 as an equation question.
*在Q2的“显示设置”选项卡章节中:
* In the "Display settings" tab chapter of Q2:
**将“始终隐藏此问题”字段设置为“开”
** set "Always hide this question" field as "On"
**在“公式”字段中输入表达式:
** type in "Equation" field the expression:
<pre>Q2=if(Q1.NAOK < 20, "1", if(Q1.NAOK > 20, "3", "2"))}</pre>
<pre>{if(Q1.NAOK < 20, "1", if(Q1.NAOK > 20, "3", "2"))}</pre>


==使用占位符创建摘要页面==
==Create a summary page using placeholders==




本教程演示了如何在调查结束时创建概述,通过[http://manual.limesurvey.org/Expression_Manager#Access_to_Variables Expression Manager placeholders]列出所有问题和答案。
This tutorial demonstrates how you can create an overview at the end of the survey, listing all questions and answers via [http://manual.limesurvey.org/ExpressionScript_-_Presentation#Access_to_variables ExpressionScript placeholders].
它还说明了如何限制这样的概述仅回答问题。
It also shows how to limit such an overview to answered questions only.


我们的示例可以从这里下载:[[Media:Limesurvey_Expression_Manager_Placeholder_Demo_Survey.lss|调查占位符调查示例]]
Our example can be downloaded from here: [[Media:Limesurvey_Expression_Manager_Placeholder_Demo_Survey.lss|Survey placeholders survey example]].




'''''这里使用的问题类型:'''''
'''''Question types being used here:'''''


'''1. 单选题/单文/数字问题'''
'''1. Single choice questions / single text/numeric questions'''
*列表(下拉)
*List (dropdown)
*列表(单选)[L]
*List (radio) [L]
*/[Y]
*Yes/No [Y]
*长自由文本[T]
*Long free text [T]
*短自由文本[S]
*Short free text [S]
*数值输入[N]
*Numerical input [N]
*方程式[*]
*Equation [*]




'''2. 子问题'''的问题'''
'''2. Question with subquestions'''
*多选题短文本[Q]
*Multiple short text [Q]
*多选题[M]
*Multiple choice [M]
*阵列[F]
*Array [F]
*阵列(是//不确定)[C]
*Array (Yes/No/Uncertain) [C]
*阵列(10分选择)[B]
*Array (10 point choice) [B]




'''3. 有2个尺度'''的问题
'''3. Questions with 2 scales'''
*阵列双尺度[1]
*Array dual scale [1]




'''4 . X和Y的问题缩放'''
'''4. Questions with X and Y scales'''
*阵列(数字)[:]
*Array (Numbers) [:]




'''5. 掩码问题'''
'''5. Mask questions'''
*文本显示[X]
*Text display [X]




在这个简单的例子中,我们的概述将是一个简单的列表:
In this simple example, our overview will be a simple list with:


- ''问题文本:''用户的答案
- ''Question text:'' User's answer




===单选题/单文本/数字问题===
===Single choice questions / single text/numeric questions===




对于所有这些问题类型,Limesurvey将存储一个响应:
For all these question types, Limesurvey will store a single response:


*列表(下拉)
*List (dropdown)
*列表(单选)
*List (radio)
*带评论的列表
*List with comment
*5分选择
*5 point choice
*/
*Yes/No
*短自由文本
*Short free text
*长自由文本
*Long free text
*超长自由文本
*Huge free text
*数字输入
*Numerical input
*等式
*Equation
*日期
*Date
*性别
*Gender


假设问题的问题代码是q1,我们可以使用以下内容引用问题文本和答案:
Assuming the question code of the question is q1, we can reference the question text and answer using:


- {q1.question}: {q1.shown}
- {q1.question}: {q1.shown}


例:
Example:


- 你多大了?:25
- How old are you?: 25




如果要检查是否已回答该问题,可以在输出周围放置IF语句:
If you want to check whether that question was answered you can put an IF statement around your output:


<pre>{if(!is_empty(q1),join("- ",q1.question,": ",q1.shown),"")}</pre>
<pre>{if(!is_empty(q1),join("- ",q1.question,": ",q1.shown),"")}</pre>


这个“翻译”为:IF 代码q1的问题不为空,输出“ - AND 问题文本 AND “:” AND 答案文本(join()函数将元素作为新字符串连接); ELSE:不输出任何内容(“ “表示空字符串)。
This "translates" to: IF question with code q1 is not empty, output "- " AND the question text AND ": " AND the answer text (the join() function joins elements as a new string; ELSE: output nothing ("" means empty string).


===子问题===
===Question with subquestions===




对于所有这些问题类型,LimeSurvey使用子问题:
For all these question types LimeSurvey uses subquestions:


*多选题短文本
*Multiple short text
*多选题
*Multiple choice
*有评论的多选题
*Multiple choice with comments
*阵列
*Array
*阵列(5分选择)
*Array (5 point choice)
*阵列(10分选择)
*Array (10 point choice)
*阵列(是/ / 不确定)
*Array (Yes/No/Uncertain)
*阵列(增加,不变,减少)
*Array (Increase/Same/Decrease)
*按列阵列
*Array by column


假设问题的问题代码是q2并且子问题编号为SQ001,SQ002(由LimeSurvey完成的自动编号),我们可以使用以下内容引用问题文本和答案:
Assuming the question code of the question is q2 and the subquestions are numbered SQ001, SQ002, (auto-numbering done by LimeSurvey), we can reference the question text and answer using:


- {q2_SQ001.question}: {q2_SQ001.shown}
- {q2_SQ001.question}: {q2_SQ001.shown}
Line 221: Line 217:
- {q2_SQ002.question}: {q2_SQ002.shown}
- {q2_SQ002.question}: {q2_SQ002.shown}


对于多选题,现在显示勾选的子项的文本,但为每个选定的选项显示Y是有意义的:
For multiple choice questions it makes sense to now show the text of the ticked subquestion but to show a Y for each selected option:


- {q2_SQ001.question}: {q2_SQ001}
- {q2_SQ001.question}: {q2_SQ001}
Line 229: Line 225:
...
...


请注意,目前无法输出带子问题问题的文本,请参阅[http://bugs.limesurvey.org/view.php?id=8858 this feature request]
Note that currently it is not possible to output the question text of questions with subquestions, see [http://bugs.limesurvey.org/view.php?id=8858 this feature request].




====示例1(假设问题类型是多选题)====
====Example 1 (assuming question type is multiple choice)====




- 你知道这些汽车品牌吗?
- Do you know these car brands?


-- Mercedes: Y
-- Mercedes: Y
Line 244: Line 240:




<div class="mw-translate-fuzzy">
If you want to output checked items only you can put an IF statement around your output and check for the checkbox value:
-- Mercedes: Y
</div>


<pre>{ if( q2_SQ001=="Y", join( "- ", q2_SQ001.question, ": ", q2_SQ001.shown ), "" ) }</pre>
<pre>{ if( q2_SQ001=="Y", join( "- ", q2_SQ001.question, ": ", q2_SQ001.shown ), "" ) }</pre>




<div class="mw-translate-fuzzy">
Use the <i>listifop</i> function if you want to output a list of only the selected items, eg: Mercedes, Volvo
-- Audi: N
</div>


<pre>{ listifop( 'value', '==', 'Y', 'question', ', ', that.q2.sgqa ) }</pre>
<pre>{ listifop( 'value', '==', 'Y', 'question', ', ', that.q2.sgqa ) }</pre>




<div class="mw-translate-fuzzy">
Where &nbsp;<small>that.q2</small>&nbsp; expands to all of q2's subquestions. See [[ExpressionScript - Presentation#The_reserved_.22this.22.2C_.22self.22.2C_and_.22that.22_variables|self, this and that]] for more detail.
如果只想输出选中的项目,可以在输出周围放置一个IF语句并检查复选框值:
</div>


</br>
</br>


====示例2(假设问题类型是阵列(10点选择))====
====Example 2 (assuming question type is Array (10 point choice))====




<div class="mw-translate-fuzzy">
- Please rate the quality of the car brands below on a scale from 1=very bad to 10=very good?
-- Audi: 9
</div>


<div class="mw-translate-fuzzy">
-- Mercedes: 7
如果您只想输出评级项目,可以在输出周围放置一个IF语句,并使用以下方法检查当前子问题是否得到了解答:
</div>


<div class="mw-translate-fuzzy">
-- Audi: 9
<pre>{if(!is_empty(q3_SQ001),join("- ",q3_SQ001.question,": ",q3_SQ001.shown),"")}</pre>
</div>


<div class="mw-translate-fuzzy">
-- Volvo: 9
===有两个尺度的问题===
</div>




<div class="mw-translate-fuzzy">
If you want to output rated items only you can put an IF statement around your output and check if the current sub question was answered by using:
{{Note|这仅适用于双尺度问题类型。}}
</div>


<div class="mw-translate-fuzzy">
<pre>{ if( ! is_empty( q3_SQ001 ), join( "- ", q3_SQ001.question, ": ", q3_SQ001.shown ), "" ) }</pre>
假设正在使用以下代码:
*问题代码:q4
*子问题代码:SQ001,SQ002,......
*答案代码比值1:A1,A2,......
*答案代码比值2:B1,B2,......
</div>




<div class="mw-translate-fuzzy">
Use the <i>listifop</i> function if for example you want to only output a list of the selected items greater than eight, eg: Audi / Volvo
{{Note|要在某个比例上引用某个子问题的结果,你需要使用QuestionCode . '_' . SubQuestionCode . '_' . ScaleID。 '''请注意,“ScaleID”对于第一个刻度为0,对于另一个刻度为1!''' </br>参考第三个子问题和第二个比例答案的示例:q4_SQ003_1 - Question 4, subquestion 3, second scale。}}
</div>


<div class="mw-translate-fuzzy">
<pre>{ listifop( 'value', '>', 8, 'question', ' / ', that.q3.sgqa ) }</pre>
让我们输出两个比例和前两个子问题的结果:
</div>


</br>
</br>
Line 331: Line 301:
To output the subquestion text, you need to add the scale ID to the placeholder (though the texts are the same for both scales). So instead of {q4_SQ001.question} we have to use {q4_SQ001_0.question} or {q4_SQ001_1.question}.
To output the subquestion text, you need to add the scale ID to the placeholder (though the texts are the same for both scales). So instead of {q4_SQ001.question} we have to use {q4_SQ001_0.question} or {q4_SQ001_1.question}.


如果要仅使用至少一个答案导出/显示子问题的结果,请对每个子问题使用以下语法:
If you want to export/display the results of subquestions with at least one answer only, use this syntax for each subquestion:


<pre>{if(count(q4_SQ001_0,q4_SQ001_1)>0,join("- ",q4_SQ001_0.question,": ",q4_SQ001_0.shown," / ",q4_SQ001_1.shown),"")}</pre>
<pre>{if(count(q4_SQ001_0,q4_SQ001_1)>0,join("- ",q4_SQ001_0.question,": ",q4_SQ001_0.shown," / ",q4_SQ001_1.shown),"")}</pre>


=== X和Y尺度的问题===
===Question with X and Y scales===




这适用于所有矩阵问题,它们允许每个单元格的答案(不仅仅是前面所示的每一行):
This applies to all matrix questions which allow an answer for every cell (not just every row as shown previously):
*阵列文本
*Array Texts
*阵列编号
*Array Numbers




使用的假设代码:
Assumed codes used:
*问题代码:q5
*Question code: q5
*子代码:SQ001,SQ002,...
*Subquestion codes: SQ001, SQ002, ...
*答案代码:A1,A2,...
*Answer codes: A1, A2, ...


要引用某列的某个子查询的结果,您需要使用QuestionCode . '_' . SubQuestionCode . '_' . AnswerCode。参考第三个子问题和第二列的答案的示例:q5_SQ003_A2。
To refer to the result of a certain subquestion from a certain column, you need to use QuestionCode . '_' . SubQuestionCode . '_' . AnswerCode. Example for referring to the answer of the third subquestion and second column: q5_SQ003_A2.


让我们输出前两个子项的第1-3列的结果:
Let's output the results for columns 1-3 of the first two subquestions:


- {q5_SQ001_A1.question}: {q5_SQ001_A1.shown} | {q5_SQ001_A2.shown} | {q5_SQ001_A3.shown}
- {q5_SQ001_A1.question}: {q5_SQ001_A1.shown} | {q5_SQ001_A2.shown} | {q5_SQ001_A3.shown}
Line 357: Line 327:




由于对于这些问题类型,每个单元格(X轴和Y轴的组合)等于一个答案选项,因此需要对每个单元格进行现有数据的测试。例:
Since for these question types each cell (combination of X axes and Y axes) equals one answer option, a test for existing data needs to be done for each cell. Example:


<pre>{if(!is_empty(q5_SQ001_A1),join("- ",q5_SQ001_A1.question,": ",q5_SQ001_A1.shown),"")}</pre>
<pre>{if(!is_empty(q5_SQ001_A1),join("- ",q5_SQ001_A1.question,": ",q5_SQ001_A1.shown),"")}</pre>




{{Note|要输出子问题文本,你需要将答案代码添加到占位符(尽管所有答案选项的文本都相同)。因此,我们必须使用{q5_SQ001_A1.question}{q5_SQ001_A2.question}而不是{q5_SQ001.question}}}
{{Note|To output the subquestion text you need to add the answer code to the placeholder (though the texts are the same for all answer options). So instead of {q5_SQ001.question} we have to use {q5_SQ001_A1.question} or {q5_SQ001_A2.question}.}}


==隐藏问题,如果上一个问题的答案是空的==
==Hide question if answer from previous question is empty==




{|class="wikitable"
{|class="wikitable"
|目的||隐藏问题,如果上一个问题的回答是空的
|Purpose||Hide question if answer from previous question is empty
|-
|-
|类型||问题相关性
|Type||Question Relevance
|-
|-
|示例||问题一:问题代码“name”,问题文本“你叫什么名字?” <br />问题二:问题文字“{name},你多大了?” <br />要做:如果问题1的文本字段为空,则隐藏问题二
|Example||Question one: question code "name", question text "What's your name?" <br /> Question two: question text "{name}, how old are you?" <br /> To do: Hide question two if the textfield of question one is empty
|-
|-
|表达式||!is_empty(name)
|Expression||!is_empty(name)
|-
|-
|描述||is_empty()确定是否为变量被认为是空的。 “!”否定结果。因此,如果变量不为空,则表达式将为真,并显示问题
|Description||is_empty() determines whether a variable is considered to be empty. The "!" negates the result. So if the variable is not empty the Expression will be true and the question is shown
|-
|-
|步骤||创建/编辑问题二<br />将“!is_empty(name)”插入“相关方程式:”
|Steps||Create/edit question two <br /> Insert "!is_empty(name)" into "Relevance equation:"
|-
|-
|示例文件||[[Media:Hide_question_if_empty_question_group.zip|Hide_question_if_empty_question_group.zip]]
|Sample File||[[Media:Hide_question_if_empty_question_group.zip|Hide_question_if_empty_question_group.zip]]
|-
|-
|colspan="2" align="center"|[[File:Hide question if empty 1.png]][[File:Hide question if empty 2.png]]
|colspan="2" align="center"|[[File:Hide question if empty 1.png]][[File:Hide question if empty 2.png]]
Line 386: Line 356:




==如果上一个问题的回答是“是”或“否”,则隐藏问题组==
==Hide question group if answer from previous question is Yes or No==




{|class="wikitable"
{|class="wikitable"
|目的||隐藏问题组如果上一个问题的回答是“是”或“否”
|Purpose||Hide question group if answer from previous question is Yes or No
|-
|-
|类型||问题组相关性
|Type||Question group Relevance
|-
|-
|示例||第一页,问题组一,问题一:问题代码“PET”,问题文本“你有宠物吗?” - - <br />第二页,问题第二组:题目“关于你的宠物” <br />要做的事:显示/隐藏问题组2如果问题1的回答是是/
|Example||Page one, Question Group one, Question one: question code "PET", question text "Do you have a pet?" -Yes -No <br /> Page two, Question Group two: Title "About your pet(s)" <br /> To do: Show/Hide question group two if answer from question one is Yes/No
|-
|-
|表达式|| PET ==“Y”
|Expression||PET == "Y"
|-
|-
|说明|| PET是您要检查答案的问题的问题代码。如果你不使用后缀EM将使用“Qcode.code”。因此,您将PET问题的答案代码与值“Y”进行比较。如果参与者回答“是”,则表达式为真,并且将显示问题组“关于您的宠物”。
|Description||PET is the question code for the question you want to check the answer. If you don't use a suffix EM will use "Qcode.code". So you compare the answer code from the PET question to the value "Y". If the participant answers "Yes" the Expression is true and the question group "About your pet(s)" will be shown.
|-
|-
|步骤||创建/编辑问题组2 <br />将“PET ==”Y“”插入“相关等式:”
|Steps||Create/edit question group two <br /> Insert "PET == "Y"" into "Relevance equation:"
|-
|-
|示例文件||[[Media:Hide question group if answer from previous question is Yes or No.zip|Hide_question group_if_answer_from_previous_question_is_Yes_or_No.zip]]
|Sample File||[[Media:Hide question group if answer from previous question is Yes or No.zip|Hide_question group_if_answer_from_previous_question_is_Yes_or_No.zip]]
|-
|-
|colspan="2" align="center"|[[File:Hide question group if answer from previous question is Yes or No 1.png]][[File:Hide question group if answer from previous question is Yes or No 2.png]]
|colspan="2" align="center"|[[File:Hide question group if answer from previous question is Yes or No 1.png]][[File:Hide question group if answer from previous question is Yes or No 2.png]]
Line 408: Line 378:




==显示多个答案输入问题字段的值==
==Display value of a multiple answer input question field==




{|class="wikitable"
{|class="wikitable"
|目的||多个答案输入问题字段的显示值
|Purpose||Display value of a multiple answer input question field
|-
|-
|类型||参考
|Type||Reference
|-
|-
|示例||问题一:问题代码“AskChildAge”,问题文字“你的孩子多大了?”。子代码-Child1 -Child2 -Child3 - ChildXXX <br />问题二:问题代码“ReportChildAge”问题文本“关于你的第一个孩子: - CHILD1是AGE1。”
|Example||Question one: question code "AskChildAge", question text "How old are your children?". Subquestions codes -Child1 -Child2 -Child3 - ChildXXX <br /> Question two: question code "ReportChildAge" question text "About your first child: - CHILD1 is AGE1."
|-
|-
|表达式||{AskChildAge_Child1.question}, {AskChildAge_Child1.value}
|Expression||{AskChildAge_Child1.question}, {AskChildAge_Child1.value}
|-
|-
|说明||你想在以下问题中使用子问题的值。你可以使用以下表达式访问子问题值:QcodeQuestion_QcodeSubquestion.value
|Description||You want to use the value of a subquestion in a following question. You can access the subquestion value with this kind of expression: QcodeQuestion_QcodeSubquestion.value
|-
|-
|步骤||创建/编辑第二个问题<br />在说明中插入此文字:“关于你的第一个孩子:- {AskChildAge_Child1.question} {AskChildAge_Child1.value}。”
|Steps||Create/edit the second question <br /> Insert this text in the description: "About your first child: - {AskChildAge_Child1.question} is {AskChildAge_Child1.value}."
|-
|-
|示例文件||[[Media:Display_value_of_a_multiple_answer_input_question_field.zip|Display_value_of_a_multiple_answer_input_question_field.zip]]
|Sample File||[[Media:Display_value_of_a_multiple_answer_input_question_field.zip|Display_value_of_a_multiple_answer_input_question_field.zip]]
|-
|-
|colspan="2" align="center"|[[File:Display value of a multiple answer input question field 1.png]]
|colspan="2" align="center"|[[File:Display value of a multiple answer input question field 1.png]]
|}
|}


==验证 "数组 (数字) 复选框" 问题的每行勾选的框数==
==Validate number of boxes ticked per row for an "Array (Numbers) Checkbox" question==




{|class="wikitable"
{|class="wikitable"
|目的||验证每行勾选的方框数
|Purpose||Validate number of boxes ticked per row
|-
|-
|类型||验证
|Type||Validation
|-
|-
|示例||问题类型“数组编号(复选框布局)“
|Example||Question of type "Array Numbers (Checkbox Layout)"
|-
|-
|表达式||sum(...)
|Expression||sum(...)
|-
|-
|方程式||(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X)确定是否检查了行A中的至少X个复选框。 <br />如果要检查所有三行(A,B,C),可以使用“&&”连接表达式:<br />(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X) <br />&& (sum(Test_B_1, Test_B_2, Test_B_3, Test_B_4, Test_B_5) <= X) <br />&& (sum(Test_C_1, Test_C_2, Test_C_3, Test_C_4, Test_C_5) <= X)
|Description||(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X) determines whether at least X checkboxes in row A are checked. <br />If you want to check all three rows (A, B, C), you can connect the expression using "&&": <br />(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X) <br />&& (sum(Test_B_1, Test_B_2, Test_B_3, Test_B_4, Test_B_5) <= X) <br />&& (sum(Test_C_1, Test_C_2, Test_C_3, Test_C_4, Test_C_5) <= X)
|-
|-
|步骤||创建/编辑“数组编号(复选框布局)”类型的问题。 <br />在高级问题设置中将上述表达式插入“问题验证方程式”(您可能需要调整变量命名!)。
|Steps||Create/edit question of type "Array Numbers (Checkbox Layout)".<br /> Insert the above expression into "Question validation equation" at the advanced question settings (you might have to adjust variable namings!).
|-
|-
|示例文件||[[Media:limesurvey_survey_53654.lss|Validate number of boxes ticker per row for an Array (Numbers) Checkbox.lss]]
|Sample File||[[Media:limesurvey_survey_53654.lss|Validate number of boxes ticker per row for an Array (Numbers) Checkbox.lss]]
|-
|-
|colspan="2" align="center"|[[File:Validate number of boxes ticker per row for an Array (Numbers) Checkbox 05.png]][[File:Validate number of boxes ticker per row for an Array (Numbers) Checkbox 1.png]]
|colspan="2" align="center"|[[File:Validate number of boxes ticker per row for an Array (Numbers) Checkbox 05.png]][[File:Validate number of boxes ticker per row for an Array (Numbers) Checkbox 1.png]]
|}
|}


==计算两个日期之间的差异==
==Calculate difference between two dates==




{| class="wikitable"
{| class="wikitable"
|目的||计算两个日期之间的差异
|Purpose||Calculate difference between two dates
|-
|-
|类型||相关性/方程式问题类型/验证
|Type||Relevance / Equation question type / Validation
|-
|-
|示例||问题一(日期/时间,代码:DOB):你的出生日期是什么时候? <br />问题二(日期/时间,代码:datetoday):今天的日期是什么时候? <br />问题三(样板):你已经出生XXXX天。 <br />要做的事情:计算并显示问题1中给出的日期和问题2之间的天数。
|Example||Question one (date/time, code: DOB): What is your date of birth? <br /> Question two (date/time, code: datetoday): What is the date today? <br /> Question three (boilerplate): You are XXXX days old. <br /> To do: Calculate and display the number of days between the date given in question 1 and the question 2.
|-
|-
|表达式||{(strtotime(datetoday)-strtotime(DOB))/60/60/24}<br>
|Expression||{(strtotime(datetoday)-strtotime(DOB))/60/60/24}<br>
|-
|-
|说明||strtotime计算1970年1月1日到给定日期之间的秒数。上面的表达式计算两个给定日期之间的秒数。术语“/60/60/24”只计算从秒数开始的天数。 <br />您也可以使用strtotime('now')或简单的time()来直接返回从1970年1月到现在(即调查的时间)的秒数,而不是要求“datetoday”。因此,你可以用表达式{(time() - strtotime(DOB)) / 60 / 60 / 24 / 365.25}快速计算一个人年龄。在任何一种情况下,输入日期至关重要以适当的格式,否则strtotime()函数将无法正常工作。
|Description|| strtotime calculates the number of seconds between the 1st of January 1970 and the given date. The above expression calculates the number of seconds between the two given dates. The term "/60/60/24" just calculates the number of days from the number of seconds.  <br /> Instead of asking for the "datetoday", you can also use strtotime('now') or simply time(), which directly returns the number of seconds from January 1970 until now (i.e. the time the survey was taken).  Thus you could quickly figure out the age of a person in years with  the equation {(time() - strtotime(DOB)) / 60 / 60 / 24 / 365.25} In any of these cases, it is critical that the date is entered in a proper format, otherwise the strtotime() function will not work right. 
|-
|-
|步骤||创建两个日期问题(出生日期和今天的日期)和样板问题。 <br />在样板问题插页的问题文本中:“到{datetoday}为止,你已经出生 {(strtotime(today)-strtotime(dob))/60/60/24}天了。” <br /><br />请注意:函数strtotime可以使用多种但不是所有日期格式。如果遇到问题,请将调查的日期格式设置为mm/dd/yyyy或yyyy-mm-dd或dd.mm.yyyy
|Steps||Create two date questions (for the date of birth and for today's date) and a boilerplate question. <br /> In the question text of the boilerplate question insert: "On {datetoday} you were {(strtotime(today)-strtotime(dob))/60/60/24} days days old." <br /><br /> Please note: The function strtotime can work with MANY but not all date formats. If you run into problems set your survey's date format to mm/dd/yyyy or yyyy-mm-dd or dd.mm.yyyy"
|-
|-
|示例文件||[[Media:TestSurvey_Working_with_Dates.zip|Date_difference.zip]]
|Sample File||[[Media:TestSurvey_Working_with_Dates.zip|Date_difference.zip]]
|-
|-
|colspan="2" align="center"|[[File:Date_difference_example.png]]
|colspan="2" align="center"|[[File:Date_difference_example.png]]
|}
|}


==使用表达式管理器进行评估==
==Using ExpressionScript for Assessments==




下面是另一个关于如何将表达式管理器与 [[Question type - Array|阵列类型问题]]和评估结合使用的示例: </ br>
Here comes another example on how to use the ExpressionScript with [[Question type - Array|Array type questions]] and assessments:</ br>


假设你有2个数组问题,并且希望将计算数据的结果保存到数据库中。它实际上很简单,你需要创建并测试数组并提交虚拟响应以查看它是否有效并在完成的页面中提供结果。
Let's say you have 2 array questions and you want to save the results of the calculation data to your database. It is actually simple, you will need to create and test your arrays and submit a dummy response to see if it works and gives you the results in the completed page.




'''实现细节:'''
'''Implementation details:'''
*添加类型方程的问题
*add a question of type equation
*添加以下行,假设Q1是您的数组问题代码,而Q2是第二个:
*add the following line assuming that Q1 is your array question code while Q2 is the second one:
**<pre>{sum(Q1_SQ001.value,Q2_SQ001.value)}</pre>
** <pre>{sum(Q1_SQ001.value,Q2_SQ001.value)}</pre>


请注意,SQ001是任何子问题的默认代码。如果更改了子代码,请相应调整上面的等式。
Note that SQ001 is the default code for any subquestion. If you change the subquestion code, adjust the equation above accordingly.




==在数组中使用em_validation_q==
==Using em_validation_q in array==




你可以使用[[QS:Em_validation_q|问题验证方程]]来控制任何条件的阵列。
You can use the [[QS:Em_validation_q|question validation equation]] to control an array with any condition.




'''实现细节:'''
'''Implementation details:'''
*为第一个数组:单选数组
* for the first array : array of single choice
**问题代码是ARRAY
** Question code is ARRAY
**子问题代码是SQ01,SQ02,SQ03和SQ04
** Sub question code are SQ01,SQ02,SQ03 and SQ04
**更新整个问题验证方程并放入<pre>!is_empty(ARRAY_SQ01)!is_empty(ARRAY_SQ03) </pre>
** update the Whole question validation equation and put <pre>!is_empty(ARRAY_SQ01) and !is_empty(ARRAY_SQ03) </pre>
*为第二个数组:文本数组
* for the second array : array of text
**问题代码是ARRAYTEXT
** Question code is ARRAYTEXT
** Y轴代码的子问题是SY01,SY02,SY03和SY04
** Sub question at Y axis code are SY01,SY02,SY03, and SY04
**更新整个问题验证方程和放置<pre>count(self.sq_SY01 >= 1)和count(self.sq_SY03 >= 3)</pre>
** Update the Whole question validation equation and put <pre>count(self.sq_SY01 >= 1) and count(self.sq_SY03 >= 3)</pre>

Latest revision as of 21:45, 17 November 2021

Please help extending this article by adding your own examples. This will help others getting started using the Expression Script.


Structure

The first part of the article will describe what Expressions can be used for and where to find/add an Expression. The second part provides examples on how to use Expression.


Categories of Expressions

LimeSurvey uses Expressions for the following purposes:


Relevance

Purpose: Hiding question groups/questions

Relevance controls whether a question group or question is shown or hidden. If the result of the Expression is "true" (i.e. 1), the element will be shown. Otherwise (if the result of the Expression is "false" (i.e. 0)), the element will be hidden.

类型 显示/隐藏 在哪里找 屏幕截图
问题组相关性 问题组 创建/编辑问题组 - > “相关方程:”
问题相关性 问题 创建/编辑问题 - >“相关方程:”
子问题相关性 子问题 编辑子问题 - >“相关方程:”


Validation

Purpose: Validate input/actions on questions/subquestions/answers

Validation controls whether the respondent's input into a question or action is valid or not. If the result of the expression is "true" (i.e. 1), the "good" class will be applied to the element and validation message. Otherwise, if the result of the expression is "false" (i.e. 0)), the "error" class will be apllied to the element and validation message. You can declare a style for these classes in the template.css.

Type Validates Where to find Screenshot
Question Validation question Create/Edit question -> "Logic tab" -> "Question validation equation"
Subquestion Validation subquestions Create/Edit question -> "Logic tab" -> "Subquestion validation equation"

Text output

Purposes: (Do something) and write out some text

An expression can be used to write out some text. For example, you can test or calculate something and specify the output depending on the result of the test or calculation. It can also be used to insert the question text, answers, question types, ... of a question used before the Expression is used. Text output can be used everywhere where a text is shown and can return every kind of results.

Use Case Description
Micro tailoring Adjust a text, e.g., write "Mr" or "Mrs" depending on a gender question asked before
Calculation Calculate a value and write it out, e.g., "Your family income is xxx"
Reference Insert a text from an element, e.g., "Your name is xxx. How old are you?"


Answers storing

Purposes: (Do something,) (write out some text) and store something in the database

The Equation Question Type can be used to store something in the database. You can do something with an Expression, show the Equation question (which is similar to a text display) and store the result of the Expression in the database. It stores the answer into the responses table. Then, the result can be used within the statistics feature or it can also be imported into one of the formats provided by the LimeSurvey export functionality.

Examples for Expressions

Insert an answer from a previous question

Purpose Insert an answer from a previous question
Type Reference
Example Question one (question code "Q00"): What city do you live in?
Question two (question code "Q01") How long have you lived in CITY?
To do: The answer from question one should be used instead of "CITY" from the second question.
Expression {QOO}
Description The answer provided in Q00 replaces the Q00 field from the second question
Steps Create/Edit question two
Insert "How long have you lived in {QOO} (years)?" into the question text field

Using hidden equation question to autofill an answer

Problem: Imagine you have two questions Q1 and Q2. Q1 asks respondents for their age. Q2 divides respondents into three groups: age is below 20 years, age is 20, age is greater than 20. So, Q2 should make use of the values "1", "2", "3", which correspond to the above conditions. Also, we should not forget to make Q2 invisible (the question will not be displayed in the survey, but the values will be via the "background process" within the responses table).

To fill in the database using the Equation question type:

  • First, create a question with code Q1 as a numerical input question.
  • Then create another question with code Q2 as an equation question.
  • In the "Display settings" tab chapter of Q2:
    • set "Always hide this question" field as "On"
    • type in "Equation" field the expression:
{if(Q1.NAOK < 20, "1", if(Q1.NAOK > 20, "3", "2"))}

Create a summary page using placeholders

This tutorial demonstrates how you can create an overview at the end of the survey, listing all questions and answers via ExpressionScript placeholders. It also shows how to limit such an overview to answered questions only.

Our example can be downloaded from here: Survey placeholders survey example.


Question types being used here:

1. Single choice questions / single text/numeric questions

  • List (dropdown)
  • List (radio) [L]
  • Yes/No [Y]
  • Long free text [T]
  • Short free text [S]
  • Numerical input [N]
  • Equation [*]


2. Question with subquestions

  • Multiple short text [Q]
  • Multiple choice [M]
  • Array [F]
  • Array (Yes/No/Uncertain) [C]
  • Array (10 point choice) [B]


3. Questions with 2 scales

  • Array dual scale [1]


4. Questions with X and Y scales

  • Array (Numbers) [:]


5. Mask questions

  • Text display [X]


In this simple example, our overview will be a simple list with:

- Question text: User's answer


Single choice questions / single text/numeric questions

For all these question types, Limesurvey will store a single response:

  • List (dropdown)
  • List (radio)
  • List with comment
  • 5 point choice
  • Yes/No
  • Short free text
  • Long free text
  • Huge free text
  • Numerical input
  • Equation
  • Date
  • Gender

Assuming the question code of the question is q1, we can reference the question text and answer using:

- {q1.question}: {q1.shown}

Example:

- How old are you?: 25


If you want to check whether that question was answered you can put an IF statement around your output:

{if(!is_empty(q1),join("- ",q1.question,": ",q1.shown),"")}

This "translates" to: IF question with code q1 is not empty, output "- " AND the question text AND ": " AND the answer text (the join() function joins elements as a new string; ELSE: output nothing ("" means empty string).

Question with subquestions

For all these question types LimeSurvey uses subquestions:

  • Multiple short text
  • Multiple choice
  • Multiple choice with comments
  • Array
  • Array (5 point choice)
  • Array (10 point choice)
  • Array (Yes/No/Uncertain)
  • Array (Increase/Same/Decrease)
  • Array by column

Assuming the question code of the question is q2 and the subquestions are numbered SQ001, SQ002, (auto-numbering done by LimeSurvey), we can reference the question text and answer using:

- {q2_SQ001.question}: {q2_SQ001.shown}

- {q2_SQ002.question}: {q2_SQ002.shown}

For multiple choice questions it makes sense to now show the text of the ticked subquestion but to show a Y for each selected option:

- {q2_SQ001.question}: {q2_SQ001}

- {q2_SQ002.question}: {q2_SQ002}

...

Note that currently it is not possible to output the question text of questions with subquestions, see this feature request.


Example 1 (assuming question type is multiple choice)

- Do you know these car brands?

-- Mercedes: Y

-- Audi: N

-- Volvo: Y


If you want to output checked items only you can put an IF statement around your output and check for the checkbox value:

{ if( q2_SQ001=="Y", join( "- ", q2_SQ001.question, ": ", q2_SQ001.shown ), "" ) }


Use the listifop function if you want to output a list of only the selected items, eg: Mercedes, Volvo

{ listifop( 'value', '==', 'Y', 'question', ', ', that.q2.sgqa ) }


Where  that.q2  expands to all of q2's subquestions. See self, this and that for more detail.


Example 2 (assuming question type is Array (10 point choice))

- Please rate the quality of the car brands below on a scale from 1=very bad to 10=very good?

-- Mercedes: 7

-- Audi: 9

-- Volvo: 9


If you want to output rated items only you can put an IF statement around your output and check if the current sub question was answered by using:

{ if( ! is_empty( q3_SQ001 ), join( "- ", q3_SQ001.question, ": ", q3_SQ001.shown ), "" ) }


Use the listifop function if for example you want to only output a list of the selected items greater than eight, eg: Audi / Volvo

{ listifop( 'value', '>', 8, 'question', ' / ', that.q3.sgqa ) }


Question with two scales

This applies to the dual scale question type only.


Let's assume that the following codes are being used:

  • Question code: q4
  • Subquestion codes: SQ001, SQ002, ...
  • Answer codes scale 1: A1, A2, ...
  • Answer codes scale 2: B1, B2, ...


To refer to the result of a certain subquestion on a certain scale, you need to use QuestionCode . '_' . SubQuestionCode . '_' . ScaleID. Note that "ScaleID" is 0 for the first scale and 1 for the other!
Example for referring to the answer of the third subquestion and second scale: q4_SQ003_1 - Question 4, subquestion 3, second scale.


Let's output the results for both scales and the first two subquestions:

- {q4_SQ001_0.question}: {q4_SQ001_0.shown} / {q4_SQ001_1.shown}

- {q4_SQ002_0.question}: {q4_SQ002_0.shown} / {q4_SQ002_1.shown}

To output the subquestion text, you need to add the scale ID to the placeholder (though the texts are the same for both scales). So instead of {q4_SQ001.question} we have to use {q4_SQ001_0.question} or {q4_SQ001_1.question}.

If you want to export/display the results of subquestions with at least one answer only, use this syntax for each subquestion:

{if(count(q4_SQ001_0,q4_SQ001_1)>0,join("- ",q4_SQ001_0.question,": ",q4_SQ001_0.shown," / ",q4_SQ001_1.shown),"")}

Question with X and Y scales

This applies to all matrix questions which allow an answer for every cell (not just every row as shown previously):

  • Array Texts
  • Array Numbers


Assumed codes used:

  • Question code: q5
  • Subquestion codes: SQ001, SQ002, ...
  • Answer codes: A1, A2, ...

To refer to the result of a certain subquestion from a certain column, you need to use QuestionCode . '_' . SubQuestionCode . '_' . AnswerCode. Example for referring to the answer of the third subquestion and second column: q5_SQ003_A2.

Let's output the results for columns 1-3 of the first two subquestions:

- {q5_SQ001_A1.question}: {q5_SQ001_A1.shown} | {q5_SQ001_A2.shown} | {q5_SQ001_A3.shown}

- {q5_SQ002_A1.question}: {q5_SQ002_A1.shown} | {q5_SQ002_A2.shown} | {q5_SQ002_A3.shown}


Since for these question types each cell (combination of X axes and Y axes) equals one answer option, a test for existing data needs to be done for each cell. Example:

{if(!is_empty(q5_SQ001_A1),join("- ",q5_SQ001_A1.question,": ",q5_SQ001_A1.shown),"")}


To output the subquestion text you need to add the answer code to the placeholder (though the texts are the same for all answer options). So instead of {q5_SQ001.question} we have to use {q5_SQ001_A1.question} or {q5_SQ001_A2.question}.

Hide question if answer from previous question is empty

Purpose Hide question if answer from previous question is empty
Type Question Relevance
Example Question one: question code "name", question text "What's your name?"
Question two: question text "{name}, how old are you?"
To do: Hide question two if the textfield of question one is empty
Expression !is_empty(name)
Description is_empty() determines whether a variable is considered to be empty. The "!" negates the result. So if the variable is not empty the Expression will be true and the question is shown
Steps Create/edit question two
Insert "!is_empty(name)" into "Relevance equation:"
Sample File Hide_question_if_empty_question_group.zip


Hide question group if answer from previous question is Yes or No

Purpose Hide question group if answer from previous question is Yes or No
Type Question group Relevance
Example Page one, Question Group one, Question one: question code "PET", question text "Do you have a pet?" -Yes -No
Page two, Question Group two: Title "About your pet(s)"
To do: Show/Hide question group two if answer from question one is Yes/No
Expression PET == "Y"
Description PET is the question code for the question you want to check the answer. If you don't use a suffix EM will use "Qcode.code". So you compare the answer code from the PET question to the value "Y". If the participant answers "Yes" the Expression is true and the question group "About your pet(s)" will be shown.
Steps Create/edit question group two
Insert "PET == "Y"" into "Relevance equation:"
Sample File Hide_question group_if_answer_from_previous_question_is_Yes_or_No.zip


Display value of a multiple answer input question field

Purpose Display value of a multiple answer input question field
Type Reference
Example Question one: question code "AskChildAge", question text "How old are your children?". Subquestions codes -Child1 -Child2 -Child3 - ChildXXX
Question two: question code "ReportChildAge" question text "About your first child: - CHILD1 is AGE1."
Expression {AskChildAge_Child1.question}, {AskChildAge_Child1.value}
Description You want to use the value of a subquestion in a following question. You can access the subquestion value with this kind of expression: QcodeQuestion_QcodeSubquestion.value
Steps Create/edit the second question
Insert this text in the description: "About your first child: - {AskChildAge_Child1.question} is {AskChildAge_Child1.value}."
Sample File Display_value_of_a_multiple_answer_input_question_field.zip

Validate number of boxes ticked per row for an "Array (Numbers) Checkbox" question

Purpose Validate number of boxes ticked per row
Type Validation
Example Question of type "Array Numbers (Checkbox Layout)"
Expression sum(...)
Description (sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X) determines whether at least X checkboxes in row A are checked.
If you want to check all three rows (A, B, C), you can connect the expression using "&&":
(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X)
&& (sum(Test_B_1, Test_B_2, Test_B_3, Test_B_4, Test_B_5) <= X)
&& (sum(Test_C_1, Test_C_2, Test_C_3, Test_C_4, Test_C_5) <= X)
Steps Create/edit question of type "Array Numbers (Checkbox Layout)".
Insert the above expression into "Question validation equation" at the advanced question settings (you might have to adjust variable namings!).
Sample File Validate number of boxes ticker per row for an Array (Numbers) Checkbox.lss

Calculate difference between two dates

Purpose Calculate difference between two dates
Type Relevance / Equation question type / Validation
Example Question one (date/time, code: DOB): What is your date of birth?
Question two (date/time, code: datetoday): What is the date today?
Question three (boilerplate): You are XXXX days old.
To do: Calculate and display the number of days between the date given in question 1 and the question 2.
Expression {(strtotime(datetoday)-strtotime(DOB))/60/60/24}
Description strtotime calculates the number of seconds between the 1st of January 1970 and the given date. The above expression calculates the number of seconds between the two given dates. The term "/60/60/24" just calculates the number of days from the number of seconds.
Instead of asking for the "datetoday", you can also use strtotime('now') or simply time(), which directly returns the number of seconds from January 1970 until now (i.e. the time the survey was taken). Thus you could quickly figure out the age of a person in years with the equation {(time() - strtotime(DOB)) / 60 / 60 / 24 / 365.25} In any of these cases, it is critical that the date is entered in a proper format, otherwise the strtotime() function will not work right.
Steps Create two date questions (for the date of birth and for today's date) and a boilerplate question.
In the question text of the boilerplate question insert: "On {datetoday} you were {(strtotime(today)-strtotime(dob))/60/60/24} days days old."

Please note: The function strtotime can work with MANY but not all date formats. If you run into problems set your survey's date format to mm/dd/yyyy or yyyy-mm-dd or dd.mm.yyyy"
Sample File Date_difference.zip

Using ExpressionScript for Assessments

Here comes another example on how to use the ExpressionScript with Array type questions and assessments:</ br>

Let's say you have 2 array questions and you want to save the results of the calculation data to your database. It is actually simple, you will need to create and test your arrays and submit a dummy response to see if it works and gives you the results in the completed page.


Implementation details:

  • add a question of type equation
  • add the following line assuming that Q1 is your array question code while Q2 is the second one:
    • {sum(Q1_SQ001.value,Q2_SQ001.value)}

Note that SQ001 is the default code for any subquestion. If you change the subquestion code, adjust the equation above accordingly.


Using em_validation_q in array

You can use the question validation equation to control an array with any condition.


Implementation details:

  • for the first array : array of single choice
    • Question code is ARRAY
    • Sub question code are SQ01,SQ02,SQ03 and SQ04
    • update the Whole question validation equation and put
      !is_empty(ARRAY_SQ01) and !is_empty(ARRAY_SQ03) 
  • for the second array : array of text
    • Question code is ARRAYTEXT
    • Sub question at Y axis code are SY01,SY02,SY03, and SY04
    • Update the Whole question validation equation and put
      count(self.sq_SY01 >= 1) and count(self.sq_SY03 >= 3)