Exchange Scripts - Export to CSV

This topic contains an example script for exporting data from an InfoAsset Manager network to 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 export  Data to using the csv_export function
#
begin
        puts 'Start InfoAsset Manager Export'
        # open an InfoAsset Manager database
        #
        db = WSApplication.open(nil,false)  # nil indicates the last opened database
        # get the network from the object type and id
        #
        nw = db.model_object_from_type_and_id('Collection Network',23)
        # check for changes made by other users
        current_commit_id = nw.current_commit_id
        latest_commit_id = nw.latest_commit_id
             if(latest_commit_id > current_commit_id) then
                     puts "Updating from Commit ID #{current_commit_id} to Commit ID #{latest_commit_id}"
                     # get all changes in the latest version of the network
                     nw.update
              else
                     puts 'Network is up to date'
              end
              # define the options for csv export, however it not necessary to
              # specify an option when using a default option
              options = Hash.new
              # set bool options
              options['Use Display Precision'] = true     # default=true
              options['Field Descriptions']    = false    # default=false
              options['Field Names']           = true     # default=true    
              options['Flag Fields']           = true     # default=true
              options['Multiple Files']        = false    # default=false
              options['User Units']            = false    # default=false
              options['Object Types']          = false    # default=false

              options['Units Text']            = false    # default=false
              options['Selection Only']        = false    # export selected objects only?
              # set string options
              options['Coordinate Arrays Format'] = 'Unpacked'   # values='Packed'(default), 'None', 'Unpacked'
              options['Other Arrays Format']      = 'Separate'   # values='Packed'(default), 'None', 'Separate'
              # export network data to csv format
              #
              nw.csv_export(
                   'norcross_network.csv',  # export to file name
                   options)                 # export options
              puts 'Done'
# handle exceptions
#
rescue Exception => exception
        puts "[#{exception.backtrace}] #{exception.to_s}"
end

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

Update network to include other users changes

Before exporting data ensure that the network is up to date so that changes by other users are included in the export.

Example

#check for changes made by other users
        current_commit_id = nw.current_commit_id
        latest_commit_id = nw.latest_commit_id
             if(latest_commit_id > current_commit_id) then
                     puts "Updating from Commit ID #{current_commit_id} to Commit ID #{latest_commit_id}"
                     # get all changes in the latest version of the network
                     nw.update
             else
                     puts 'Network is up to date'
end

Where:

CSV export options

The CSV export options are defined in a hash. The options correspond to the options that are set from the Select CSV Export Options dialog n the main application.

e.g.

options=Hash.new
options['Multiple Files'] = true		

Option Type

Key

Default

Further Information

Select CSV Export Options Dialog Option

Boolean

'Use Display Precision'

TRUE

Use display precision for numbers

Boolean

'Field Descriptions'

FALSE

Include field descriptions

Boolean

'Field Names'

TRUE

Include database field names

Boolean

'Flag Fields'

TRUE

Include flag fields

Boolean

'Multipile Files'

FALSE

Set to true to export to different files, false to export to the same file

Export each table to

Boolean

'User Units'

FALSE

Use InfoAsset Manager native units

Boolean

'Object Types'

FALSE

Include object type column

Boolean

'Selection Only'

FALSE

To create a selection for export use:

nw.select_sql(<table>,<sql query>)

Where:

  • table is InfoAsset Manager table name

e.g.

nw.select_sql('pipe','location IsNull')

(See the Using SQL for further information on SQL Queries in InfoAsset Manager)

Export selection only

Boolean

'Units Text'

FALSE

Include unit descriptions

String

'Coordinate Arrays Format'

PACKED

Possible values are "Packed", "None", "Unpacked"

Coordinate Arrays Format

String

'Other Arrays Format'

PACKED

Possible values are "Packed", "None", "Separate"

Other Arrays Format

Export from network to CSV format

Export network to CSV file:

nw.csv_export(<filename>,options)

Where: