首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将csv数据放入数组imacros js

将csv数据放入数组imacros js
EN

Stack Overflow用户
提问于 2015-04-12 14:58:51
回答 2查看 797关注 0票数 1

我试图循环通过csv文件将每一列推入一个数组,但我不确定如何做到这一点,我知道标签{{!COL1}}会给我我想要的数据,但我不知道如何将其保存到一个变量中,我可以用来推入一个数组中。

代码语言:javascript
复制
csvToArray = "CODE:";
csvToArray += "SET !DATASOURCE artist.csv" + "\n";
csvToArray += "SET !ERRORIGNORE YES" + "\n";
csvToArray += "SET !DATASOURCE_LINE {{CSV}}" + "\n";
csvToArray += "SET !VAR1 {{!COL1}}" + "\n";

for(i = 0; i < 10; i++){
iimSet("CSV", i);
iimPlay(csvToArray);
}

这段代码将允许我遍历csv文件,{{!COL1}}标签给了我想要的数据,我如何将它保存到一个我可以使用的变量中,请帮助解决这个问题。:(

EN

回答 2

Stack Overflow用户

发布于 2016-04-21 08:25:06

要获取COL1的数据,您需要设置EXTRACT = COL1,然后将变量赋给iimGetLastExtract()。

将以下内容添加到宏中:

代码语言:javascript
复制
csvToArray += "SET !EXTRACT {{!COL1}}";

然后将此代码添加到js文件中:

代码语言:javascript
复制
var myArray = []; // place at top of file for readability
myArray.push(iimGetLastExtract()); // place at end of for loop
                                   // puts each extraction into myArray
票数 0
EN

Stack Overflow用户

发布于 2016-04-21 18:59:47

尝试下面的代码块。

代码语言:javascript
复制
function read_file(path) {
var content = '', i = 1, f, res = '',spl;

 while (1){
    content = res;      
    if (content != "") {
    spl = content.split("|"); 
    //spl[0] will contain the Row1 and Column1 value, spl[1] will contain Row 1 and Column 2 value etc.,
    /*Here you can specify your script/conditions
    Example:
    iimPlay('CODE:TAG POS=1 TYPE=INPUT:TEXT ATTR=NAME:login[username] CONTENT='+spl[0]);
    iimPlay('CODE:TAG POS=1 TYPE=INPUT:PASSWORD ATTR=NAME:login[password] CONTENT='+spl[1]);

I used the infinite loop, so in order to come out of the loop, you need to add a condition like below

if (spl[0]=="End") {//In your input sheet if the Row1,Col1 contains the value 'End', then it will exit the loop
        iimDisplay("All Rows Completed");
        return;
    }   

    */ 
    f = "CODE: "+"\n";
    f += "SET !EXTRACT null" + "\n"; 
    f += "SET !LOOP 3" + "\n"; 
    f += "SET !DATASOURCE \""+path+"\" "+"\n";
    f += "SET !DATASOURCE_COLUMNS 5" + "\n"; //Assume that you have five columns in the your input file
    f += "SET !DATASOURCE_LINE " + i + "\n"; 
    f += "SET !EXTRACT {{!col1}}|{{!col2}}|{{!col3}}|{{!col4}}|{{!col5}}" + "\n";
    iimPlay(f);        
    res = iimGetLastExtract();
    i++;
}
return content;
}
var file_conten = read_file("yourinputfilename.csv");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29586878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档