kkr
kkr

Reputation: 63

JavaScript loop issue in iMacros

English isn’t my first language, so please forgive any mistakes.

FCIM : I'm using iMacros v9.0.3 on Firefox 53.0.3 (64 bit) in Windows 8 x64

I'm trying to loop the JavaScript code in iMacros with a Datasource.

It's working fine when it's just iim (iMacro) code.
It perfectly enters the right value in the right row number.
But after I convert the script from iim into JavaScript, it doesn't enter the data correctly.

for eg, in iMacro code when I loop, it rightly enters row by row data:

But after I convert it into JavaScript, it always enters THE SAME DATA over and over again.

Here is my Datasource demo.csv

Here is an iim code example

VERSION BUILD=9030808 RECORDER=FX
TAB T=1
TAB CLOSEALLOTHERS
SET !DATASOURCE demo.csv
SET !DATASOURCE_COLUMNS 8
SET !DATASOURCE_LINE {{!LOOP}}
SET !LOOP 1
URL GOTO=http://demo.imacros.net/Automate/AutoDataEntry
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:fname CONTENT={{!COL1}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:lname CONTENT={{!COL2}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:address CONTENT={{!COL3}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:city CONTENT={{!COL4}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:zip CONTENT={{!COL5}}
TAG POS=1 TYPE=SELECT FORM=ID:demo ATTR=ID:state CONTENT=%IN
TAG POS=1 TYPE=SELECT FORM=ID:demo ATTR=ID:country CONTENT=%73
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:email CONTENT={{!COL8}}
TAG POS=1 TYPE=BUTTON FORM=ID:demo ATTR=TXT:Submit

Here is a Javascript example code

var macro; 
macro =  "CODE:";
for(var X = 1; X <= 10; X++){

  macro +=  "TAB T=1" + "\n";
  macro +=  "TAB CLOSEALLOTHERS" + "\n";
  macro +=  "SET !DATASOURCE demo.csv" + "\n";
  macro +=  "SET !DATASOURCE_COLUMNS 8" + "\n";
  macro +=  "SET !DATASOURCE_LINE {{!LOOP}} " + "\n";
  macro +=  "SET !LOOP 1" + "\n"
  macro +=  "URL GOTO=http://demo.imacros.net/Automate/AutoDataEntry" + "\n";
  macro +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:fname CONTENT={{!COL1}}" + "\n";
  macro +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:lname CONTENT={{!COL2}}" + "\n";
  macro +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:address CONTENT={{!COL3}}" + "\n";
  macro +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:city CONTENT={{!COL4}}" + "\n";
  macro +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:zip CONTENT={{!COL5}}" + "\n";
  macro +=  "TAG POS=1 TYPE=SELECT FORM=ID:demo ATTR=ID:state CONTENT=%IN" + "\n";
  macro +=  "TAG POS=1 TYPE=SELECT FORM=ID:demo ATTR=ID:country CONTENT=%73" + "\n";
  macro +=  "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:email CONTENT={{!COL8}}" + "\n";
  macro +=  "TAG POS=1 TYPE=BUTTON FORM=ID:demo ATTR=TXT:Submit" + "\n";
  iimPlay(macro)

}
iimPlay(macro)

Any ideas on how to make it loop correctly? and thx in advance to all my brothers :)

Upvotes: 0

Views: 362

Answers (1)

dreftymac
dreftymac

Reputation: 32370

Background

  • User PayovYuxug wishes to run auto-generated code which has auto-generated regions that are generated by a loop.

Problem

  • The auto-generated code behaves differently than when simply entered by hand without auto-generation.

Solution

  • Compare the auto-generated code, and determine whether the loop-generated-region includes any unintentionally repeated commands that should only appear once.

Simple example

The following is a simple example demonstrating the issue in pseudo-code

Hand-edited source code

## Code
connection = new DatabaseConnection;
print connection.currentrow.firstname;
print connection.currentrow.lastname;
print connection.currentrow.age;
connection.getNextRow();

## Result
Alice Anderson 33

Generated source code

## Code
mycode = "";
for ixx in range(0 .. 3):
   mycode += "connection = new DatabaseConnection;"  //<--MISTAKE
   mycode += "print connection.currentrow.firstname;"
   mycode += "print connection.currentrow.lastname;"
   mycode += "print connection.currentrow.age;"
   mycode += "connection.getNextRow();"
myApp.run(mycode);

Results from generated source code

## Actual Result
Alice Anderson 33
Alice Anderson 33
Alice Anderson 33
Alice Anderson 33

## Desired Result
Alice Anderson 33
Bob Banderson 44
Charlie Chanderson 55
Danny Danderson 66

## BROKEN ;; Actual Result does not match desired result, because
## the DatabaseConnection gets reset every time.

Fixing the mistake

## Fixed code
mycode = "";
mycode += "connection = new DatabaseConnection;"  //<-- FIXED MISTAKE
for ixx in range(0 .. 3):
   mycode += "print connection.currentrow.firstname;"
   mycode += "print connection.currentrow.lastname;"
   mycode += "print connection.currentrow.age;"
   mycode += "connection.getNextRow();"
myApp.run(mycode);

Case-specific example

The same mistake seems to be repeated here. These lines should not be inside the loop.

SET !DATASOURCE demo.csv
SET !DATASOURCE_COLUMNS 8

Pitfalls

  • For this specific question, there is no need to generate code with JavaScript, because iim already handles iteration over the rows in the data source anyway. Why is there a need to generate code at all?

Upvotes: 1

Related Questions