4 Replies Latest reply on Nov 9, 2016 9:30 AM by Jump_Over

    write the content in particular column in .csv

    BEGINNER_X Level 3

      Hi All,

       

      Is it possible to write the content in particular column like 4 or 6 etc..?

       

      I have column count with the Heading.

       

      Before script:

      Screen Shot 2016-11-09 at 14.51.11.png

       

      After Script, (array content placing in 5 column

       

      Screen Shot 2016-11-09 at 14.51.23.png

       

      Simple code for your reference:

       

      var myArray = ["PASS", "FAIL", "PASS"]
      alert("myArray: " + myArray)
      
      
      var myJoin =myArray.join("\n");
      alert(myJoin)
      
      
      var csvFile=File(Folder.desktop + "/somefile.csv");
      
      
      csvFile.open('w');     
      csvFile.write(myJoin)
      csvFile.close();  
      

       

      Thanks in Advance

      Siraj

        • 1. Re: write the content in particular column in .csv
          Jump_Over Level 5

          Hi,

           

          Open your.csv file in Notebook to see its real structure. You need to modify this 'text' body using this structure (row after row with 'comma separated' elements.

           

          Jarek

          • 2. Re: write the content in particular column in .csv
            Loic Aigon Level 2
            var f = File.openDialog();
            f.open('r');
            var arr = [];
            while ( !f.eof ) {
              arr.push (  f.readln() );
            }
            
            
            f.close();
            
            
            arr[0] = arr[0]+"\t\t"+"result";
            arr[1] = arr[1]+"\t\t"+"FAIL";
            arr[2] = arr[2]+"\t\t"+"SUCCESS";
            arr[3] = arr[3]+"\t\t"+"FAIL";
            
            
            f.open('w');
            f.write(arr.join("\r"));
            f.close();
            
            
            
            
            • 3. Re: write the content in particular column in .csv
              BEGINNER_X Level 3

              Thank you Jump_Over for your quick response...

               

              Is it possible to placing the content by (i.e.,) Column count 5 or 10 or whatever as per job input.. (not by "," comma seperator)

               

              1. Earlier stage itself, read the heading column count like "Missing Font", "Missing Link" etc... from Input.csv

               

              See below code finding the Column count (its your code only, got from forum)

               

              //----------------------Pick Missing Font Column Count----------------------------
                  
                  MissingFont_xIdx = (cLine).search("," + String(Missing_Font_Heading));
                  
                  if(MissingFont_xIdx == -1)
                  {
                      alert("Missing Font Heading is not in Standard Format. Please check and Fix")
                      exit(0)
                      }
                  Missing_Font_column = cLine.slice(0, MissingFont_xIdx).split(",").length - 1; 
                  alert("Missing_Font_column: " + Missing_Font_column)
              //----------------------Pick Text Size Column Count----------------------------
              

               

               

              2. Grab the value in myMissingFont_Array (refer top of the thread or Previous link created on yesterday)

               

              3. Finally write it in Input.csv Column (as per Heading Column) (this is my request now)

               

              Regards

              Siraj

              • 4. Re: write the content in particular column in .csv
                Jump_Over Level 5

                Hi,

                 

                1. if you read a .csv file and split text by "\n" ==> you have an array (each CSV line is an element of this array)

                2. Assuming you have another array with data to input ==> alike ['FAIL','PASS','FAIL']

                3. Assuming you want to input this as a 5th column

                 

                so you need to loop - split - splice - join

                something like:

                --------------------------------------------------

                var

                  mTotalArray, // csvFile.read().split("\n")

                  mLineArray, // current  mTotalArray element.split(",")

                  mInputArray, // your data

                  k,

                  mTargetColumn = 5; // number of target column

                 

                for (k = 0; k < mTotalArray.length; k++) {

                  mLineArray = mTotalArray[k].split(",");

                  mLineArray.splice(mTargetColumn - 1, 1, mInputArray[k]);

                  mTotalArray[k] = mLineArray.join();

                  }

                 

                 

                //After this you can write entire string - mTotalArray.join("\n") - into your CSV file

                ---------------------------------------------------------

                 

                Jarek