ExpressionScript examples/zh: Difference between revisions
From LimeSurvey Manual
Created page with "===有两个尺度的问题===" |
Created page with "{{Note|这仅适用于双尺度问题类型。}}" |
||
Line 265: | Line 265: | ||
{{Note| | {{Note|这仅适用于双尺度问题类型。}} | ||
Revision as of 07:58, 21 November 2018
结构
本文的第一部分将介绍可以使用哪些表达式以及在何处查找/添加表达式。第二部分提供了有关如何使用表达式的示例。
表达式的类别
LimeSurvey使用表达式用于以下目的:
相关性
目的:隐藏问题组/问题
相关性控制是显示还是隐藏问题组或问题。如果表达式的结果为“真”(即1),则将显示该元素。否则(如果表达式的结果为“假”(即0)),则元素将被隐藏。
类型 | 显示/隐藏 | 在哪里找 | 屏幕截图 |
---|---|---|---|
问题组相关性 | 问题组 | 创建/编辑问题组 - > “相关方程:” | ![]() |
问题相关性 | 问题 | 创建/编辑问题 - >“相关方程:” | ![]() |
子问题相关性 | 子问题 | 编辑子问题 - >“相关方程:” | ![]() |
验证
目的:验证问题/子问题/答案的输入/操作
验证控制受访者对问题或行动的输入是否有效。如果表达式的结果为“true”(即1),则“good”类将应用于元素和验证消息。否则,如果表达式的结果是“假”(即0)),则“错误”类将被用于元素和验证消息。您可以在template.css中为这些类声明样式。
类型 | 验证 | 在哪里找 | 屏幕截图 |
---|---|---|---|
问题验证 | 问题 | 创建/编辑问题 - >“逻辑选项卡” - >“问题验证方程式” | ![]() |
子问题验证 | 子问题 | 创建/编辑问题 - >“逻辑选项卡” - >“子问题验证方程“ | 30px]] |
文本输出
目的:(做点什么)并写一些文本
表达式可用于写出一些文本。例如,你可以测试或计算某些内容,并根据测试或计算的结果指定输出。它还可用于插入使用表达式之前使用的问题的问题文本、答案、问题类型等。文本输出可以在显示文本的任何地方使用,并且可以返回各种结果。
用例 | 说明 |
---|---|
微剪裁 | 调整文字,例如根据之前提出的性别问题写“先生”或“女士” |
计算 | 计算一个值并将其写出来,例如“你的家庭收入是xxx” |
参考 | 插入元素中的文本,例如, “你的名字是xxx。你多大了?” |
答案存储
目的:(做点什么)(写一些文本)并在数据库中存储一些东西
方程式问题类型 可用于在数据库中存储内容。你可以使用方程式执行某些操作,显示方程式问题(类似于文本显示)并将表达式的结果存储在数据库中。它将答案存储在响应表中。然后,结果可以在统计功能中使用,也可以导入到LimeSurvey导出功能提供的格式之一。
表达式的例子
插入上一个问题的答案
目的 | 插入上一个问题的答案 |
类型 | 参考 |
示例 | 问题一(问题代码“ Q00“):你住在哪个城市? 问题二(问题代码“Q01”)你在CITY住多久了? 要做的:第二个问题应该使用问题1的答案而不是“CITY”。 |
表达式 | {QOO} |
说明 | Q00中提供的答案取代了第二个问题的Q00字段 |
步骤 | 创建/编辑问题二 插入“你在{QOO}生活多久了?”进入问题文本字段 |
![]() |
使用隐藏的方程式问题自动填充答案
问题: 想象一下你有两个问题Q1和Q2。 Q1询问受访者的年龄。 Q2将受访者分为三组:年龄低于20岁,年龄为20岁,年龄大于20。 因此,Q2应使用值“1”,“2”,“3”,这对应于以上条件。此外,我们不应忘记使Q2不可见(问题不会显示在调查中,但值将通过响应表中的“后台进程”)。
使用公式问题类型填写数据库:
- 首先,创建一个问题,将代码Q1作为数字输入问题。
- 然后用代码Q2创建另一个问题作为方程问题。
- 在Q2的“显示设置”选项卡章节中:
- 将“始终隐藏此问题”字段设置为“开”
- 在“公式”字段中输入表达式:
Q2=if(Q1.NAOK < 20, "1", if(Q1.NAOK > 20, "3", "2"))}
使用占位符创建摘要页面
本教程演示了如何在调查结束时创建概述,通过Expression Manager placeholders列出所有问题和答案。 它还说明了如何限制这样的概述仅回答问题。
我们的示例可以从这里下载:调查占位符调查示例。
这里使用的问题类型:
1. 单选题/单文/数字问题
- 列表(下拉)
- 列表(单选)[L]
- 是/否[Y]
- 长自由文本[T]
- 短自由文本[S]
- 数值输入[N]
- 方程式[*]
2. 子问题的问题
- 多选题短文本[Q]
- 多选题[M]
- 阵列[F]
- 阵列(是/否/不确定)[C]
- 阵列(10分选择)[B]
3. 有2个尺度的问题
- 阵列双尺度[1]
4 . X和Y的问题缩放
- 阵列(数字)[:]
5. 掩码问题
- 文本显示[X]
在这个简单的例子中,我们的概述将是一个简单的列表:
- 问题文本:用户的答案
单选题/单文本/数字问题
对于所有这些问题类型,Limesurvey将存储一个响应:
- 列表(下拉)
- 列表(单选)
- 带评论的列表
- 5分选择
- 是/否
- 短自由文本
- 长自由文本
- 超长自由文本
- 数字输入
- 等式
- 日期
- 性别
假设问题的问题代码是q1,我们可以使用以下内容引用问题文本和答案:
- {q1.question}: {q1.shown}
例:
- 你多大了?:25
如果要检查是否已回答该问题,可以在输出周围放置IF语句:
{if(!is_empty(q1),join("- ",q1.question,": ",q1.shown),"")}
这个“翻译”为:IF 代码q1的问题不为空,输出“ - ” AND 问题文本 AND “:” AND 答案文本(join()函数将元素作为新字符串连接); ELSE:不输出任何内容(“ “表示空字符串)。
子问题
对于所有这些问题类型,LimeSurvey使用子问题:
- 多选题短文本
- 多选题
- 有评论的多选题
- 阵列
- 阵列(5分选择)
- 阵列(10分选择)
- 阵列(是/ 否 / 不确定)
- 阵列(增加,不变,减少)
- 按列阵列
假设问题的问题代码是q2并且子问题编号为SQ001,SQ002(由LimeSurvey完成的自动编号),我们可以使用以下内容引用问题文本和答案:
- {q2_SQ001.question}: {q2_SQ001.shown}
- {q2_SQ002.question}: {q2_SQ002.shown}
对于多选题,现在显示勾选的子项的文本,但为每个选定的选项显示Y是有意义的:
- {q2_SQ001.question}: {q2_SQ001}
- {q2_SQ002.question}: {q2_SQ002}
...
请注意,目前无法输出带子问题问题的文本,请参阅this feature request。
示例1(假设问题类型是多选题)
- 你知道这些汽车品牌吗?
-- Mercedes: Y
-- Audi: N
如果只想输出选中的项目,可以在输出周围放置一个IF语句并检查复选框值:
{if(q2_SQ001=="Y",join("- ",q2_SQ001.question,": ",q2_SQ001.shown),"")}
示例2(假设问题类型是阵列(10点选择))
- 请评价以下汽车品牌的质量,从1 =非常差到10 =非常好?
-- Mercedes: 7
-- Audi: 9
如果您只想输出评级项目,可以在输出周围放置一个IF语句,并使用以下方法检查当前子问题是否得到了解答:
{if(!is_empty(q3_SQ001),join("- ",q3_SQ001.question,": ",q3_SQ001.shown),"")}
有两个尺度的问题
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, ...

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),"")}

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 Expression Manager for Assessments
Here comes another example on how to use the Expression Manager 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)