Importing Data Using Scripts |
Accessing and Saving Data > Importing and Exporting Data > Importing Data Using Scripts
At times, it can useful to create a script to transform and import an external data set instead of using the other options for opening, connecting to or importing data. This section provides a very brief sample script and related notes to get you started with an import. Please see the scripting section for further information on using scripts.
Below is a sample script that defines the fields of an external data set and then imports the table into Strata:
var def = { type: "fixed", row_width: 15,
fields: [ {source_offset: 0, source_width: 2, name: "FIELD1", type: DbType.Character, width: 2, scale: 0 }, {source_offset: 2, source_width: 2, name: "FIELD2", type: DbType.Character, width: 2, scale: 0 }, {source_offset: 4, source_width: 4, name: "FIELD3", type: DbType.Character, width: 4, scale: 0 }, {source_offset: 8, source_width: 6, name: "FIELD4", type: DbType.Character, width: 6, scale: 0 }, {source_offset: 14, source_width: 1, name: "FIELD5", type: DbType.Character, width: 1, scale: 0 }, {source_offset: 14, source_width: 1, name: "FIELD6", type: DbType.Character, width: 1, scale: 0, formula: "UPPER(FIELD5)" } ] };
// assign a layout to the file HostServices.data.assignDefinition("c:\\users\\myname\\sample.txt", def);
// optionally import this file into a table called "tbl" var db = HostApp.getDatabase(); db.execute("SELECT * INTO TABLE tbl FROM file://c:/users/myname/sample.txt"); HostApp.refresh();
alert("done");
When creating your script, please note the following definitions:
|