ExpressionScript How-tos/ja: Difference between revisions
From LimeSurvey Manual
Bravehorse (talk | contribs) (Created page with "子どもの数を1に変更すると、同じページ上にあってもこのように表示が即座に変わります。") |
(Updating to match new version of source page) |
||
Line 1: | Line 1: | ||
<languages /> | |||
__TOC__ | __TOC__ | ||
= | =Overview of what has been previously explained= | ||
If you have followed so far the structure of the LimeSurvey manual, it means that you already the [[Expression_Manager_-_presentation#Key_Definitions|LimeSurvey terminology]]. Next, we covered expressions and variables in order to learn how to enhance our surveys. In the next part, we moved to the types and meanings of [[Expression_Manager_-_presentation#Syntax Highlighting|syntax highlighting]] to learn how to correct syntax errors. | |||
With these basic notions covered, we could move towards examples and explaining how expressions work within LimeSurvey. | |||
==例== | |||
= | |||
== | |||
The following screenshots give examples, | The following screenshots give examples, but do not show the values of the tooltips. A tooltip is an informative box which is displayed whenever you hover the mouse over any bold colored words. | ||
Because of this syntax highlighting, it is very easy to compose correct expressions, even the ones that are complicated. Although the LimeSurvey team plans to try to build an Expression Builder GUI, you can use the existing syntax-highlighting to quickly identify and fix typos. You can also use the tooltips to validate the accuracy of your expressions (e.g., confirm you have selected the desired variable(s)). | |||
In each of the examples, there are three columns: | In each of the examples, there are three columns: | ||
#Source - this is the raw text that you would enter into the LimeSurvey | #Source - this is the raw text that you would enter into the LimeSurvey question field | ||
#Pretty Print - this is the syntax-highlighted equivalent of what you entered | #Pretty Print - this is the syntax-highlighted equivalent of what you entered | ||
#*Note that Expressions are shown with a tan background, but not surrounded by curly braces in this highlighting. | #*Note that Expressions are shown with a tan background, but not surrounded by curly braces in this highlighting. | ||
Line 84: | Line 27: | ||
#Result - this is the output generated when EM processes the source | #Result - this is the output generated when EM processes the source | ||
#*Everything that can be properly substituted is | #*Everything that can be properly substituted is | ||
#*Expressions with errors are shown in-line, with syntax highlighting. | #*Expressions with errors are shown in-line, with syntax highlighting. Errors are surrounded by a red-lined box. | ||
===適切な構文=== | ===適切な構文=== | ||
#Values: shows that known variables are color coded according to whether are set on the current page. | You may find below examples of proper syntax: | ||
#Values: shows that known variables are color coded according to whether are set on the current page. Old-style INSERTANS:xxxx gets its own color-coding style | |||
#Question Attributes: shows that dot notation can access some properties of questions | #Question Attributes: shows that dot notation can access some properties of questions | ||
#Math: shows that basic and complex calculations are supported | #Math: shows that basic and complex calculations are supported | ||
#TextProcessing: shows some of the available text-processing functions | #TextProcessing: shows some of the available text-processing functions | ||
#Dates: shows two of the available date-related functions | #Dates: shows two of the available date-related functions | ||
#Conditional: shows | #Conditional: shows the usage of the if() function. The choices can be nested. | ||
#Tailored | #Tailored paragraph: you can completely customize a report based upon prior values | ||
#EM processes within strings: | #EM processes within strings: shows that it can do substitutions within strings. This example generates a tailored image name. | ||
#EM doesn't process curly braces like these: | #EM doesn't process curly braces like these: shows that if the curly braces are escaped, or there is a white space between the expression and the curly braces, EM ignores the expression. | ||
[[File:em-syntax-highlighting-1-v2.jpg]] | [[File:em-syntax-highlighting-1-v2.jpg]] | ||
===エラーを含むEM構文=== | ===エラーを含むEM構文=== | ||
Here are examples of common errors when typing EM expressions. | |||
Here are examples of common errors when typing EM expressions. Note that the tooltips provide additional information. | |||
#Inline Javascript that forgot to add spaces after curly brace | #Inline Javascript that forgot to add spaces after curly brace | ||
#*Since "document.write" appears right after a curly brace, EM thinks it is an expression, and red-boxes "document" and "write" since they are undefined variable and functions, respectively | #*Since "document.write" appears right after a curly brace, EM thinks it is an expression, and red-boxes "document" and "write" since they are undefined variable and functions, respectively | ||
#Unknown/ | #Unknown/Misspelled variables, functions and operators | ||
#*Here we forgot that we are using the variable name "gender" instead of "sex", but EM catches that error. | #*Here we forgot that we are using the variable name "gender" instead of "sex", but EM catches that error. It also red-boxes '++', since that is not a supported operator. | ||
#Warns if use = instead of eq, or perform value assignments | #Warns if use = instead of eq, or perform value assignments | ||
#*Note that the '=' and '+=' are in red text instead of black. | #*Note that the '=' and '+=' are in red text instead of black. If you hover the mouse over them, you will see warnings that you are assigning a value. | ||
#Wrong number of arguments for functions | #Wrong number of arguments for functions | ||
#*if() takes 3 arguments, but has been given 4, so hovering over the red-boxed "if" will explain the error and show the supported syntax | #*if() takes 3 arguments, but it has been given 4, so hovering over the red-boxed "if" will explain the error and show the supported syntax | ||
#*sum() takes an unlimited number of arguments, but we had a trailing comma before the closing parentheses, so that is red-boxed | #*sum() takes an unlimited number of arguments, but we had a trailing comma before the closing parentheses, so that is red-boxed | ||
#Mismatched parentheses | #Mismatched parentheses | ||
Line 120: | Line 67: | ||
#*If you use an operator or punctuation that EM does not support, it will red-box it. | #*If you use an operator or punctuation that EM does not support, it will red-box it. | ||
#Invalid assignments | #Invalid assignments | ||
#*Some variables are readWrite and can have their values changed. Others are read-only | #*Some variables are readWrite and can have their values changed. Others are read-only. | ||
#*If you try to change the value of a read-only variable, you can't. | #*If you try to change the value of a read-only variable, you can't. EM will red-box the attempt. | ||
#*If you try to assign a value to an equation or a string, you will also get an error | #*If you try to assign a value to an equation or a string, you will also get an error | ||
[[File:em-syntax-highlighting-errors-v2.jpg]] | [[File:em-syntax-highlighting-errors-v2.jpg]] | ||
=="Live" examples of Syntax Highlighting with active tooltips== | |||
<table border="1"> | <table border="1"> | ||
<tr><th>Source</th><th>Pretty Print</th><th>Result</th></tr><tr><td><b>Here is an example of OK syntax with tooltips</b><br>Hello {if(gender=='M','Mr.','Mrs.')} {surname}, it is now {date('g:i a',time())}. Do you know where your {sum(numPets,numKids)} chidren and pets are? | <tr><th>Source</th><th>Pretty Print</th><th>Result</th></tr><tr><td><b>Here is an example of OK syntax with tooltips</b><br>Hello {if(gender=='M','Mr.','Mrs.')} {surname}, it is now {date('g:i a',time())}. Do you know where your {sum(numPets,numKids)} chidren and pets are? | ||
Line 149: | Line 99: | ||
</td></tr> | </td></tr> | ||
</table> | </table> | ||
=テーラリングの例({INSERTANS:xxx}の拡張)= | =テーラリングの例({INSERTANS:xxx}の拡張)= | ||
=="Dear {Mr}/{Mrs} Smith..."== | =="Dear {Mr}/{Mrs} Smith..."== | ||
'Mr.'か'Mrs.'を使い分けるにはif()関数を使用します。 | 'Mr.'か'Mrs.'を使い分けるにはif()関数を使用します。 | ||
Line 167: | Line 120: | ||
| | | | ||
|} | |} | ||
[[File:em-tailoring-q-ex1.jpg]] | [[File:em-tailoring-q-ex1.jpg]] | ||
使用ビュー: | 使用ビュー: | ||
[[File:em-tailoring-q-ex1-usage.jpg]] | [[File:em-tailoring-q-ex1-usage.jpg]] | ||
==案内メールにおける"Dear {Mr}/{Mrs} Smith..."== | ==案内メールにおける"Dear {Mr}/{Mrs} Smith..."== | ||
トークンテーブルの属性を使用して、上記の例を案内メールに使用できます。電子メールの中で'Mr.'または'Mrs.'を使い分けるにはif()関数を使用します。 | トークンテーブルの属性を使用して、上記の例を案内メールに使用できます。電子メールの中で'Mr.'または'Mrs.'を使い分けるにはif()関数を使用します。 | ||
構文は、if(test,do_if_true,do_if_false)です。 | 構文は、if(test,do_if_true,do_if_false)です。 | ||
Line 191: | Line 150: | ||
| | | | ||
|} | |} | ||
案内メールのテキスト: | 案内メールのテキスト: | ||
Line 203: | Line 163: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[File:emEx1mail.jpg]] | [[File:emEx1mail.jpg]] | ||
電子メールビュー: | 電子メールビュー: | ||
[[File:emEx1mailview.jpg]] | [[File:emEx1mailview.jpg]] | ||
=計算 / 評価の例= | =計算 / 評価の例= | ||
==実行時に評価値を計算し、その結果をアンケートデータに格納する== | ==実行時に評価値を計算し、その結果をアンケートデータに格納する== | ||
この例では、EMのすべての機能(出現条件、文言調整、式の質問タイプなど)を使用しています。 | この例では、EMのすべての機能(出現条件、文言調整、式の質問タイプなど)を使用しています。 | ||
Line 237: | Line 203: | ||
| | | | ||
|} | |} | ||
To download this example, please click on the following link: [[Media:Assessments_survey_example.zip|Assessments_survey_example]]. | |||
代表的な質問のスクリーンショットです。ご覧のとおり、EM構文では、文言調整を含んでいそうなすべてのフィールドが強調表示されます。ここでは、出現条件、式の質問タイプ、および質問内の置換の例を示します。ヘルプ、グループヘッダーの表示、ようこそメッセージ、および終了メッセージ内で置き換えることもできます。 | 代表的な質問のスクリーンショットです。ご覧のとおり、EM構文では、文言調整を含んでいそうなすべてのフィールドが強調表示されます。ここでは、出現条件、式の質問タイプ、および質問内の置換の例を示します。ヘルプ、グループヘッダーの表示、ようこそメッセージ、および終了メッセージ内で置き換えることもできます。 | ||
この質問では、出現条件は{numKids >= 2}であるため、回答者が少なくとも2人の子供がいると回答した場合にのみ表示されます。 | この質問では、出現条件は{numKids >= 2}であるため、回答者が少なくとも2人の子供がいると回答した場合にのみ表示されます。 | ||
[[File:em-tailoring2-q-kid2.jpg]] | [[File:em-tailoring2-q-kid2.jpg]] | ||
また、各変数に.NAOK接尾辞を使用することに注意してください。これは、EMが入れ子の出現条件を実現するためのものです。もし.NAOKをつけない場合持、4人の子供を持っていると回答した場合にのみ合計が計算されることになります(すべての変数が真の場合)。.NAOKを使用すると、変数のすべてまたは一部が偽であっても合計を計算することを意味します(すなわち"該当なし"(Not Applicable - NA)でも構わないということです)。 | また、各変数に.NAOK接尾辞を使用することに注意してください。これは、EMが入れ子の出現条件を実現するためのものです。もし.NAOKをつけない場合持、4人の子供を持っていると回答した場合にのみ合計が計算されることになります(すべての変数が真の場合)。.NAOKを使用すると、変数のすべてまたは一部が偽であっても合計を計算することを意味します(すなわち"該当なし"(Not Applicable - NA)でも構わないということです)。 | ||
ただし、.NAOK属性は変数がEMに渡されるかどうかにのみ影響します。その人が最初に3人の子供がいると回答し、それぞれの年齢を入力した後、気が変わり、2人であると答えた場合、3つの入力値の合計は必要ありません。次の例で確認してください。 | ただし、.NAOK属性は変数がEMに渡されるかどうかにのみ影響します。その人が最初に3人の子供がいると回答し、それぞれの年齢を入力した後、気が変わり、2人であると答えた場合、3つの入力値の合計は必要ありません。次の例で確認してください。 | ||
[[File:em-tailoring2-q-sumKidAges.jpg]] | [[File:em-tailoring2-q-sumKidAges.jpg]] | ||
それぞれの式は、黄褐色の背景で色分けされています。ご覧の通り、3つの別々の式があります。最後の式には、2人以上の子供がいる場合にのみ表示されるメッセージが入っています。 | それぞれの式は、黄褐色の背景で色分けされています。ご覧の通り、3つの別々の式があります。最後の式には、2人以上の子供がいる場合にのみ表示されるメッセージが入っています。 | ||
[[File:em-tailoring2-q-kidSummary.jpg]] | [[File:em-tailoring2-q-kidSummary.jpg]] | ||
ここでは、実際のアンケートのスクリーンショットを示します。 | ここでは、実際のアンケートのスクリーンショットを示します。 | ||
最初にページにアクセスすると、これが表示されます。"0人の'''子ども'''がいます"ではなく、"0人の'''子どもたち'''がいます"となっていることに注意してください。 | 最初にページにアクセスすると、これが表示されます。"0人の'''子ども'''がいます"ではなく、"0人の'''子どもたち'''がいます"となっていることに注意してください。 | ||
[[File:em-tailoring2-s-0kids.jpg]] | [[File:em-tailoring2-s-0kids.jpg]] | ||
子どもの数を1に変更すると、同じページ上にあってもこのように表示が即座に変わります。 | 子どもの数を1に変更すると、同じページ上にあってもこのように表示が即座に変わります。 | ||
[[File:em-tailoring2-s-1kid.jpg]] | [[File:em-tailoring2-s-1kid.jpg]] | ||
Now notice that the grammar is correct: "You have 1 '''child'''". | |||
Now I change the value for number of children to 3, and the display instantly changes to this. | Now I change the value for number of children to 3, and the display instantly changes to this. | ||
Notice that you now see the conditional message at the bottom: "The sum of ages of your first 3 kids is 0.". | Notice that you now see the conditional message at the bottom: "The sum of ages of your first 3 kids is 0.". | ||
[[File:em-tailoring2-s-3kids.jpg]] | [[File:em-tailoring2-s-3kids.jpg]] | ||
Now I'll enter ages for my imaginary children, and I get this display, summing their ages. | |||
Now I'll enter ages for my imaginary children, and I get this display, summing up their ages: | |||
[[File:em-tailoring2-s-3kids-sum.jpg]] | |||
Again, the score and display updates instantly as I enter the values, so you can use this to show a running total of an Assessment Score. | Again, the score and display updates instantly as I enter the values, so you can use this to show a running total of an Assessment Score. | ||
Now, I change the value for the number of children to 2 | Now, I change the value for the number of children to 2. The display has changed to this: | ||
[[File:em-tailoring2-s-2kids-sum.jpg]] | |||
Notice that although I had entered a value of 5.5 for the third child, the report now only sums the values of my first 2 children. | Notice that although I had entered a value of 5.5 for the third child, the report now only sums the values of my first 2 children. | ||
Line 286: | Line 273: | ||
The reason for this is that the 3rd value is now irrelevant, and irrelevant values are actively ignored by EM. | The reason for this is that the 3rd value is now irrelevant, and irrelevant values are actively ignored by EM. | ||
If I were to change the number of kids back to 3, I would see the value of 5.5 I entered again. So, I don't lose any information I enter on the page. | |||
However, if I navigate to the Next or Previous page, all irrelevant values will be NULLed out in the session and in the database. So, if I were to keep the value at 2, go to the next page, and then come back and state that I actually have 3 kids, I would no longer see the age of 5.5. | |||
==Enter data and see a dynamically changing report of what was entered on the same page== | ==Enter data and see a dynamically changing report of what was entered on the same page== | ||
This example presents the Tailoring process within LimeSurvey. | |||
To download this example, click on the following link: [[Media:Dynamic_change_of_the_report_when_data_is_entered_on_the_same_page_survey.zip|Dynamic changes survey example]]. | |||
Here is what the page looks like initially. You only see the question asking what city you live in: | |||
[[File:em-on_page_report-usage-1.jpg]] | [[File:em-on_page_report-usage-1.jpg]] | ||
Once you start to enter an answer, the | |||
Once you start to enter an answer, the tailoring process is also starting: | |||
[[File:em-on_page_report-usage-2.jpg]] | [[File:em-on_page_report-usage-2.jpg]] | ||
As you enter answers, the table at the bottom of the page is updated to show the answer codes and values of your responses. | As you enter answers, the table at the bottom of the page is updated to show the answer codes and values of your responses. | ||
[[File:em-on_page_report-usage-3.jpg]] | [[File:em-on_page_report-usage-3.jpg]] | ||
=Common Debugging Examples= | |||
= | ==Nested if() Statements (Conditional Logic)== | ||
EM supports the function if(test,do_if_true,do_if_false) so that you can perform conditional logic or tailoring. | EM supports the function "if(test,do_if_true,do_if_false)" so that you can perform conditional logic or tailoring. This function can be nested to do the equivalent of "if { } else if { } else { }". EM will let you know if the parentheses are not balanced (e.g., you are missing a closing right parenthesis), or if you have any extra right parentheses. You should try to count the parentheses as you compose long nested if statements, save it, check for syntax errors, and fix them if any are found. Let's check together the below example. | ||
The group of questions used below can be accessed from here: [[Media:Example_tailoring_group11.zip|Tailoring survey example.lsg]] | |||
First, with nothing entered, you just see "Hello." | First, with nothing entered, you just see "Hello." | ||
[[File:em-nested-if-blank.jpg]] | [[File:em-nested-if-blank.jpg]] | ||
If you enter a name, it says, "Hello {name}." | If you enter a name, it says, "Hello {name}." | ||
[[File:em-nested-if-name.jpg]] | [[File:em-nested-if-name.jpg]] | ||
If you enter an age, you get a tailored message, depending upon whether you are a pre-school-age child: | |||
If you enter an age, you get a tailored message, depending upon whether you are a pre-school-age child or not: | |||
[[File:em-nested-if-toddler.jpg]] | [[File:em-nested-if-toddler.jpg]] | ||
School aged, teenager, or adult. | |||
School aged, teenager, or adult. Here is a teenager who wants to be anonymous: | |||
[[File:em-nested-if-teenager.jpg]] | [[File:em-nested-if-teenager.jpg]] | ||
Here is the logic file | |||
Here is the logic file of the group. As you can see in the "if-based" question, there are nested if statements based upon the person's age. | |||
[[File:em-nested-if-logic-file.jpg]] | [[File:em-nested-if-logic-file.jpg]] | ||
When you are originally editing this question, it is likely that at some point, you will have the wrong number of parentheses. | |||
When you are originally editing this question, it is likely that at some point, you will have the wrong number of parentheses. Here's what happens if you have too few: | |||
[[File:em-nested-if-missing-closing-paren.jpg]] | [[File:em-nested-if-missing-closing-paren.jpg]] | ||
If you hover over the word "if" which is surrounded by a red box, it says "Parentheses not balanced". | |||
If you hover over the word "if", which is surrounded by a red box, it says "Parentheses not balanced". In this case, there should be four closing parentheses after "already an adult!", but there are only three. | |||
If, on the other hand, you have an extra right parenthesis, it will be surrounded by a red box, like this: | If, on the other hand, you have an extra right parenthesis, it will be surrounded by a red box, like this: | ||
[[File:em-nestedd-if-extra-right-paren.jpg]] | [[File:em-nestedd-if-extra-right-paren.jpg]] | ||
When you are actually editing the question, | |||
When you are actually editing the question, the question looks like this: | |||
[[File:em-nested-if-edit-screen.jpg]] | [[File:em-nested-if-edit-screen.jpg]] | ||
Revision as of 17:55, 20 September 2018
Overview of what has been previously explained
If you have followed so far the structure of the LimeSurvey manual, it means that you already the LimeSurvey terminology. Next, we covered expressions and variables in order to learn how to enhance our surveys. In the next part, we moved to the types and meanings of syntax highlighting to learn how to correct syntax errors.
With these basic notions covered, we could move towards examples and explaining how expressions work within LimeSurvey.
例
The following screenshots give examples, but do not show the values of the tooltips. A tooltip is an informative box which is displayed whenever you hover the mouse over any bold colored words.
Because of this syntax highlighting, it is very easy to compose correct expressions, even the ones that are complicated. Although the LimeSurvey team plans to try to build an Expression Builder GUI, you can use the existing syntax-highlighting to quickly identify and fix typos. You can also use the tooltips to validate the accuracy of your expressions (e.g., confirm you have selected the desired variable(s)).
In each of the examples, there are three columns:
- Source - this is the raw text that you would enter into the LimeSurvey question field
- Pretty Print - this is the syntax-highlighted equivalent of what you entered
- Note that Expressions are shown with a tan background, but not surrounded by curly braces in this highlighting.
- Since EM supports recursive substitution, showing curly braces in the highlighting would cause syntax errors
- Result - this is the output generated when EM processes the source
- Everything that can be properly substituted is
- Expressions with errors are shown in-line, with syntax highlighting. Errors are surrounded by a red-lined box.
適切な構文
You may find below examples of proper syntax:
- Values: shows that known variables are color coded according to whether are set on the current page. Old-style INSERTANS:xxxx gets its own color-coding style
- Question Attributes: shows that dot notation can access some properties of questions
- Math: shows that basic and complex calculations are supported
- TextProcessing: shows some of the available text-processing functions
- Dates: shows two of the available date-related functions
- Conditional: shows the usage of the if() function. The choices can be nested.
- Tailored paragraph: you can completely customize a report based upon prior values
- EM processes within strings: shows that it can do substitutions within strings. This example generates a tailored image name.
- EM doesn't process curly braces like these: shows that if the curly braces are escaped, or there is a white space between the expression and the curly braces, EM ignores the expression.
エラーを含むEM構文
Here are examples of common errors when typing EM expressions. Note that the tooltips provide additional information.
- Inline Javascript that forgot to add spaces after curly brace
- Since "document.write" appears right after a curly brace, EM thinks it is an expression, and red-boxes "document" and "write" since they are undefined variable and functions, respectively
- Unknown/Misspelled variables, functions and operators
- Here we forgot that we are using the variable name "gender" instead of "sex", but EM catches that error. It also red-boxes '++', since that is not a supported operator.
- Warns if use = instead of eq, or perform value assignments
- Note that the '=' and '+=' are in red text instead of black. If you hover the mouse over them, you will see warnings that you are assigning a value.
- Wrong number of arguments for functions
- if() takes 3 arguments, but it has been given 4, so hovering over the red-boxed "if" will explain the error and show the supported syntax
- sum() takes an unlimited number of arguments, but we had a trailing comma before the closing parentheses, so that is red-boxed
- Mismatched parentheses
- This is one of the most common errors when writing expressions.
- This shows two examples of missing closing parentheses, and one example of having one too many closing parentheses.
- Unsuported syntax
- If you use an operator or punctuation that EM does not support, it will red-box it.
- Invalid assignments
- Some variables are readWrite and can have their values changed. Others are read-only.
- If you try to change the value of a read-only variable, you can't. EM will red-box the attempt.
- If you try to assign a value to an equation or a string, you will also get an error
"Live" examples of Syntax Highlighting with active tooltips
Source | Pretty Print | Result |
---|---|---|
Here is an example of OK syntax with tooltips Hello {if(gender=='M','Mr.','Mrs.')} {surname}, it is now {date('g:i a',time())}. Do you know where your {sum(numPets,numKids)} chidren and pets are? | Here is an example of OK syntax with tooltips Hello if(gender == 'M','Mr.','Mrs.') surname, it is now date('g:i a',time()). Do you know where your sum(numPets,numKids) chidren and pets are? | Here is an example of OK syntax with tooltips Hello Mr. Smith, it is now 6:07 am. Do you know where your 3 chidren and pets are? |
Here are common errors so you can see the tooltips Variables used before they are declared: {notSetYet} Unknown Function: {iff(numPets>numKids,1,2)} Unknown Variable: {sum(age,num_pets,numKids)} Wrong # parameters: {sprintf()},{if(1,2)},{date()} Assign read-only-vars:{TOKEN:ATTRIBUTE_1+=10},{name='Sally'} Unbalanced parentheses: {pow(3,4},{(pow(3,4)},{pow(3,4))} | Here are common errors so you can see the tooltips Variables used before they are declared: notSetYet Unknown Function: iff(numPets > numKids,1,2) Unknown Variable: sum(age,num_pets,numKids) Wrong # parameters: sprintf(),if(1,2),date() Assign read-only-vars:TOKEN:ATTRIBUTE_1+=10,name='Sally' Unbalanced parentheses: pow(3,4,(pow(3,4),pow(3,4)) | Here are common errors so you can see the tooltips Variables used before they are declared: notSetYet Unknown Function: iff(numPets > numKids,1,2) Unknown Variable: sum(age,num_pets,numKids) Wrong # parameters: sprintf(),if(1,2),date() Assign read-only-vars:TOKEN:ATTRIBUTE_1+=10,name='Sally' Unbalanced parentheses: pow(3,4,(pow(3,4),pow(3,4)) |
Here is some of the unsupported syntax No support for '++', '--', '%',';': {min(++age, --age,age % 2);} Nor '|', '&', '^': {(sum(2 | 3,3 & 4,5 ^ 6)}} Nor arrays: {name[2], name['mine']} | Here is some of the unsupported syntax No support for '++', '--', '%',';': min( ++ age, -- age,age % 2) ; Nor '|', '&', '^': (sum(2 | 3,3 & 4,5 ^ 6)} Nor arrays: name [ 2 ] ,name [ 'mine' ] | Here is some of the unsupported syntax No support for '++', '--', '%',';': min( ++ age, -- age,age % 2) ; Nor '|', '&', '^': (sum(2 | 3,3 & 4,5 ^ 6)} Nor arrays: name [ 2 ] ,name [ 'mine' ] |
テーラリングの例({INSERTANS:xxx}の拡張)
"Dear {Mr}/{Mrs} Smith..."
'Mr.'か'Mrs.'を使い分けるにはif()関数を使用します。
構文は、if(test,do_if_true,do_if_false)です。
# | コード | 質問 | タイプ |
1 | gender | 性別は? | 性別 |
2 | example1 | Dear {if(gender=='M','Mr.','Mrs.')} Smith, ... | テキスト表示 |
使用ビュー:
案内メールにおける"Dear {Mr}/{Mrs} Smith..."
トークンテーブルの属性を使用して、上記の例を案内メールに使用できます。電子メールの中で'Mr.'または'Mrs.'を使い分けるにはif()関数を使用します。
構文は、if(test,do_if_true,do_if_false)です。
# | 属性 | 値 |
案内メールのテキスト:
Dear {if(ATTRIBUTE_2=='M','Mr','Mrs')} {LASTNAME},
あなたはアンケートに招待されました。
http:/...
電子メールビュー:
計算 / 評価の例
実行時に評価値を計算し、その結果をアンケートデータに格納する
この例では、EMのすべての機能(出現条件、文言調整、式の質問タイプなど)を使用しています。
また、これらのすべてがJavaScript対応であることも示しています。したがって、ページにこれらの機能を導入すると、回答者が回答を設定したり変更したりすると、動的に変化します。
# | コード | 質問 | タイプ | 出現条件 |
1 | numKids | 子どもは何人いますか。 | 数値入力 | 1 |
2 | kid1 | 一番目の子どもは何歳ですか。 | 数値入力 | numKids >= 1 |
3 | kid2 | 二番目の子どもは何歳ですか。 | 数値入力 | numKids >= 2 |
4 | kid3 | 三番目の子どもは何歳ですか。 | 数値入力 | numKids >= 3 |
5 | kid4 | 四番目の子どもは何歳ですか。 | 数値入力 | numKids >= 4 |
6 | sumKidAges | {sum(kid1.NAOK,kid2,NAOK,kid3.NAOK,kid4.NAOK)} | 式 | 1 |
7 | kidSummary | {numKids}人の{if(numKids==1,'子ども','子どもたち')}がいると回答しました。{if(numKids>1,implode(' ','上から',min(numKids,4),'人までの年齢の合計は',sumKidAges,'です。'),' ')} | テキスト表示 | 1 |
To download this example, please click on the following link: Assessments_survey_example.
代表的な質問のスクリーンショットです。ご覧のとおり、EM構文では、文言調整を含んでいそうなすべてのフィールドが強調表示されます。ここでは、出現条件、式の質問タイプ、および質問内の置換の例を示します。ヘルプ、グループヘッダーの表示、ようこそメッセージ、および終了メッセージ内で置き換えることもできます。
この質問では、出現条件は{numKids >= 2}であるため、回答者が少なくとも2人の子供がいると回答した場合にのみ表示されます。
また、各変数に.NAOK接尾辞を使用することに注意してください。これは、EMが入れ子の出現条件を実現するためのものです。もし.NAOKをつけない場合持、4人の子供を持っていると回答した場合にのみ合計が計算されることになります(すべての変数が真の場合)。.NAOKを使用すると、変数のすべてまたは一部が偽であっても合計を計算することを意味します(すなわち"該当なし"(Not Applicable - NA)でも構わないということです)。
ただし、.NAOK属性は変数がEMに渡されるかどうかにのみ影響します。その人が最初に3人の子供がいると回答し、それぞれの年齢を入力した後、気が変わり、2人であると答えた場合、3つの入力値の合計は必要ありません。次の例で確認してください。
それぞれの式は、黄褐色の背景で色分けされています。ご覧の通り、3つの別々の式があります。最後の式には、2人以上の子供がいる場合にのみ表示されるメッセージが入っています。
ここでは、実際のアンケートのスクリーンショットを示します。
最初にページにアクセスすると、これが表示されます。"0人の子どもがいます"ではなく、"0人の子どもたちがいます"となっていることに注意してください。
子どもの数を1に変更すると、同じページ上にあってもこのように表示が即座に変わります。
Now notice that the grammar is correct: "You have 1 child".
Now I change the value for number of children to 3, and the display instantly changes to this.
Notice that you now see the conditional message at the bottom: "The sum of ages of your first 3 kids is 0.".
Now I'll enter ages for my imaginary children, and I get this display, summing up their ages:
Again, the score and display updates instantly as I enter the values, so you can use this to show a running total of an Assessment Score.
Now, I change the value for the number of children to 2. The display has changed to this:
Notice that although I had entered a value of 5.5 for the third child, the report now only sums the values of my first 2 children.
The reason for this is that the 3rd value is now irrelevant, and irrelevant values are actively ignored by EM.
If I were to change the number of kids back to 3, I would see the value of 5.5 I entered again. So, I don't lose any information I enter on the page.
However, if I navigate to the Next or Previous page, all irrelevant values will be NULLed out in the session and in the database. So, if I were to keep the value at 2, go to the next page, and then come back and state that I actually have 3 kids, I would no longer see the age of 5.5.
Enter data and see a dynamically changing report of what was entered on the same page
This example presents the Tailoring process within LimeSurvey.
To download this example, click on the following link: Dynamic changes survey example.
Here is what the page looks like initially. You only see the question asking what city you live in:
Once you start to enter an answer, the tailoring process is also starting:
As you enter answers, the table at the bottom of the page is updated to show the answer codes and values of your responses.
Common Debugging Examples
Nested if() Statements (Conditional Logic)
EM supports the function "if(test,do_if_true,do_if_false)" so that you can perform conditional logic or tailoring. This function can be nested to do the equivalent of "if { } else if { } else { }". EM will let you know if the parentheses are not balanced (e.g., you are missing a closing right parenthesis), or if you have any extra right parentheses. You should try to count the parentheses as you compose long nested if statements, save it, check for syntax errors, and fix them if any are found. Let's check together the below example.
The group of questions used below can be accessed from here: Tailoring survey example.lsg
First, with nothing entered, you just see "Hello."
If you enter a name, it says, "Hello {name}."
If you enter an age, you get a tailored message, depending upon whether you are a pre-school-age child or not:
School aged, teenager, or adult. Here is a teenager who wants to be anonymous:
Here is the logic file of the group. As you can see in the "if-based" question, there are nested if statements based upon the person's age.
When you are originally editing this question, it is likely that at some point, you will have the wrong number of parentheses. Here's what happens if you have too few:
If you hover over the word "if", which is surrounded by a red box, it says "Parentheses not balanced". In this case, there should be four closing parentheses after "already an adult!", but there are only three.
If, on the other hand, you have an extra right parenthesis, it will be surrounded by a red box, like this:
When you are actually editing the question, the question looks like this: