Forum Discussion

SMR's avatar
SMR
New Contributor
6 years ago

How to write data to csv file using Java script?

Hello All,

 

I need help in writing values to csv file.My code is as below:

 

Driver = DDT.CSVDriver("C:/test.csv"); 

 while(!Driver.EOF())

{

 

rowValue = Driver.Value("1stcolumn");

columnValue = Driver.Value("2ndcolumn");

// here I want to write some data to 3rd column based on the row., can anyone suggest me how to open csv file and //write in java script?

 

Driver.Next();

}

Thank you.

3 Replies

  • karkadil's avatar
    karkadil
    Valued Contributor

    CSV driver doesn't support data writing (as well as other DDT drivers), you need to open the file as usual text file and write raw data into it.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      To build on what karkadil has indicated, there are many ways of doing precisely what he suggests.  One such way is to use the aqFile scripting object.  The way I'd do it is

       

      1) Open the file to be read using aqFile.OpenTextFile

      2) Open another new file as writable for your output

      3) Use ReadLn to read the columns and immediately write it out to the new file

      4) Start a "while not end of file" loop

      5) Use ReadLn to read a line of data

      6) Modify what is read in to include your value you want for the third column

      7) Use WriteLn to output to the new file

      8) After the loop is finished, close both files.

  • cunderw's avatar
    cunderw
    Community Hero

    I believe you should be able to connect to a csv file with ADO like you can with and excel file, then you could loop through rows and perform update statments like you can with a normal SQL database. I haven't had a chance to test this theory with a CSV but I know it works fine with excel.