Script-Generated Question

Overview

This feature only available in Row Labeling project and disabled by default. Please reach out to [email protected] if your team needs this feature, and we'll assist you!

Script-Generated Questions is an advanced question type that allows dynamic question generation based on each row’s data. Using TypeScript, 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 identifiers part of the script.

Identifiers

Each identifier should have the following attributes:

  • id — A unique, zero-indexed integer, signifying the relative order of the questions.

  • label — A human-readable string that represents the question.

  • type — The question type (i.e. text, multiple choice, date).

    • Currently, all question types are supported except Grouped Attributes and Script-Generated Questions, with some caveat.

    • See this for more details on each question type.

  • config.multiple — Configuration that allows a question to have multiple answers.

Example Configuration

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

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
  },
};

Dynamically Generating Questions

The core function that determines which questions appear is getQuestionsForRow. This function runs for each row in the dataset, evaluating its data and returning the appropriate questions.

Tip: You can view available row and columns attributes in the editor by pressing Ctrl / Cmd + Space.

Example Usage

In the following example:

  • A text question appears if the column DATA contains the word "text".

  • Otherwise, a multiple-choice question is shown, with options generated from the values in DATA.

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 })),
      },
    },
  ];

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

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

Limitation

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.

Last updated