Export module
đ Descriptionâ
The Export
module contains helper functions for exporting data from form instances. When you run a Tripetto form, all the data that is collected by the form is stored inside the form instance. When you want to retrieve the collected data, you need one of the functions in this module to do so.
The most common used function for data extraction is exportables
which retrieves all exportable data from the form. Exportable data is all the data marked by the form creator as exportable. This is the response data that should be saved in most cases.
âšī¸ Exportable vs. actionable dataâ
Tripetto supports two types of data collections that are retrieved from a form:
- Exportable data: These are all the fields in the form that are marked as exportable by the form creator;
- Actionable data: These fields are generated by action blocks that can perform certain actions like sending an email message.
The exportable data is the most important data collection as it includes all the fields that usually need to be stored. Form creators have the ability to specify if a block should be included in the data export or not. By default, most blocks are. There is a special function called exportables
to make it easy to retrieve those data fields. So, a common setup would be to retrieve this data collection and then submit it to an endpoint as this is the response data that should be saved in most cases.
The actionable data, on the other hand, is volatile data required for performing actions when the form completes. It often needs to be transferred to an API or endpoint to allow further processing. One of the most common actions is post-processing the Mailer block. This block allows form creators to compose email messages in the form. It is a powerful feature, but it needs additional work to get it up and running properly.
Please have a look at the Post-processing actions guide for more information about actionable data and how to process it.
đ Functions overviewâ
The following table shows the available data export functions and a description of what they do:
Function | Description |
---|---|
exportables | Exports all exportable fields (fields in the form that are marked as exportable by the form creator). This is the de facto function for retrieving response data from a form. |
exportablesWithData | Exports all exportable fields that actually contain data. |
exportablesDiff | Exports all exportable fields whose value has been changed. |
dataset | Exports all fields to a dataset object. |
typedDataset | Exports all fields to a statically typed recursive dataset object. |
NVPs | Exports all exportable fields as name-value pairs. |
CSV | Exports all exportable fields in CSV format. |
CSVWithData | Exports all exportable fields that actually contain data in CSV format. |
CSVDiff | Exports all exportable fields whose value has been changed in CSV format. |
actionables | Exports all actionables fields. |
values | Exports all raw data values from the form. |
fields đī¸ | Exports all exportable fields (deprecated, use exportables instead). |
đŠâđģ Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
// This exports all exportable data in the form
const exportables = Export.exportables(instance);
// Iterate through all the fields
exportables.fields.forEach((field) => {
// Output each field name and value to the console
console.log(`${field.name}: ${field.string}`);
});
}
});
âļī¸ Functionsâ
đ§ actionables
â
Retrieves all actionable fields. These fields are generated by action blocks that can perform certain actions like sending an email message.
More information about actionable data can be found in the Post-processing actions guide. To implement validation of the actionable fields read the Validating response data guide.
Signatureâ
actionables(instanceOrContext: Instance | Context): IActionables | undefined
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
Return valueâ
Returns an IActionables
object with the actionable fields or undefined
if there is no actionable data in the supplied instance or context.
There is a JSON Schema available for the IActionables
object (handy for static validation). You can download it here.
đ§ CSV
â
Retrieves the exportables fields in CSV format.
Signatureâ
CSV(
instanceOrContext: Instance | Context,
selection?: string[],
separator?: string,
delimiter?: string
): ICSV
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
selection | string[] | Yes | Specifies the identifiers of the slots that should be included (see exportables for more information). |
separator | string | Yes | Specifies the string that is used to separate subject and field names for slots that are part of an iteration (default is / , see exportables for more information). |
delimiter | string | Yes | Specifies the delimiter for the CSV output (default is , ). |
Return valueâ
Returns an ICSV
object with the CSV fields header and data record.
Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
// Retrieve the CSV output
const csv = Export.CSV(instance);
// Output CSV to the console
console.log(csv.fields);
console.log(csv.record);
}
});
đ§ CSVDiff
â
Retrieves all exportable fields that have changed during the lifecycle of the form in CSV format. It is identical to the CSV
function, with the only difference that it only includes the fields that are changed. This is useful, for example, when a form is prefilled with data and you are only interested in the fields that have been changed by the form respondent.
Signatureâ
CSVDiff(
instanceOrContext: Instance | Context,
selection?: string[],
separator?: string,
delimiter?: string
): ICSV
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
selection | string[] | Yes | Specifies the identifiers of the slots that should be included (see exportables for more information). |
separator | string | Yes | Specifies the string that is used to separate subject and field names for slots that are part of an iteration (default is / , see exportables for more information). |
delimiter | string | Yes | Specifies the delimiter for the CSV output (default is , ). |
Return valueâ
Returns an ICSV
object with the CSV fields header and data record.
Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
// Retrieve the CSV output
const csv = Export.CSVDiff(instance);
// Output CSV to the console
console.log(csv.fields);
console.log(csv.record);
}
});
đ§ CSVWithData
â
Retrieves all exportable fields that have a value set (and therefore contain data) in CSV format. It is identical to the CSV
function, with the only difference that it only includes fields that contain data (fields that are filled in by the form respondent).
Signatureâ
CSVWithData(
instanceOrContext: Instance | Context,
selection?: string[],
separator?: string,
delimiter?: string
): ICSV
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
selection | string[] | Yes | Specifies the identifiers of the slots that should be included (see exportables for more information). |
separator | string | Yes | Specifies the string that is used to separate subject and field names for slots that are part of an iteration (default is / , see exportables for more information). |
delimiter | string | Yes | Specifies the delimiter for the CSV output (default is , ). |
Return valueâ
Returns an ICSV
object with the CSV fields header and data record.
Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
// Retrieve the CSV output
const csv = Export.CSVWithData(instance);
// Output CSV to the console
console.log(csv.fields);
console.log(csv.record);
}
});
đ§ dataset
â
Exports all fields as a dataset object. A dataset is a flat list of all the fields of a form, where the name (or alias) of each field is used as key. When a field is part of a section or subform that has an alias set, that alias is used to prefix the key of the field. Multiple aliases are separated using the supplied separator.
When there are fields with duplicate keys (for example, fields that have the same alias set), then the names of consecutive fields are suffixed (for example, Field name (2)
).
Signatureâ
dataset(
instanceOrContext: Instance | Context,
separator?: string,
escape?: boolean
): IDataset
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
separator | string | Yes | Specifies the string that is used to separate multiple field names and aliases (default is / ). |
escape | boolean | Yes | Specifies if the field names needs to be escaped to prevent the use of the separator in the field names. Escaping is only possible when a single-character separator is used and works by prepending the character with a backslash (default is true ). |
Return valueâ
Returns an IDataset
object with the fields.
Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
const dataset = Export.dataset(instance);
// Let's assume the form has a text block with alias `name`
// Now we can access the value for that block in the dataset
console.log(dataset.name.value);
}
});
đ§ exportables
â
Retrieves an IExportables
object with all exportable fields. These are all the fields in the form that are marked as exportable by the form creator. Tripetto uses a slot system to store data. Each block in a form generates one or more slots, and these slots are then filled with values as the respondent fills out the form. This function exports all data slots regardless of whether a value is set for a particular slot or not. If you only want to retrieve the slots that have a value set, use the exportablesWithData
function.
If you want, you can filter the desired fields by entering the slot identifiers of the fields you want to export in the selection
argument. The separator
argument is there to separate field names and is useful for slots that are part of an iteration. Tripetto allows the use of iterations to ask the same set of questions multiple times for multiple subjects. The field names of those fields will then be prepended with the name of the current subject in the iteration (also known as the context of a field). This prevents multiple values with the same value name in the data set. The subject and field name is separated with the specified separator string (that defaults to /
).
To implement validation of the exportable fields read the Validating response data guide.
Signatureâ
exportables(
instanceOrContext: Instance | Context,
selection?: string[],
separator?: string,
escape?: boolean
): IExportables
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
selection | string[] | Yes | Specifies the identifiers of the slots that should be included. |
separator | string | Yes | Specifies the string that is used to separate subject and field names for slots that are part of an iteration (default is / ). |
escape | boolean | Yes | Specifies if the field names needs to be escaped to prevent the use of the separator in the field names. Escaping is only possible when a single-character separator is used and works by prepending the character with a backslash (default is false ). |
Return valueâ
Returns an IExportables
object with the exportable fields.
There is a JSON Schema available for the IExportables
object (handy for static validation). You can download it here.
Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
const exportables = Export.exportables(instance);
// Iterate through all the fields
exportables.fields.forEach((field) => {
// Output each field name and value to the console
console.log(`${field.name}: ${field.string}`);
});
}
});
đ§ exportablesDiff
â
Retrieves an IExportables
object containing all exportable fields that have changed during the lifecycle of the form. It is identical to the exportables
function, with the only difference that it only includes the fields that are changed. This is useful, for example, when a form is prefilled with data and you are only interested in the fields that have been changed by the form respondent.
Signatureâ
exportablesDiff(
instanceOrContext: Instance | Context,
selection?: string[],
separator?: string,
escape?: boolean
): IExportables
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
selection | string[] | Yes | Specifies the identifiers of the slots that should be included (see exportables for more information). |
separator | string | Yes | Specifies the string that is used to separate subject and field names for slots that are part of an iteration (default is / , see exportables for more information). |
escape | boolean | Yes | Specifies if the field names needs to be escaped to prevent the use of the separator in the field names. Escaping is only possible when a single-character separator is used and works by prepending the character with a backslash (default is false ). |
Return valueâ
Returns an IExportables
object with the exportable fields.
There is a JSON Schema available for the IExportables
object (handy for static validation). You can download it here.
Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
const exportables = Export.exportablesDiff(instance);
// Iterate through all the fields that contain data
exportables.fields.forEach((field) => {
// Output each field name and value to the console
console.log(`${field.name}: ${field.string}`);
});
}
});
đ§ exportablesWithData
â
Retrieves an IExportables
object containing all exportable fields that have a value set and therefore contain data. It is identical to the exportables
function, with the only difference that it only includes fields that contain data (fields that are filled in by the form respondent).
Signatureâ
exportablesWithData(
instanceOrContext: Instance | Context,
selection?: string[],
separator?: string,
escape?: boolean
): IExportables
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
selection | string[] | Yes | Specifies the identifiers of the slots that should be included (see exportables for more information). |
separator | string | Yes | Specifies the string that is used to separate subject and field names for slots that are part of an iteration (default is / , see exportables for more information). |
escape | boolean | Yes | Specifies if the field names needs to be escaped to prevent the use of the separator in the field names. Escaping is only possible when a single-character separator is used and works by prepending the character with a backslash (default is false ). |
Return valueâ
Returns an IExportables
object with the exportable fields.
There is a JSON Schema available for the IExportables
object (handy for static validation). You can download it here.
Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
const exportables = Export.exportablesWithData(instance);
// Iterate through all the fields that contain data
exportables.fields.forEach((field) => {
// Output each field name and value to the console
console.log(`${field.name}: ${field.string}`);
});
}
});
đ§ fields
đī¸â
This function is deprecated and here for legacy purposes. Please use the exportables
function instead.
Retrieves an IExportables
object with all exportable fields. These are all the fields in the form that are marked as exportable by the form creator. Tripetto uses a slot system to store data. Each block in a form generates one or more slots, and these slots are then filled with values as the respondent fills out the form. This function exports all data slots regardless of whether a value is set for a particular slot or not.
Signatureâ
fields(
instanceOrContext: Instance | Context,
selection?: string[],
separator: string,
escape?: boolean
): IExportables
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
selection | string[] | Yes | Specifies the identifiers of the slots that should be included. |
separator | string | Yes | Specifies the string that is used to separate subject and field names for slots that are part of an iteration (default is / ). |
escape | boolean | Yes | Specifies if the field names needs to be escaped to prevent the use of the separator in the field names. Escaping is only possible when a single-character separator is used and works by prepending the character with a backslash (default is false ). |
Return valueâ
Returns an IExportables
object with the exportable fields.
There is a JSON Schema available for the IExportables
object (handy for static validation). You can download it here.
đ§ NVPs
â
Export the exportable fields as name-value pairs. If the modifier
argument is omitted, the type of the values is either string
, boolean
, number
or undefined
. If modifier
is set to strings
all values will cast to a string value. So then the value type is always string
. You can also supply a custom modifier function that receives an IExportableField
object for each field as input and should return one of the supported primitive types as output.
Signatureâ
NVPs(
input: Instance | Context | IExportables,
modifier?: "strings" | (field: IExportableField) => string | boolean | number | undefined
): {
[name: string]: string | boolean | number | undefined;
}
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
input | Instance | Context | IExportables | No | Reference to the instance, context or an exportables object. |
modifier | "strings" | (field: IExportableField) => string | boolean | number | undefined | Yes | Specifies a modifier for the value of each pair. It can be one of the following values: - strings : Will cast each value to a string;- Custom modifier function: Receives an IExportableField object for each field as input and should return one of the supported primitive types as output. |
Return valueâ
Returns an object with name-value pairs.
Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
// Retrieve the name-value pairs
const values = Export.NVPs(instance);
// Output the values to the console
console.dir(values);
// Or use a custom modifier
const custom = Export.NVPs(instance, (field) => `has value '${field.string}'`);
}
});
There is only one version of the NVPs
function and there are no separate functions for retrieving the NVPs of all exportable fields with data (exportablesWithData
) or the changed fields (exportablesDiff
). Instead the NVPs
function accepts the output of one of the exportables functions as input. So you can do this:
// Retrieve name-value pairs for all fields that contain data
const fieldsWithData = Export.NVPs(Export.exportablesWithData(instance));
// Retrieve name-value pairs for all fields that have been changed
const changedData = Export.NVPs(Export.exportablesDiff(instance));
đ§ typedDataset
â
Exports all fields to a statically typed recursive dataset object.
This function is generic and allows to specify the type of dataset you are expecting from your form. Since this is a loose binding, you should always verify that a field actually exists in the returned dataset. To force this, the function automatically marks all properties in the returned object as optional. You can use optional chaining to easily check if a value is present or not.
When there are fields with duplicate keys (for example, fields that have the same alias set), then the names of consecutive fields are suffixed (for example, Field name (2)
).
Signatureâ
typedDataset<T extends ITypedDataset>(
instanceOrContext: Instance | Context,
): T
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
Return valueâ
Returns an ITypedDataset
derived object with the fields.
All the properties in the returned dataset object are marked as optional and readonly.
Exampleâ
import { run } from "@tripetto/runner-autoscroll";
import { Export } from "@tripetto/runner";
run({
definition: /* Supply your form definition here */,
onSubmit: (instance) => {
// Let's assume we have a form that has a text block with alias `name`, and
// a subform with alias `order` that has a number block with alias `amount` and `total`.
const dataset = Export.typedDataset<{
name: Export.IExportableField;
order: {
amount: Export.IExportableField;
// You can also pick the properties you need from `IExportableField`:
total: {
value: number;
}
}
}>(instance);
// We can use optional chaining to assure we have a valid value.
console.log(dataset.order?.amount?.value || "Oh noes!");
// Or drilldown with if statements
if (dataset.order?.total) {
console.log(dataset.order.total.value);
}
}
});
đ§ values
â
Retrieves a collection of all valid values. The values are stored per slot. The key of each value object is the identifier of the slot.
Signatureâ
values(instanceOrContext: Instance | Context, selection?: string[]): IValues
Parametersâ
Name | Type | Optional | Description |
---|---|---|---|
instanceOrContext | Instance | Context | No | Reference to the instance or context. |
selection | string[] | Yes | Specifies the identifiers of the slots that should be included. |
Return valueâ
Returns an IValues
object with the values.
Only use the values
function if you have good reason for it. If you can use the exportables
function, always go with that. The values
function returns more information, but also requires more processing and understanding to handle properly.
âī¸ Interfacesâ
đ IActionableData
â
Describes the interface for a data item in the IActionableNode
interface.
Type declarationâ
interface IActionableData { key: string;Readonly name: string;Readonly slot: string;Readonly datatype: string;Readonly string: string;Readonly value: any;Readonly modified: boolean;Readonly reference: string | undefined;Readonly time: number | undefined;Readonly }
đˇī¸ datatype
â
Contains the slot data type.
Typeâ
string
đˇī¸ key
â
A unique key for the data field.
Typeâ
string
đˇī¸ modified
â
Contains if the data was modified.
Typeâ
boolean
đˇī¸ name
â
Contains the name.
Typeâ
string
đˇī¸ reference
â
Contains the data reference.
Typeâ
string | undefined
đˇī¸ slot
â
Contains the slot reference.
Typeâ
string
đˇī¸ string
â
Contains the data as a string.
Typeâ
string
đˇī¸ time
â
Contains the UTC set time of the data.
Typeâ
number | undefined
đˇī¸ value
â
Contains the data value.
Typeâ
any
đ IActionableNode
â
Describes the interface for a single node in the IActionables
interface.
Type declarationâ
interface IActionableNode { key: string;Readonly type: string;Readonly version: string;Readonly node: IExportableFieldNode;Readonly data: IActionableData[];Readonly }
đˇī¸ data
â
Contains the data array.
Typeâ
đˇī¸ key
â
Contains the node key.
Typeâ
string
đˇī¸ node
â
Contains the node.
Typeâ
đˇī¸ type
â
Contains the block type identifier.
Typeâ
string