2021.1

Table Of Contents
Using a script
A script could also provide a solution when data needs to be extracted from a variable region.
This requires using a Javascript-based field.
1. Add a field to an Extract step, preferably by extracting data from one of the possible
regions; see "Extracting data" on page231. To add a field without extracting data, see
"Expression-based field" on page273.
2. On the Step properties pane, under Field Definition, select the field and change its Mode
to Javascript.
If the field was created with its Mode set to Location, you will see that the script already
contains one line of code to extract data from the original location.
3. Expand the script. Start by doing the check(s) to determine where the data that needs to
be extracted is located. Use the data.extract() function to extract the data. The
parameters that this function expects depend on the data source, see "extract()" on
page401.
Example
The following script extracts data from a certain region in a Text file; let's assume that this
region contains the unit price. If the unit price is empty (after trimming any spaces), the product
description has to be extracted from two lines; else the product description should be extracted
from one line.
var s = data.extract(1,7,1,2,"");
if (s.substring(1,3).trim().length == 0)
{ data.extract(12,37,0,2,""); } /* extract two lines */
else { data.extract(12,37,0,1,""); } /* extract one line */
The fourth parameter of the extract() function contains the height of the region. When working
with a Text file, this equals a number of lines.
Tip
With a Text file, the data.extract() method accepts 0 as its height parameter. With the
height set to 0 it extracts all lines starting from the given position until the end of the
record.
Page 254