Actions

ExpressionScript Engine - Quick start guide/ja: Difference between revisions

From LimeSurvey Manual

m (FuzzyBot moved page Expression Manager - quick start guide/ja to ExpressionScript Engine - Quick start guide/ja without leaving a redirect: Part of translatable page "Expression Manager - quick start guide")
(Updating to match new version of source page)
Line 4: Line 4:




=クイックスタートガイド=
=Quick start guide=




[https://www.limesurvey.org LimeSurvey]では、式マネージャー(EM)を使ってアンケートをカスタマイズすることができます。
Within [https://www.limesurvey.org LimeSurvey], you can further customize your surveys via the usage of the ExpressionScript (short: ES).
Sidenote: "ExpressionScript was named Expression Manager (EM) in earlier version. If you read Expression Manager somewhere, it is just the old name of ES."


EMを使用して以下の指定ができます。
ES can be used to specify the following:


#'''ナビゲーション/分岐''' - 回答者ごとに質問が表示される順番を変更します。
# '''Navigation/Branching''' - allows a respondent's answers to change the order in which the questions are displayed;
#'''文言調整/パイピング''' - 質問の文言調整(先行する回答の引用、数や性別による語句の変化など)、カスタムレポートの生成(評価点数、アドバイスの提示など)
# '''Tailoring/Piping''' - helps you phrase the question (such as referring to prior answers, or conjugating sentences based upon the age or gender of your subjects) or how to generate custom reports (like assessment scores or tailored advice);
#'''検証''' - 回答が条件(最大値、最小値、入力パターンなど)を満たすようにします。
# '''Validation''' - ensures that answers pass certain criteria, like min and max values or a certain input pattern.


式マネージャー(EM)では、各機能のロジックを指定する直感的な方法が提供されています。標準的な数式として書くことができるものは、ほとんどが有効な式になります。
ES provides an intuitive way to specify the logic for each of those features. Nearly anything that you can write as a standard mathematical equation is a valid expression.


EMは現在、70の関数が利用でき、より多くの機能をサポートするよう、簡単に拡張することができます。また、([[SGQA_identifier/ja|SGQA名]]ではなく)人間が読める変数名を使用して変数にアクセスすることもできます。
ES currently provides access to 70 functions and it can be easily extended to support more. It also lets you access you
variables using human-readable variable names (rather than [[SGQA_identifier|SGQA names]]).


以下のセクションで、式マネージャーを使う主な場面を紹介します。
The upcoming sections show the main places where the ES is used.




==関連(ナビゲーション制御/分岐)==
==Relevance (Controlling Navigation/Branching)==




アンケートによっては、"Gotoロジック"を使用して、質問1でCと答えた場合は質問5にジャンプしたりします。このアプローチは、検証が難しいため、大きな制約があります。また、質問を並べ替えるときにロジックが壊れやすくなってしまいます。一方、EMでは、質問が有効かどうかを決めるすべての条件を、[https://en.wikipedia.org/wiki/Boolean_algebra ブール値の出現条件式]を使用して指定します。質問が出現条件を満たす場合は質問が表示され、それ以外の場合は「適用外」となり、データベースにはNULL値が格納されます。
Some surveys use "Goto Logic", such that if you answer Question1 with option C, you are redirected to Question5. This approach is very limited since it is hard to validate it. Moreover, it easily breaks when you have to reorder questions. On the other hand, ES uses [https://en.wikipedia.org/wiki/Boolean_algebra Boolean relevance equations] to specify all the conditions under which a question might be valid. If the question is relevant, then the question is shown, otherwise, it is not applicable, and the value "NULL" is stored in the database.


<div class='simplebox'> '''注意:''' これは[[Setting conditions/ja|条件エディター]]で実行できるものと似ていますが、EMではより複雑で強力な条件を簡単に指定でき、さらにSGQA名ではなく変数名を使用できます。</div>
<div class='simplebox'> '''Note:''' This is similar to what can be done via the [[Setting conditions|Conditions editor]], but ES lets you easily specify much more complex and powerful criteria (and lets you use the variable name rather than SGQA identifiers).</div>




Line 33: Line 35:




出現条件の概念をよく理解するために、アンケート回答者の体格指数(BMI)を計算する以下のアンケートに焦点を当ててみましょう。本例をダウンロードするには、次のリンクをクリックしてください。[[Media:EM_survey_-_Cohabs.zip|体質指数調査の例]]
To better understand the relevance concept, let's focus on the following survey which computes the Body Mass Index (BMI) of survey respondents. To download it, click on the following link: [[Media:ES_survey_-_Cohabs.zip|Body Mass Index survey example]].


出現条件の式は、変数名の後の'''Relevance'''列に次のように表示されます。 weight、weight_units、height、およびheight_unitsの出現条件はすべて1(既定値)であり、これらの質問は常に表示されます。一方、BMIの出現条件は{!is_empty(height) and !is_empty(weight)}です。つまり、BMIは、回答者が身長と体重の両方の値を入力した場合にのみ計算されます(ゼロエラーを防止するため)。また、"レポート"という質問は、回答者が4つの主な質問(身長、身長の単位、体重、体重の単位)にすべて答えた場合にのみ表示されます。
出現条件の式は、変数名の後の'''Relevance'''列に次のように表示されます。 weight、weight_units、height、およびheight_unitsの出現条件はすべて1(既定値)であり、これらの質問は常に表示されます。一方、BMIの出現条件は{!is_empty(height) and !is_empty(weight)}です。つまり、BMIは、回答者が身長と体重の両方の値を入力した場合にのみ計算されます(ゼロエラーを防止するため)。また、"レポート"という質問は、回答者が4つの主な質問(身長、身長の単位、体重、体重の単位)にすべて答えた場合にのみ表示されます。
Line 41: Line 43:




<div class="simplebox"> [[File:help.png]] '''注意:''' 上記の画像は[[Show logic file/ja|アンケートロジックファイル]]のもので、アンケートを有効にする前に構文エラーを探すことができます。</div>
<div class="simplebox"> [[File:help.png]] '''Note:''' The above image comes from the [[Show logic file|survey logic file]] which allows you to look for syntax errors before activating the survey.</div>




Line 49: Line 51:




===質問レベルの関連の閲覧/編集===
===Viewing / Editing Question-Level Relevance===




この式は、体格指数(BMI)を計算します。身長と体重を入力した場合のみ計算されます。
This equation computes the Body Mass Index (BMI). It is only asked if the person enters their height and weight.




Line 58: Line 60:




こちらが"BMI"質問の編集画面です。
This is the edit screen for the "BMI" question.




Line 64: Line 66:




出現条件式を入力するときは中括弧を使用しないようにしてください。
Note that you do not use the curly braces when you enter a relevance equation.




===グループレベルの関連の閲覧/編集===
===Viewing / Editing Group-Level Relevance===




ここでもう一つの例 - 単純な国勢調査に焦点を当てましょう。ダウンロードするには、[[Media:EM survey - Cohabs.zip|国勢調査の例]]をクリックしてください。
Let's focus now on another example - a simple census survey. To download it, click on the following link: [[Media:ES survey - Cohabs.zip|Census survey example]].


最初のページでは、同居人数を 尋ね、"cohabs"変数に保存します。このページは、複数の同居者がいる場合にのみ表示されます(同居している2人目の人向けに表示されます)。また、p2name、p2age、p2sumは、それぞれの前の質問に回答がある場合にのみ表示されます。
The first page asks how many people live with you and stores that in the "cohabs" variable. This page is only shown if you have more than one cohabitant (it is shown for the second person cohabitating with you). Also, p2name, p2age. p2sum are displayed only if the question before each of them contains a response.




Line 78: Line 80:




ご覧のように、グループには質問レベルの出現条件もあり、こうした質問は、先行する質問に回答したときにのみ表示されます(例:p2ageはp2nameに回答したときだけ表示する)。EMは、グループと質問レベルの出現条件を結合します。'''グループ内の質問は、グループ全体が出現条件を満たす場合にのみ表示されます'''。次に、グループ内で出現条件を満たす質問のみが表示されます。
So, the group also has question-level relevance criteria, such that some questions only appear if you answered certain questions before them (e.g., p2age is displayed if p2name was answered). ES combines the Group and Question-level relevance for you. '''Questions in a group are only asked if the group as a whole is relevant. Then, only the subset of questions within the group that are relevant are asked.'''


''Cohabitant 2''のグループレベルの出現条件を編集する画面は次のとおりです。
Here is the screenshot for editing the group-level relevance of ''Cohabitant 2'':




Line 86: Line 88:




出現条件式を入力するときは中括弧を使用しないようにしてください。
Note that you do not use the curly braces when you enter a relevance equation.




==カスタマイズ/パイプ==
==Tailoring/Piping==




EMを使用すると、単純な、または複雑な条件によって文言を変えることができます。例えば、"<nowiki>[</nowiki>製品]を購入したと回答しました。よかった点は何ですか?"のように、単純な「置き換え」が必要な場合があります。また、"<nowiki>[</nowiki>Mr./Mrs.] <nowiki>[</nowiki>LastName]、アンケートを完了してもよろしいですか?"のように、条件付きの置き換えが必要な場合もあります。この場合、回答者の性別に基づいてMr.かMrs.を使用します。また、例えば、数学的計算に基づいてさらに複雑な置き換えが必要になることもあります。EMは、このような文言調整/パイプをサポートしています。
ES lets you easily do simple and complex conditional tailoring. Sometimes you just need a simple substitution, like saying, "You said you purchased <nowiki>[</nowiki>Product]. What did you like best about it?". Sometimes you need conditional substitution like "<nowiki>[</nowiki>Mr./Mrs.] <nowiki>[</nowiki>LastName], would you be willing to complete our survey?". In this case, you want to use "Mr. or Mrs." based on the person's gender. Other times you need even more complex substitution (such as based upon a mathematical computation). ES supports each of these types of tailoring/piping.




===条件式===
===Conditional Equations===




体格指数BMIの例では、身長と体重の入力単位がメートル法であってもメートル法でなくても(cm/インチ、kg/ポンド)、BMIが計算できることを示しています。
The Body Mass Index example shows the ability to compute a person's BMI, even while letting them enter their height and weight in two different units (cms vs inches and kgs vs lbs):




Line 104: Line 106:




ここで、weightkgは、{if(weightunits == "kg", weight, weight * 0.453592)}となっています。このif()関数では、キログラムで体重が入力されていた場合は入力値をそのまま採用し、それ以外の場合は入力値(単位はポンド)に0.453592をかけてキログラムに変換します。同様に、変数heightmは、身長がセンチメートル単位で入力されていても(センチメートル/100)、インチ単位で入力されていても(1メートル=3.28084インチ)メートル単位の身長を計算するようになっています。
In this case, weightkg is {if(weightunits == "kg", weight, weight * 0.453592)}. This "if()" function means that if the subject enters the weight using kilograms, use that value, otherwise multiply the entered value (pounds is the alternative) by 0.453592 to convert it to kilograms. The heightm variable uses a similar approach to compute the person's height in meters (height in cms/100), even if he has entered his height in inches (1 meter=3.28084 inches).


BMIは、{weightkg / (heightm * heightm)}として計算されます。
BMI is computed as: {weightkg / (heightm * heightm)}.


最後に、回答者に対し、入力した内容を条件付きでカスタマイズして表示します。("身長2メートル、体重70キロと回答しました。"
Lastly, the report conditionally tailors the message for the subject, telling her what he entered. (e.g., "You said you are 2 meters tall and weight 70 kg.")


上の画像では、weightstatusは入れ子の"if()"ステートメントを使用し、その人物がやせているのか肥満なのかを分類します。その式をロジックを確認して見ることができます。
In the below image, weightstatus uses nested "if()" statements to categorize the person as underweight to severely obese. You can see its equation by checking its logic:




Line 116: Line 118:




この質問の編集ウィンドウから、次の2つのことがわかります。
From the edit window for this question, you can see two things:
#中括弧で表現を囲む必要があります
#Tailoring must surround expressions with curly braces
#式は、例えばこの例のように、ネストされた条件付きロジックを読みやすくするため、複数の行にまたがって記述することができます。
#Expressions can span multiple lines if, as in this case, you want to make it easier to read the nested conditional logic.




Line 124: Line 126:




===カスタマイズされた質問・解答・レポート===
===Tailored Questions, Answers, and Reports===




<div class="simplebox">[[File:help.png]] '''注意:''' 同じ質問ページの選択ボックスで回答オプションを利用できるようにすると、動的な文言調整が機能しないことがあります。これは、文言調整において、選択オプション内で使えない<nowiki><span></nowiki>タグを挿入してしまうことに起因しています。</div>
<div class="simplebox">[[File:help.png]] '''Note:''' Dynamic tailoring may not work if answer options are made available in select boxes on the same question page. This results from the fact that tailoring inserts a <nowiki><span></nowiki> tag which is not valid inside select options.</div>


BMIレポートは次のようになります。
The BMI report looks like this:




Line 135: Line 137:




同じ質問の編集ウィンドウです。
Here is the edit window for the same question.




Line 141: Line 143:




中括弧内のものは式として扱われ、構文強調表示(色分け)されます(前出の画像)。タイプミス(変数名または関数の定義が誤っている、または定義されていないなど)があると、EMにエラーが表示されます。例では次のようになっています。
Anything within curly braces is treated as an expression, being syntax-highlighted (color coded) in the prior image. If you have any typos (such as misspelled or undefined variable names or functions), ES would show an error. In our below example:
* heightunit.shownは未定義の変数名(正しくはheightunits.shown)
* heightunit.shown is an undefined variable name (it is actually heightunits.shown) and
* "rnd()"は未定義の関数(適切な関数名は "round()"
* "rnd()" is an undefined function (the proper function name is "round()").


どちらの場合も、発見・修正しやすくするため、エラーは赤いボックス内に配置されます。
In both cases, the errors are located within a red box to make it easier to spot and fix them.




Line 151: Line 153:




また、入力された値の一覧やカスタマイズされたアドバイスなど、複雑なレポートをすばやく作成できます。
You can also see that you can quickly create complex reports, such as a table of entered values or tailored advice.


質問のどの部分がフリーテキストで、どの部分が式マネージャーで解析する必要があるかをLimeSurveyが判断するため、すべての式を中括弧で囲む必要があります。
Please remember that all tailoring must surround expressions with curly braces so that LimeSurvey knows which parts of the question are free text and which should be parsed by the ExpressionScript engine.




==検証==
==Validation==




EMは、高度な質問オプションがどのように機能するかを制御します。回答の数、個々の入力値や合計の最小値/最大値を制限したり、入力された値が指定された文字列パターンと一致していることを確認したりします。いつものように高度な質問オプションの入力を行いますが、これらのフィールドの値はどれも式と見なされるため、他の質問と複雑な条件で組み合わせ、最小値/最大値基準を設定することができます。
ES controls how most of the advanced question options work. These control aspects like min/max numbers of answers, min/max individual values, min/max sum values, and checking that entered values match specified string patterns. Any value in one of those fields is considered an expression, so you can have min/max criteria with complex conditional relationships to other questions.


これらのすべてのケースでは、高度な質問オプションは常に式と見なされるため、中括弧を使用することはありません。
In all of these cases, since the advanced question attribute is always considered an expression, you do not use curly braces when specifying it.


[[Expression Manager sample surveys|サンプルアンケート]]ページには、様々な検証式を使用した実例がたくさんあります。
The [[ExpressionScript sample surveys|sample surveys]] page shows many working examples containing a variety of validation expressions.


=式マネージャー - プレゼンテーション=
=Expression Manager - presentation=




式マネージャーの詳細や、様々な式を使用してアンケートを強化する方法については、次の[https://manual.limesurvey.org/Expression_Manager_-_presentation リンク]をクリックしてください。
To find out more about the Expression Manager and how you can use different expressions to enhance your survey, please click on the following [https://manual.limesurvey.org/Expression_Manager_-_presentation link].

Revision as of 15:45, 11 February 2020


Quick start guide

Within LimeSurvey, you can further customize your surveys via the usage of the ExpressionScript (short: ES). Sidenote: "ExpressionScript was named Expression Manager (EM) in earlier version. If you read Expression Manager somewhere, it is just the old name of ES."

ES can be used to specify the following:

  1. Navigation/Branching - allows a respondent's answers to change the order in which the questions are displayed;
  2. Tailoring/Piping - helps you phrase the question (such as referring to prior answers, or conjugating sentences based upon the age or gender of your subjects) or how to generate custom reports (like assessment scores or tailored advice);
  3. Validation - ensures that answers pass certain criteria, like min and max values or a certain input pattern.

ES provides an intuitive way to specify the logic for each of those features. Nearly anything that you can write as a standard mathematical equation is a valid expression.

ES currently provides access to 70 functions and it can be easily extended to support more. It also lets you access you variables using human-readable variable names (rather than SGQA names).

The upcoming sections show the main places where the ES is used.


Relevance (Controlling Navigation/Branching)

Some surveys use "Goto Logic", such that if you answer Question1 with option C, you are redirected to Question5. This approach is very limited since it is hard to validate it. Moreover, it easily breaks when you have to reorder questions. On the other hand, ES uses Boolean relevance equations to specify all the conditions under which a question might be valid. If the question is relevant, then the question is shown, otherwise, it is not applicable, and the value "NULL" is stored in the database.

Note: This is similar to what can be done via the Conditions editor, but ES lets you easily specify much more complex and powerful criteria (and lets you use the variable name rather than SGQA identifiers).




To better understand the relevance concept, let's focus on the following survey which computes the Body Mass Index (BMI) of survey respondents. To download it, click on the following link: Body Mass Index survey example.

出現条件の式は、変数名の後のRelevance列に次のように表示されます。 weight、weight_units、height、およびheight_unitsの出現条件はすべて1(既定値)であり、これらの質問は常に表示されます。一方、BMIの出現条件は{!is_empty(height) and !is_empty(weight)}です。つまり、BMIは、回答者が身長と体重の両方の値を入力した場合にのみ計算されます(ゼロエラーを防止するため)。また、"レポート"という質問は、回答者が4つの主な質問(身長、身長の単位、体重、体重の単位)にすべて答えた場合にのみ表示されます。



Note: The above image comes from the survey logic file which allows you to look for syntax errors before activating the survey.


出現条件は以下の場合に表示/編集できます。

  • 質問レベルの出現条件を表示/編集したい
  • グループレベルの出現条件を表示/編集したい


Viewing / Editing Question-Level Relevance

This equation computes the Body Mass Index (BMI). It is only asked if the person enters their height and weight.



This is the edit screen for the "BMI" question.



Note that you do not use the curly braces when you enter a relevance equation.


Viewing / Editing Group-Level Relevance

Let's focus now on another example - a simple census survey. To download it, click on the following link: Census survey example.

The first page asks how many people live with you and stores that in the "cohabs" variable. This page is only shown if you have more than one cohabitant (it is shown for the second person cohabitating with you). Also, p2name, p2age. p2sum are displayed only if the question before each of them contains a response.



So, the group also has question-level relevance criteria, such that some questions only appear if you answered certain questions before them (e.g., p2age is displayed if p2name was answered). ES combines the Group and Question-level relevance for you. Questions in a group are only asked if the group as a whole is relevant. Then, only the subset of questions within the group that are relevant are asked.

Here is the screenshot for editing the group-level relevance of Cohabitant 2:



Note that you do not use the curly braces when you enter a relevance equation.


Tailoring/Piping

ES lets you easily do simple and complex conditional tailoring. Sometimes you just need a simple substitution, like saying, "You said you purchased [Product]. What did you like best about it?". Sometimes you need conditional substitution like "[Mr./Mrs.] [LastName], would you be willing to complete our survey?". In this case, you want to use "Mr. or Mrs." based on the person's gender. Other times you need even more complex substitution (such as based upon a mathematical computation). ES supports each of these types of tailoring/piping.


Conditional Equations

The Body Mass Index example shows the ability to compute a person's BMI, even while letting them enter their height and weight in two different units (cms vs inches and kgs vs lbs):



In this case, weightkg is {if(weightunits == "kg", weight, weight * 0.453592)}. This "if()" function means that if the subject enters the weight using kilograms, use that value, otherwise multiply the entered value (pounds is the alternative) by 0.453592 to convert it to kilograms. The heightm variable uses a similar approach to compute the person's height in meters (height in cms/100), even if he has entered his height in inches (1 meter=3.28084 inches).

BMI is computed as: {weightkg / (heightm * heightm)}.

Lastly, the report conditionally tailors the message for the subject, telling her what he entered. (e.g., "You said you are 2 meters tall and weight 70 kg.")

In the below image, weightstatus uses nested "if()" statements to categorize the person as underweight to severely obese. You can see its equation by checking its logic:



From the edit window for this question, you can see two things:

  1. Tailoring must surround expressions with curly braces
  2. Expressions can span multiple lines if, as in this case, you want to make it easier to read the nested conditional logic.



Tailored Questions, Answers, and Reports

Note: Dynamic tailoring may not work if answer options are made available in select boxes on the same question page. This results from the fact that tailoring inserts a <span> tag which is not valid inside select options.

The BMI report looks like this:



Here is the edit window for the same question.



Anything within curly braces is treated as an expression, being syntax-highlighted (color coded) in the prior image. If you have any typos (such as misspelled or undefined variable names or functions), ES would show an error. In our below example:

  • heightunit.shown is an undefined variable name (it is actually heightunits.shown) and
  • "rnd()" is an undefined function (the proper function name is "round()").

In both cases, the errors are located within a red box to make it easier to spot and fix them.



You can also see that you can quickly create complex reports, such as a table of entered values or tailored advice.

Please remember that all tailoring must surround expressions with curly braces so that LimeSurvey knows which parts of the question are free text and which should be parsed by the ExpressionScript engine.


Validation

ES controls how most of the advanced question options work. These control aspects like min/max numbers of answers, min/max individual values, min/max sum values, and checking that entered values match specified string patterns. Any value in one of those fields is considered an expression, so you can have min/max criteria with complex conditional relationships to other questions.

In all of these cases, since the advanced question attribute is always considered an expression, you do not use curly braces when specifying it.

The sample surveys page shows many working examples containing a variety of validation expressions.

Expression Manager - presentation

To find out more about the Expression Manager and how you can use different expressions to enhance your survey, please click on the following link.