Actions

ExpressionScript エンジン - クイックスタートガイド

From LimeSurvey Manual

Revision as of 02:41, 5 March 2020 by Bravehorse (talk | contribs) (Created page with "出現条件は以下の場合に表示/編集できます。 *質問レベルの出現条件を表示/編集したい *グループレベルの出現条件を表示/編...")


クイックスタートガイド

LimeSurvey内で、ExpressionScript(略称: ES)を使用して、さらにアンケートをカスタマイズできます。 注: "ExpressionScriptは、以前のバージョンでは式マネージャー(EM)という名前でした。式マネージャーという表記があった場合は、ESの旧称であるとご理解ください。"

ESを使用して、下記の指定ができます。

  1. ナビゲーション/分岐 - 回答者ごとに質問が表示される順番を変更します。
  2. 文言調整/パイピング - 質問の文言調整(先行する回答の引用、数や性別による語句の変化など)、カスタムレポートの生成(評価点数、アドバイスの提示など)
  3. 検証 - 回答が条件(最大値、最小値、入力パターンなど)を満たすようにします。

ESでは、各機能のロジックを指定する直感的な方法が提供されています。標準的な数式として書くことができるものは、ほとんどが有効な式になります。

ESは現在、70の関数が利用でき、より多くの機能をサポートするよう、簡単に拡張することができます。また、(SGQA名ではなく)人間が読める変数名を使用して変数にアクセスすることもできます。

以下のセクションで、ESを使う主な場面を紹介します。


出現条件(ナビゲーション制御/分岐)

アンケートによっては、"Gotoロジック"を使用して、質問1でCと答えた場合は質問5にジャンプしたりします。このアプローチは、検証が難しいため、大きな制約があります。また、質問を並べ替えるときにロジックが壊れやすくなってしまいます。一方、ESでは、質問が有効かどうかを決めるすべての条件を、ブール値の出現条件式を使用して指定します。質問が出現条件を満たす場合は質問が表示され、それ以外の場合は「適用外」となり、データベースには"NULL"値が格納されます。

注意: これは条件エディターで実行できるものと似ていますが、ESではより複雑で強力な条件を簡単に指定でき、さらにSGQA名ではなく変数名を使用できます。




出現条件の概念をよく理解するために、アンケート回答者の体格指数(BMI)を計算する以下のアンケートに焦点を当ててみましょう。本例をダウンロードするには、次のリンクをクリックしてください。体質指数調査の例

出現条件の式は、下図の変数名の後の出現条件欄に示されています。weight、weight_units、height、およびheight_unitsの出現条件の値はすべて1(既定値)であり、これらの質問は常に表示されます。ただし、BMIの出現条件は{!is_empty(height) and !is_empty(weight)}となっていて、回答者が身長と体重の両方の値を入力した場合にのみBMIが計算されることを意味します(これにより、ゼロエラーが発生するリスクを回避します)。また、質問"Report"は、回答者が4つの質問(身長、身長単位、体重、体重単位)のすべてに回答した場合にのみ表示されます。



注意: 上記の画像はアンケートロジックファイルのもので、アンケートを有効にする前に構文エラーを探すことができます。


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

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


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.