> For the complete documentation index, see [llms.txt](https://docs.datasaur.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datasaur.ai/advanced/script-generated-question.md).

# 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](/files/wrZcd4ObKw0lr8FZ5ZFR)

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](/data-studio-projects/lets-get-labeling/label-sets.md#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.

![](/files/vT0DzTicrosKNaVBufzn)

![](/files/lA8oWdrS3dN90nvgIKo0)

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

{% file src="/files/242pkAHNwlNkpZlnUhKZ" %}

{% file src="/files/29qV2UhkOvkjUWWDhxkN" %}

## 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.
