Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Fill multi line form from CSV


  • Please log in to reply
1 reply to this topic
Craig_Youngman
  • Members
  • 1 posts
  • Last active: Oct 29 2015 08:31 PM
  • Joined: 29 Oct 2015

I have a application form that I need to fill out using data from a CSV file. It is a multi-line form, It has 2 fields to fill in and 3 fields I need to [Tab] through. Then loop and enter the next row of data. 

 

Specifically it is a purchase order form that has ITEM, QTY, Cost, discount_type, discount_amount.

I will have a CSV file with ITEM, QTY that I want to fill the form in with then skip the other fields with [TAB]. Then I need AHK to read the next line in the CSV and repeat the process until the end of the CSV file is reached. 

 

It looks like this is possible but I can't figure it out. 



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014

A few questions.

 

1) Is it really a CSV file or do you export it to CSV from Excel.

 

AHK by itself can only read very simple CSV files,

 

If you have an Excel file it would be advised to work from Excel directly as CSV can be very trick to get right - see https://en.wikipedia..._values#Example for example (note the "" and , IN the cells)

 

If you have Excel: here is a tutorial for reading data from Excel http://autohotkey.co...rial-for-excel/

If you really have CSV you could still convert it to Excel by opening it in Excel and work from there.

 

If you don't have Excel and thus only have the CSV you can read it with AHK using a parsing loop http://ahkscript.org...s/LoopParse.htm - look for CSV on the page. If it is a very complex CSV and the parsing loop fails there are some additional libraries which may work:

a) http://autohotkey.co...sv-library-lib/

B) https://github.com/JnLlnd/ObjCSV

 

2) Is the form actually a web page (brower) or another program?

 

If it is a webpage and you can work with Internet Explorer, you can use the same COM technique as with Excel and "write" the data to the correct fields.

 

If it is another program you can try to send the data and TAB to the next field, but you can also try to send the data to the fields directly if AHK can see them. You can check that with the Window Spy. Right click on the AHK tray icon, Window Spy and move your mouse over the "fields" (controls) in your program. Note it won't work with web pages, so only try the Spy with a normal program. If you see stuff like ahk_class Edit1 something like that AHK can see it, if there no name AHK might not be able to see the controls and you would have to use SEND/TAB.

 

The above sounds complicated but it doesn't have to be, break it down in steps:

 1 - how to read the data and each cell

 2 - how to send the data

 

It could be as simple as this (not very reliable) but just to show you it doesn't have to be too complicated

; FileRead, data, file.csv
data= ; just to have some test data
(join`r`n
ITEM, QTY, Cost, discount_type, discount_amount
1,1,"18,220.74",Type1,"5,000"
2,6,14,Type2,20
3,1,8,Type3,30
)

Loop, parse, data, `n, `r
    {
     If (A_Index = 1) ; if you have a header row in your CSV you can skip the first line
         continue
     Loop, parse, A_LoopField, CSV
        { ; we want cells 1 & 2
         if (A_Index = 1) or (A_Index = 2)
            MsgBox % A_LoopField ; Send %A_LoopField%{tab}
         else
            MsgBox We skip cell %A_Index% ; Send {tab}    
        }
    }