x

Main chapters

  1. LimeSurvey Cloud vs LimeSurvey CE
  2. LimeSurvey Cloud - Quick start guide
  3. LimeSurvey CE - Installation
  4. How to design a good survey (Guide)
  5. Getting started
  6. LimeSurvey configuration
  7. Introduction - Surveys
  8. View survey settings
  9. View survey menu
  10. View survey structure
  11. Introduction - Questions
  12. Introduction - Question Groups
  13. Introduction - Surveys - Management
  14. Survey toolbar options
  15. Multilingual survey
  16. Quick start guide - ExpressionScript
  17. Advanced features
  18. General FAQ
  19. Troubleshooting
  20. Workarounds
  21. License
  22. Version change log
  23. Plugins - Advanced
 Actions

ExpressionScript examples/zh: Difference between revisions

From LimeSurvey Manual

Created page with "假设正在使用以下代码: *问题代码:q4 *子问题代码:SQ001,SQ002,...... *答案代码比值1:A1,A2,...... *答案代码比值2:B1,B2,......"
Created page with "{{Note|要在某个比例上引用某个子问题的结果,你需要使用QuestionCode . '_' . SubQuestionCode . '_' . ScaleID。 '''请注意,“ScaleID”对于第一个..."
Line 275: Line 275:




{{Note|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!'''</br> Example for referring to the answer of the third subquestion and second scale: q4_SQ003_1 - Question 4, subquestion 3, second scale.}}
{{Note|要在某个比例上引用某个子问题的结果,你需要使用QuestionCode . '_' . SubQuestionCode . '_' . ScaleID'''请注意,“ScaleID”对于第一个刻度为0,对于另一个刻度为1!''' </br>参考第三个子问题和第二个比例答案的示例:q4_SQ003_1 - Question 4, subquestion 3, second scale}}





Revision as of 08:01, 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),"")}


有兩個尺度的問題

這僅適用於雙尺度問題類型。


假設正在使用以下代碼:

  • 問題代碼:q4
  • 子問題代碼:SQ001,SQ002,......
  • 答案代碼比值1:A1,A2,......
  • 答案代碼比值2:B1,B2,......


要在某個比例上引用某個子問題的結果,你需要使用QuestionCode . '_' . SubQuestionCode . '_' . ScaleID。 請注意,「ScaleID」對於第一個刻度為0,對於另一個刻度為1!
參考第三個子問題和第二個比例答案的示例: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 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)