How to export JavaScript array to csv (on client side)?

Discussions and Tech Support related to automating the iMacros Browser or Internet Explorer from any scripting and programming language, such as VBS (WSH), VBA, VB, Perl, Delphi, C# or C++.

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:


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.

How to export JavaScript array to csv (on client side)?

by chivracp on Sat May 23, 2015 3:00 am

CONFIG: firefox 38.0.1,imacros for firefox 8.9.2, windows xp sp 3

Code: Select all
function readTextFile(file) {
    var csvAsArray;
    const XMLHttpRequest = Components.Constructor(";1");
    var rawFile = new XMLHttpRequest();"GET", file, false);
    rawFile.onreadystatechange = function ()
        if(rawFile.readyState === 4)
            if(rawFile.status === 200 || rawFile.status == 0)
                var allText = rawFile.responseText;
                csvAsArray = CSVToArray(allText,",");
    return csvAsArray;}

function CSVToArray(strData, strDelimiter){
        // Check to see if the delimiter is defined. If not,
        // then default to comma.
        strDelimiter = (strDelimiter || ",");

        // Create a regular expression to parse the CSV values.
        var objPattern = new RegExp(
                // Delimiters.
                "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

                // Quoted fields.
                "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

                // Standard fields.
                "([^\"\\" + strDelimiter + "\\r\\n]*))"

        // Create an array to hold our data. Give the array
        // a default empty first row.
        var arrData = [[]];

        // Create an array to hold our individual pattern
        // matching groups.
        var arrMatches = null;

        // Keep looping over the regular expression matches
        // until we can no longer find a match.
        while (arrMatches = objPattern.exec( strData )){

            // Get the delimiter that was found.
            var strMatchedDelimiter = arrMatches[ 1 ];

            // Check to see if the given delimiter has a length
            // (is not the start of string) and if it matches
            // field delimiter. If id does not, then we know
            // that this delimiter is a row delimiter.
            if (
                strMatchedDelimiter.length &&
                strMatchedDelimiter !== strDelimiter

                // Since we have reached a new row of data,
                // add an empty row to our data array.
                arrData.push( [] );


            var strMatchedValue;

            // Now that we have our delimiter out of the way,
            // let's check to see which kind of value we
            // captured (quoted or unquoted).
            if (arrMatches[ 2 ]){

                // We found a quoted value. When we capture
                // this value, unescape any double quotes.
                strMatchedValue = arrMatches[ 2 ].replace(
                    new RegExp( "\"\"", "g" ),

            } else {

                // We found a non-quoted value.
                strMatchedValue = arrMatches[ 3 ];


            // Now that we have our value string, let's add
            // it to the data array.
            arrData[ arrData.length - 1 ].push( strMatchedValue );

        // Return the parsed data.
        return( arrData );}

var firstline;
var accounts = new Array();

accounts = readTextFile("file:///C:/ICT.csv");
firstline = accounts[0].toString();

before run code in file ICT.csv:

Code: Select all

after run code output file ICT.csv (this i want):

Code: Select all
Posts: 1
Joined: Wed May 20, 2015 11:21 pm

Return to Scripting and Command Line Interface

Who is online

Users browsing this forum: No registered users and 2 guests