# Script-Generated Question

## **Overview**

{% hint style="info" %}
This feature only available in **row labeling** project and disabled by default. Please reach out to [**support@datasaur.ai**](mailto:support@datasaur.ai) if your team needs this feature, and we'll assist you!
{% endhint %}

**Script-generated questions** is an advanced question type that allows dynamic question generation based on each row’s data. Using [TypeScript](https://www.typescriptlang.org/), users can configure logic that determines which questions should appear for a given row. This flexibility makes it ideal for cases where static question sets are insufficient.

### When to Use Script-Generated Questions

* You need **dynamic questions** that change based on row data.
* You want to **automate question creation** instead of manually defining them.
* You require **custom logic** to determine which questions appear.
* You need **dropdown options that are generated based on the data in each row.**

## Configuration

### **Defining Question Identifiers**

An **identifier** is a unique representation of a question. It serves as a reference to the question type, label, and configuration, allowing the script to access and manipulate the question during execution. Before generating questions dynamically, all possible question identifiers **must be defined during project creation**.

This is configured in the <mark style="color:red;">**`identifiers`**</mark> section of the script.

![Identifiers](https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-13818b35dbd63168c3c6f480ec7acb717d30cc17%2FPCW%20-%20step%203%20-%20row%20labeling%20-%20script-generated%20question%20-%20configure%20custom%20script.png?alt=media)

Each identifier should have the following attributes:

* <mark style="color:red;">**`id`**</mark> — A unique, zero-indexed integer, signifying the relative order of the questions.
* <mark style="color:red;">**`label`**</mark> — A human-readable string that represents the question.
* <mark style="color:red;">**`type`**</mark> — The question type (i.e. text, multiple choice, date).
  * Currently, all question types are supported **except** **Grouped Attributes** and **Script-Generated Questions**, with some caveats detailed [here](#limitation).
  * See [this](https://docs.datasaur.ai/data-studio-projects/lets-get-labeling/label-sets#question-types) for more details on each question type.
* <mark style="color:red;">**`config.multiple`**</mark> — Configuration that allows a question to have multiple answers.

<details>

<summary>Example Configuration</summary>

Here’s an example of an identifier for a **text** question and a **multiple-choice** question.

```tsx
identifiers: Record<string, QuestionIdentifier> = {
  ["Text Question"]: {
    id: 0, 
    label: "Text Question", 
    config: { multiple: false }, 
    type: QuestionType.TEXT
  },
  ["Multiple Choice Question"]: {
    id: 1, 
    label: "Multiple Choice Question", 
    config: { multiple: true }, 
    type: QuestionType.MULTIPLE_CHOICE
  },
};
```

</details>

### Dynamically Generating Questions

The core function that determines which questions appear is <mark style="color:red;">`getQuestionsForRow`</mark>. This function runs for each row in the dataset, evaluating its data and returning the appropriate questions.

{% hint style="info" %}
**Tip:** You can view available <mark style="color:red;">`row`</mark> and <mark style="color:red;">`columns`</mark> attributes in the editor by pressing **Ctrl / Cmd + Space**.
{% endhint %}

<details>

<summary>Example Usage</summary>

In the following example:

* A **text question** appears if the column <mark style="color:red;">`DATA`</mark> contains the word <mark style="color:red;">`"text"`</mark>.
* Otherwise, a **multiple-choice question** is shown, with options generated from the values in <mark style="color:red;">`DATA`</mark>.

```tsx
getQuestionsForRow({ row, columns }: RowContext): Question[] {
  if (getCellValueByColumnLabel("DATA").includes("text")) {
    return [{ ...this.identifiers["Text Question"], required: false }];
  }

  const options = getCellValueByColumnLabel("DATA").split(",");
  return [
    {
      ...this.identifiers["Multiple Choice Question"],
      required: false,
      config: {
        ...this.identifiers["Multiple Choice Question"],
        options: options.map((o) => ({ id: o, label: o })),
      },
    },
  ];
```

</details>

Here’s how it will look once the project is created.

![](https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-110c443c39ab81a60005303437f5e4aba54d2901%2FRow%20labeling%20-%20script-generated%20question%20-%20project%201%20-%20question%201.png?alt=media)

![](https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-5999903bef0a7da75d2f749682d64cd282673e11%2FRow%20labeling%20-%20script-generated%20question%20-%20project%201%20-%20question%202.png?alt=media)

You can find a sample CSV, as well as the full script to recreate the project below.

{% file src="<https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-edadab40913d51e2ccfaa3111147e3e11bb8ed25%2FDatasaur%20sample%20-%20Script%20generated%20question.csv?alt=media&token=f46fe717-a1d2-47a4-9583-608c42f92cda>" %}

{% file src="<https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-1134dfeab4df31cb2d0ecf8ef3eed74425ee9b00%2FDatasaur%20sample%20-%20Script%20for%20row%20question.ts?alt=media&token=03d241a1-4dca-4d52-8cd6-4166fe1dfad8>" %}

## Limitation

### Dropdown and Hierarchical Dropdown

We currently **don’t support** configuring dropdown or hierarchical dropdown question types in script-generated questions.

We recommend using **multiple choice** or **single choice** question types, as these also allow users to select from a set of options.

### Question Set Management

1. Any script-generated questions **will not be saved** to the library when the question set is saved.
2. When configuring the question set in **Label set management**, these questions **will not appear** in the question type dropdown.

### Question Logic

Since question logic relies on a predefined order, it’s not currently possible to configure logic for questions that reference a script-generated one. This is because script-generated questions are created dynamically during the project, which makes it difficult to set conditions for subsequent questions based on them.

What this means is that questions following a script-generated one can still have logic, but they can only reference other question types, not the script-generated question itself.
