Samuel
Samuel

Reputation: 103

IMacros Reading/Writing From/To CSV File

I've written a Java code in IMacros Add-on for Firefox that gets information from CSV file. The javascript is used to control one macros from another. Everything was working great until I thought to improve it. The structure of the CSV file is the following:

LASTLINEUSED, NUMBEROFLINES
 4,10 
 NAME, SURNAME
 A,B 
 C,D 
 E,F 
 ... 
 M,N

I've tried to count the number of lines in CSV file but ended with manually specifying it in the file.

The problem is (using java-script macros, there are 2 java-scripts the first controls the second):

1) Reading information from the CSV file and writing it to the variable

2) Use this variable as a loop counter and in another macros

3) Writing the number of the last used line to the CSV file so on the next startup it will continue from where it stopped

While I successfully was able to read the information from CSV file everything else become complicated. I've tried in different ways but somehow now it reads only the first line of file if to specify SET !DATASOURCE_LINE 1 or SET !DATASOURCE_LINE 3 and doesn't do anything at all if the line is 2 or 4.

If to put all the code in one java-script and to call sub-macros in specified order everything was working before. But if to call the second java-script from the first the problem with passing variables between them arises (as well as if to call sub-macros not in special order).

The thing that in IMacros version I'm using you can't see the variable values while executing the program so debugging is very limited everything becomes very complicated.
Here is the code:

var lastUsedLine;
lastUsedLine = "CODE:";
lastUsedLine += "SET !ERRORIGNORE YES" + "\n";
lastUsedLine += "SET !TIMEOUT_TAG 3" + "\n";
lastUsedLine += "SET !TIMEOUT_STEP 3" + "\n";
lastUsedLine += "SET !DATASOURCE MyInformation.csv" + "\n";
lastUsedLine += "SET !DATASOURCE_LINE 2" + "\n";
lastUsedLine += "ADD !EXTRACT {{!COL1}}" + "\n";
lastUsedLine += "ADD !EXTRACT {{!COL2}}" + "\n";
iimPlay(lastUsedLine);

var currentLine = iimGetLastExtract(1);
var numberOfLines = iimGetLastExtract(2);

for (a = currentLine; a < (numberOfLines + 1); a++)
{
iimPlay(macro1);
//iimSet ("CURRENTLINE", a); 
//--- This CURRENTLINE worked before but only if it is placed one line before the sub-macro where it will be used
iimPlay(processingInformation);
}

var processingInformation;
processingInformation = "CODE:";
processingInformation += "SET !ERRORIGNORE YES" + "\n";
processingInformation += "SET !TIMEOUT_TAG 3" + "\n";
processingInformation += "SET !TIMEOUT_STEP 3" + "\n";
processingInformation += "SET !TIMEOUT_PAGE 60" + "\n";
processingInformation += "TAB T=1" + "\n";
processingInformation += "TAB CLOSEALLOTHERS" + "\n";
processingInformation += "SET !DATASOURCE MyInformation.csv" + "\n";
processingInformation += "SET !DATASOURCE_LINE {{currentLine}}" + "\n";
processingInformation += "URL GOTO=http://www.example.com/" + "\n" ;
processingInformation += "TAG POS=1 TYPE=INPUT:Name ATTR=ID:id1 CONTENT={{!COL1}}" + "\n" ;
processingInformation += "TAG POS=1 TYPE=INPUT:SURNAME ATTR=ID:id2 CONTENT={{!COL2}}" + "\n" ;
processingInformation += "TAG POS=1 TYPE=INPUT:SUBMIT ATTR=ID:done" + "\n" ;

The For loop will be placed in another java-script macro that will call the second where everything else is specified. Writing value to the CSV file I haven't implemented yet because of the problems above.

Thank you for your help!

Upvotes: 0

Views: 1525

Answers (1)

Shugar
Shugar

Reputation: 5299

Let the structure of your CSV file be the following:

NAME, SURNAME
A,B 
C,D 
E,F 
... 
M,N

Try how the js-script below works and modify it for your needs:

var lastUsedLine = "SET !DATASOURCE MyInformation.csv" + "\n";
lastUsedLine += "SET !DATASOURCE_LINE {{currentLine}}" + "\n";
lastUsedLine += "SET !EXTRACT {{!COL1}}" + "\n";
lastUsedLine += "ADD !EXTRACT {{!COL2}}" + "\n";

var processingInformation = "SET !ERRORIGNORE YES" + "\n";
processingInformation += "SET !TIMEOUT_STEP 3" + "\n";
processingInformation += "SET !TIMEOUT_PAGE 60" + "\n";
processingInformation += "TAB T=1" + "\n";
processingInformation += "TAB CLOSEALLOTHERS" + "\n";
processingInformation += "URL GOTO=http://www.example.com/" + "\n" ;
processingInformation += "TAG POS=1 TYPE=INPUT:Name ATTR=ID:id1 CONTENT={{name}}" + "\n" ;
processingInformation += "TAG POS=1 TYPE=INPUT:SURNAME ATTR=ID:id2 CONTENT={{surname}}" + "\n" ;
processingInformation += "TAG POS=1 TYPE=INPUT:SUBMIT ATTR=ID:done" + "\n" ;

for (i = 2; ; i++) {
    iimSet("currentLine", i);
    if (iimPlayCode(lastUsedLine) != 1)
        break;
    var ext = iimGetExtract().split("[EXTRACT]");
    iimDisplay(i + " >>>   " + ext.join("  "));

    iimSet("name", ext[0]);
    iimSet("surname", ext[1]);
    iimPlayCode(processingInformation);
}

Upvotes: 1

Related Questions