Exchange Scripts - Import from CSV

This topic contains an example script for importing data to an InfoAsset Manager network from a CSV file using InfoAsset Manager Exchange. This example only works for the single file method.

The example below provides a text version of the example script that can be copied and pasted into a text file:

Example

#
# Example Script to update InfoAsset Manager Data using the csv_import function
#
begin
        puts 'Start InfoAsset Manager CSV Import'
        # open an InfoAsset Manager database
        #
        db = WSApplication.open('C:\MyDatabase.icmm',false)
        # get the network from the object type and id
        # reserve the network to prevent other users from editing it
        #
        nw.reserve
        # define the options for csv import, however it not necessary to
        # specify an option when using a default option
     
        # define the options for csv import, however it not necessary to
        # specify an option when using a default option
        options = Hash.new
        # set bool options
        options['Force Link Rename']     =  true    # default=true
        options['Flag Genuine Only']     = false    # default=false
        options['Load Null Fields']      = true     # default=true    
        options['Update With Any Flag']  = true     # default=true
        options['Use Asset ID']          = false    # default=false
        options['User Units']            = false    # default=true
        options['UK Dates']              = false    # default=false
        # set string options
        options['Action'] = 'Mixed'  # values='Mixed'(default), 'Update And Add', 'Update Only', 'Delete'
        options['Header'] = 'ID'     # values='ID'(default), 'ID Description', 'ID Description Units', 'ID Units'
        # set flag options
        options['New Flag']    = 'NF'   # a two character flag ID
        options['Update Flag'] = 'CF'   # a two character flag ID
        # import network data from csv format
        #
        nw.csv_import(
             'C:\Malden.csv',  # import from file name
             options)                 # import options
        # commit changes and unreserve the network
        #
        nw.commit('Imported data via csv_import - test where this goes')
        puts 'Done'
# handle exceptions
#
rescue Exception => exception
        puts "[#{exception.backtrace}] #{exception.to_s}"
ensure
        # unreserve the network on error, only if it has been reserved
        nw.unreserve if nw
end

Additional information about each section of the example script (shown below) can be found in the following sections:

CSV import options

The CSV import options are defined in a hash. The options correspond to the options that are set from the Import / Update CSV Data dialog in the main application.

e.g.

options=Hash.new
options['Use Asset ID'] = true

Option Type

Key

Default

Further Information

Import/Update CSV Data Dialog Option

Boolean

'Force Link Rename'

TRUE

Renaming of node forces rename of connected links

Boolean

'Flag Genuine Only'

FALSE

Only flag genuine changes to data values

Boolean

'Load Null Fields'

TRUE

Allow blank fields in CSV to overwrite existing data

Boolean

'Update With Any Flag'

TRUE

Set to true to update any data, false to update only those fields with the 'Update Flag'

Only update existing data that has flag

Boolean

'Use Asset ID'

FALSE

Update uses 'Asset ID' field to identify data items

Boolean

'User Units'

TRUE

Set to true for User Units, false for Native Units

Unspecified units are:

Boolean

'UK Dates'

FALSE

If set to true, the import is done with the UK locale for dates regardless of the PC dates.

Not on dialog

String

'Action'

MIXED

Possible values are "Mixed", "Update And Add", "Update Only", "Delete"

Type of update

String

'Header'

ID

Possible values are "ID", "ID Description", "ID Description Units", "ID Units"

Block Headers

String

(2 chars)

'New Flag'

-

Flag to use for new/update data

String

(2 chars)

'Update Flag'

-

(Flag to use for) Only update existing data that has flag

Import to network from CSV format

Import to network from CSV file:

nw.csv_import(<filename>,options)

Where: