Input from Comma Separated Data (CSV) File
Top  Previous  Next

Related example macros: Demo-ReadCSV
Related example script: CSV-2-web.vbs, Database-2-web.vbs

iMacros allows you to specify a text file with comma separated values to be used as input. Imagine, for example, that you want to submit a list of CD's to an online auction. Here is the list of the CD's in the comma separated format:

"ARTIST"
 "ALBUM TITLE" , "PRICE"
"Beatles""Abbey Road""13.49"
"Beatles""The Beatles 1,2,3" , "25.49"
"Mozart" , "Symphonies No.40 & 41""9.98"
"
Mozart""Requiem""7.50"

Note: Quotation marks are optional in most cases. They are only required if the value itself contains a comma.

We now need to tell the iMacros macro where the data input file can be found. For that, we use the built-in variable !DATASOURCE

SET !DATASOURCE OnlineAuction.csv

If you do not use any path information (like
C:\myPath\) in the !DATASOURCE value, the file is assumed to lie in the standard datasources directory, which can be specified in the Paths tab of the Options dialog. The default directory is in the datasources\ directory of your iMacros installation (e.g. C:\Program Files\iMacros\datasources\).

We then need to tell iMacros how many columns the CSV file has in each line. We do that by using the
!DATASOURCE_COLUMNS variable:

SET !DATASOURCE_COLUMNS 3

This number must match the exact number of columns in the input file, even if you do not use some columns.

Since we want to insert all datasets into the form, we need to loop over the macro, each time inserting the next CD. Therefore, we need to tell iMacros in which line of the datasource we currently are. We do this using the built-in variable
!DATASOURCE_LINE. By cunningly using the built-in variable !LOOP, we let iMacros take care of the counting:

SET !DATASOURCE_LINE {{!LOOP}}

Now we can have the macro fill out the online form with the values from the current CD dataset. We use the built-in variables
!COLn, where n represents the number of the columns to put into the form element.

TAG TYPE=INPUT:TEXT FORM=Listing ATTR=NAME:Name CONTENT={{!COL1}}  
TAG TYPE=INPUT:TEXT FORM=Listing ATTR=NAME:Album CONTENT={{!COL2}}
  
TAG TYPE=INPUT:TEXT FORM=Listing ATTR=NAME:Price CONTENT={{!COL3}}
  

During the execution of the macro, the constants in parentheses
{{..}} are replaced by the value specified in the data sources.




Page URL http://www.iopus.com/imacros/help/input_csv.htm