4 Replies Latest reply on Mar 3, 2017 6:45 AM by Silly-V

    CSV parsing routine works fast in AI, slow in ID

    Silly-V Adobe Community Professional

      I have a modified version of a csv parsing code from here: excel.js/csv.js at master · thetalecrafter/excel.js · GitHub      Javascript code to parse CSV data - Stack Overflow

       

      I've always used it with Illustrator scripting and it worked instantly. However, using it inside ID takes an outrageous amount of time to do the same thing!

      Try the following snippet using #target illustrator and then using #target indesign.

       

      #target illustrator

      //#target indesign

      function test(){

        var grid = {

       

       

         parse: function(csv, reviver, delimiter) {

           delimiter = delimiter  || ',';

           reviver = reviver || function(r, c, v) { return v; };

           var chars = csv.split(''), c = 0, cc = chars.length, start, end, table = [], row;

           while (c < cc) {

             table.push(row = []);

             while (c < cc && '\r' !== chars[c] && '\n' !== chars[c]) {

               start = end = c;

               if ('"' === chars[c]){

                 start = end = ++c;

                 while (c < cc) {

                   if ('"' === chars[c]) {

                     if ('"' !== chars[c+1]) { break; }

                     else { chars[++c] = ''; } /* unescape "" */

                   }

                   end = ++c;

                 }

                 if ('"' === chars[c]) { ++c; }

                 while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && delimiter !== chars[c]) { ++c; }

               } else {

                 while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && delimiter !== chars[c]) { end = ++c; }

               }

               row.push(reviver(table.length-1, row.length, chars.slice(start, end).join('')));

               if (delimiter === chars[c]) { ++c; }

             }

             if ('\r' === chars[c]) { ++c; }

             if ('\n' === chars[c]) { ++c; }

           }

           return table;

         },

         stringify: function(table, replacer, delimiter) {

           replacer = replacer || function(r, c, v) { return v; };

           var csv = '', c, cc, r, rr = table.length, cell;

           for (r = 0; r < rr; ++r) {

             if (r) { csv += '\r\n'; }

             for (c = 0, cc = table[r].length; c < cc; ++c) {

               if (c) { csv += delimiter; }

               cell = replacer(r, c, table[r][c]);

               var rx = new RegExp("["+delimiter+"\\r"+"\\n\"]");

               if (rx.test(cell)) { cell = '"' + cell.replace(/"/g, '""') + '"'; }

               csv += (cell || 0 === cell) ? cell : '';

             }

           }

           return csv;

         }

        };

       

       

        var f = File.openDialog();

        if(f){

          f.open('r');

          var fileStr = f.read();

          f.close();

        var parsedFile = grid.parse(fileStr);

         var len = parsedFile.length;

      alert("The CSV file has " + len + " records.");

        }

      };

      test();

        • 1. Re: CSV parsing routine works fast in AI, slow in ID
          Loic.Aigon Adobe Community Professional

          Hi Vassily,

           

          Never noticed CSV processing was specifically long but I don't use that library of yours. My approach is rather:

           

          var main = function() {
            var csv = getfCSVFile(),
                  data;
              
              if !csv ) return;
              
              data = getCSVData ( csv );
              
              if ( !data.rows || !data.rows.length ) {
                  alert("No data found sorry !");
                  return;
              }
              
              alert( data.rows.length+ " rowws found and headers are\r"+data.headers.join("\r"));
          }
          
          
          var u;
          
          
          var getfCSVFile = function ( ) {
              var 
              os = $.os[0],
              mac = os=="M",
              wF = "CSV files : *.csv;",
              mF = function() {
                  return (f instanceof Folder) || /\.csv/i.test(f.name) );
              },
              f = File.openDialog("Please pick a CSV file…", mac? mF : wF );
              return f;
          }
          
          
          var getCSVData = function ( csvFile ) {
              var data = {rows:[]]}, sep = ",";
              
              csvFile.open('r');
              data.headers = csvFile.readln ().split(sep);
              while ( !csvFile.eof ) {
                  data.rows.push (csvFile.readln ().split(sep); )
              }
              
              return data;
          
          
          }
          
          
          app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
          
          

           

          But it will fails differenciating carriage returns from line feeds and it needs you to know the separator. Let me know if it's "speeder" than the  lib above.

          • 2. Re: CSV parsing routine works fast in AI, slow in ID
            Silly-V Adobe Community Professional

            Yea, a simple parse like that works instantly in both AI and ID, but in my case they need to have commas inside their cells.

            If you try my snippet, you'll see a strange and enormous time discrepancy between executing in AI and ID.

            • 3. Re: CSV parsing routine works fast in AI, slow in ID
              Vamitul Level 4

              The JS engine in Indesign is the oldest one of the Adobe products AFIK, and it does have quite some inefficiencies when using regular expressions in a large string.

              Some performance gains can be had in your code by moving the regex compilation outside of the loops in the stringify method, and using chunks in the parse function instead of going character by character.

               

              Moving forward, I used with great results a modified version of GitHub - cparker15/CSV-js: A CSV (comma-separated values) parser written in JavaScript. And I heard great things about GitHub - knrz/CSV.js: A simple, blazing-fast CSV parser and encoder. Full RFC 4180 compliance.

              1 person found this helpful
              • 4. Re: CSV parsing routine works fast in AI, slow in ID
                Silly-V Adobe Community Professional

                Interesting. I looked at the first link and saw there's a TODO regarding split lines inside of quotes, and the 2nd link has some code which has to do with higher versions of ES using the 'let' keyword.

                Using the 1st method, were you able to get good results when there are line breaks inside of a cell?