添加到JS脚本时,iMacros脚本无法正确循环

时间:2015-02-17 03:42:54

标签: javascript loops firefox-addon imacros

我有一个iMacros脚本在隔离运行时正确循环 - 读取输入表的所有行 - 当我的JS脚本中包含时,它根本不循环。

当我设置“重复宏”'Max:'变量

时,iMacros脚本独立工作
VERSION BUILD=8881205 RECORDER=FX
TAB T=1
SET !DATASOURCE Org_Acc_DK.csv
SET !DATASOURCE_COLUMNS 4
SET !LOOP 2
SET !DATASOURCE_LINE {{!LOOP}}
URL GOTO=http://SOME SITE HERE (replaced the name to protect the guilty)
WAIT Seconds=3
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:form1 ATTR=ID:MainContent_btnNewAcct
TAG POS=1 TYPE=SELECT FORM=ID:form1 ATTR=ID:MainContent_ucAddOrganisationAccount_ddOrgList CONTENT={{!COL1}}
WAIT Seconds=3
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:form1 ATTR=ID:MainContent_ucAddOrganisationAccount_txtOAAccount CONTENT={{!COL2}}
TAG POS=1 TYPE=SELECT FORM=ID:form1 ATTR=ID:MainContent_ucAddOrganisationAccount_ddlOACountry CONTENT=$*{{!COL3}}*
WAIT Seconds=5
TAG POS=1 TYPE=SELECT FORM=ID:form1 ATTR=ID:MainContent_ucAddOrganisationAccount_ddOALessor CONTENT=$*{{!COL4}}*
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:form1 ATTR=ID:btnOASave
WAIT Seconds=3

当我将该脚本添加到以下JS脚本时 - 它只运行一次然后返回到JS脚本并完成。 现在我意识到这是因为我没有将“重复宏”设置为行数。但是我需要添加到JAVA脚本中,只为这个特定的脚本设置重复行而不是之前或之后的重复行?

var i, retcode;
var report;
var macrolist = new Array();


macrolist.push("Create Account/0 Login");
macrolist.push("Create Account/01 Create Organisation And Account.iim");
macrolist.push("Create Account - DK Loop.iim");



iimDisplay("Start Self Test");

report  =  "Self-Test Report\n\n";

for (i = 0; i < macrolist.length; i++) {
iimDisplay("Step "+(i+1)+" of "+macrolist.length + "\nMacro: "+macrolist[i]);
retcode = iimPlay(macrolist[i]);
report += macrolist[i];
if (retcode < 0) {
    report += ": "+iimGetLastError();
} else {
    report += ": OK";

    s = iimGetLastExtract(1);
    if ( s != "" )  report += ", Extract: "+s;
}
report += "\n";
}
iimDisplay("Test complete");

alert ( report );

提前致谢, 道格

1 个答案:

答案 0 :(得分:0)

我的一位非常有帮助的朋友开发了一个Java脚本来完成我所需要的工作。外部调用CSV文件来获取数据将循环遍历工作表,直到使用此脚本找到最后一条记录。

这太好了,我觉得有必要与其他人分享,最终可能和我一样。

您真正需要对iMacros脚本进行的唯一更改是将“POSVAL”添加到“SET!LOOP”命令中,如下所示:

SET !LOOP {{POSVAL}} 

首先,需要表达所有引用的数据源,并确定它们正在使用哪个宏。

var i, retcode, report;
var macro2_file="Org_Acc_info.csv";
var macro3_file="Testfile1.csv";
var macro4_file="Testfile2.csv";
var macro5_file="Details_Tab.csv";

在运行循环脚本之前运行单个登录脚本。

retcode = iimPlay("Create Account/0 Login");
report = "";
if (retcode < 0) report += ": "+iimGetLastError();

然后是循环例程的标题引用代码

var lines_in_file;

然后我想循环使用macro2

循环的各个文件
 lines_in_file = count_rows(get_data_folder()+"\\", macro2_file);

for (i=2; i<=lines_in_file ; i++)
{
  retcode = iimSet("POSVAL",  i);
  retcode = iimPlay("Create Account/01 Create Organisation And Account.iim");
  if (retcode < 0) report += ": "+iimGetLastError();
}

然后是macro3

lines_in_file = count_rows(get_data_folder()+"\\", macro3_file);

for (i=2; i<=lines_in_file ; i++)
{
  retcode = iimSet("POSVAL",  i);
  retcode = iimPlay("Create Account - DK Loop - VBS.iim");
  if (retcode < 0) report += ": "+iimGetLastError();
}

然后是macro4

lines_in_file = count_rows(get_data_folder()+"\\", macro4_file);

for (i=2; i<=lines_in_file ; i++)
{
  retcode = iimSet("POSVAL",  i);
  retcode = iimPlay("Create Account/2 Create Account Address.iim");
  if (retcode < 0) report += ": "+iimGetLastError();
}

然后是macro5

lines_in_file = count_rows(get_data_folder()+"\\", macro5_file);

for (i=2; i<=lines_in_file ; i++)
{
  retcode = iimSet("POSVAL",  i);
  retcode = iimPlay("Create Account/4 Edit Account Details.iim");
  if (retcode < 0) report += ": "+iimGetLastError();
}

然后是循环代码的主体

function get_data_folder()
{
  var code;
  code =  "CODE:";
  code +=  "SET !extract {{!FOLDER_DATASOURCE}}" + "\n";
  iimPlay(code);
  return iimGetLastExtract(0);
}


function count_rows(datasource,csv_file_name)
{
  // COUNT THE NUMBERS OF NON-BLANK ROWS IN CSV FOR LOOP
  const CRLF = "\r\n";
  const LF = "\n";

  var lines = new Array();

  var file_i = imns.FIO.openNode(datasource+csv_file_name);
  var text = imns.FIO.readTextFile(file_i); // Read file into one string

  // Determine end-of-line marker
  var eol = (text.indexOf(CRLF) == -1) ? LF : CRLF;

  // Split into lines (number of lines) NUMBER OF LINES IN CSV
  lines = text.split(eol);
  var count=0;
  for(var i=0;i< lines.length; i++){
    if (lines[i]!="") count++;
  }
// FINISH COUNTING count= NUmber of LInes
return count;
}