javascript or java function for csv file

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.

Moderators: Community Moderators, iMacros Moderators

Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the Google search box (at the top of each forum page) to see if a similar problem or question has already been addressed. This will search the entire contents of the forums as well as the iMacros Wiki.
3. We can respond much faster to your posts if you include the following information:

CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST

Answering your own posts (e.g. attempting to "bump" your topic) drops your topic from the list of unanswered threads, so it may actually receive less views.

javascript or java function for csv file

by vt2011 on Fri Sep 09, 2011 11:57 pm

Hi,

I am looking for a function in firefox javascript or java using liveconnect to push all the values of a csv file into an array of array and count the total number of the rows of the csv file.
CSV file have their specific rules, if someone has written these functions, I would be very interested.

Thanks
vt2011
 
Posts: 35
Joined: Sat Jul 23, 2011 11:21 am

Re: javascript or java function for csv file

by Tom, Tech Support on Mon Sep 12, 2011 4:01 am

Hi vt2011,

Here's some sample code that we use inside of our iMacros Javascripts for this purpose:

Code: Select all
// End-of-line markers
const CRLF = "\r\n";
const LF = "\n";

var lines = new Array();               

var file_i = imns.FIO.openNode("PATH_TO_INPUT_FILE");
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
lines = text.split(eol);

var i, cells

// Iterate over each line and split into columns
for (i = 0; i < lines.length; i++)
{
   cells = splitLine(lines[i]);
}

// Split a line into columns separated by commas, where each column might have embracing quotation marks (also with commas inside a column)
function splitLine(line)
{
   var columns = new Array();
   var i,j,state, c;
   /*state
   0 = not quotation mark; set by final comma
   1 = quotation mark started
   2 = quotation mark end but not yet final comma
   3= no quotation mark expected (first character was no quotation mark)
     */
   j=0; state=0;
   columns[0]="";
   for(i=0; i<=line.length; i++)
   {
      c=line.charAt(i);
      switch(state)
      {
         case 0:
         switch(c)
         {
            case ',':
            columns[++j]="";   // empty column
            break;
            case '"':
            state = 1;
            break;
            case ' ':         // ignore leading spaces
            break;
            default:
            columns[j]+=c;
            state = 3;
         }
         break;
         case 1:
         switch(c)
         {
            case '"':
            state = 2;
            break;
            default:
            columns[j]+=c;
         }
         break;
         case 2:
         switch(c)
         {
            case ',':
            columns[++j]="";
            state = 0;
            break;
            case ' ':         // ignore trailing spaces
            break;
            default:
            //error
         }
         break;
         case 3:
         switch(c)
         {
            case ',':
            columns[++j]="";
            state = 0;
            break;
            default:
            columns[j]+=c;
         }
         break;
      }
   }
   return (columns);
}
Regards,

Tom, iMacros Support
Tom, Tech Support
 
Posts: 3322
Joined: Mon May 31, 2010 9:59 am

Re: javascript or java function for csv file

by vt2011 on Mon Sep 12, 2011 8:35 pm

hi,

Thanks for your help. Unfortunately I have csv files which have line breaks inside a field value embraced in double-quotes, so the function split lines in the post does not work anymore.

Rgds
vt2011
 
Posts: 35
Joined: Sat Jul 23, 2011 11:21 am

Re: javascript or java function for csv file

by Tom, Tech Support on Tue Sep 13, 2011 1:09 am

Well the beauty of having this code is that you can modify it to suite your needs!
Regards,

Tom, iMacros Support
Tom, Tech Support
 
Posts: 3322
Joined: Mon May 31, 2010 9:59 am

Re: javascript or java function for csv file

by vt2011 on Tue Sep 13, 2011 8:32 pm

you're right, the code is well done and can be used for specific needs.

Thanks
vt2011
 
Posts: 35
Joined: Sat Jul 23, 2011 11:21 am

Re: javascript or java function for csv file

by crself on Fri Jan 04, 2013 7:29 am

I have another splitline function.quotation mark inside a column

Code: Select all
function splitLine(line)
{
   var columns = new Array();
   var comm=",";
   var s1="";
   var s=read(filename);//read from file line by line
     for(var i=0;i<s.length-1;i++)
   {
      var arr=s[i].match(/\"/g);
      
      while(s[i].match(/\"/g)&&s[i].match(/\"/g).length%2==1)
         {
         s[i]+=nl+s[i+1];
         s.splice(i+1,1);
         }
   }
   
   
   for(var i=0;i<s.length;i++){
         columns=s[i].split(comm);
         
           for(var j=0;j<columns.length-1;j++)
         {
            while(columns[j].match(/\"/g)&&columns[j].match(/\"/g).length%2==1)
            {
            columns[j]+=comm+columns[j+1];
            columns.splice(j+1,1);
            }
         
         }   
         for(var j=0;j<columns.length;j++)
         {
            if(columns[j].indexOf("\"")
            columns[j]=columns[j].replace(/(^\")|(\"$)/g, "")
            if(columns[j].indexOf("\"\"")
            columns[j]=columns[j].replace(/\"\"/g,"\"");
         }
   }
   return columns;
}
crself
 
Posts: 3
Joined: Mon Oct 25, 2010 1:01 am


Return to iMacros for Firefox

Who is online

Users browsing this forum: No registered users and 10 guests

-->