3 Replies Latest reply on Feb 4, 2014 2:23 AM by try67

    Javascript cannot read my csv file

    yvesha Level 1

      Hi there,

       

      I have a 48 page document which I need to split into 8 documents. I have the excel spreadsheet with column headed "filename" with 8 file names.

       

      When I select the data file to import after being asked to enter text at start of filename etc I see the message 'No filenames found - using "file-XX.pdf". Press Escape after continuing to cancel.'

       

      and then the error -

       

      RaiseError: The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.

      Doc.extractPages:83:Console undefined:Exec

      ===> The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.

      file-0

       

      I have checked the number of rows in the csv with the one which i created the merge in Indesign and it is the same. I have also tried usinh google docs spreadsheet but acrobat doesnt recognise the URL.

       

      Thank you.

       

      The script which I am using is:

       

      var CSV = function (data, delimiter) {

          var _data = CSVToArray(data, delimiter);

          var _head = _data.shift();

          return {

              length: function () {return _data.length;},

              adjustedLength: function () {return _data.length - 1;},

              getRow: function (row) {return _data[row];},

              getRowAndColumn: function (row, col) {

                  if (typeof col !== "string") {

                      return _data[row][col];

                  } else {

                      col = col.toLowerCase();

                      for (var i in _head) {

                          if (_head[i].toLowerCase() === col) {

                              return _data[row][i];

                          }

                      }

                  }

              }

          };

      };

       

      function CSVToArray( strData, strDelimiter ){

          strDelimiter = (strDelimiter || ",");

          var objPattern = new RegExp(

              (

                  // Delimiters.

                  "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

                  // Quoted fields.

                  "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

                  // Standard fields.

                  "([^\"\\" + strDelimiter + "\\r\\n]*))"

              ),

              "gi"

              );

       

          var arrData = [[]];

          var arrMatches = null;

          while (arrMatches = objPattern.exec( strData )){

              var strMatchedDelimiter = arrMatches[ 1 ];

              if (

                  strMatchedDelimiter.length &&

                  (strMatchedDelimiter != strDelimiter)

                  ){

                  arrData.push( [] );

              }

              if (arrMatches[ 2 ]){

                  var strMatchedValue = arrMatches[ 2 ].replace(

                      new RegExp( "\"\"", "g" ),

                      "\""

                      );

              } else {

                  var strMatchedValue = arrMatches[ 3 ];

              }

              arrData[ arrData.length - 1 ].push( strMatchedValue );

          }

          return( arrData );

      }

       

      function isInt(n) {

          return typeof n === "number" && n % 1 == 0;

      }

       

      var prepend = app.response("Enter any text to go at the START of each filename:");

      var append = app.response("Enter any text to go at the END of each filename:");

      var pathStr = app.response("If the PDFs should be saved in a sub folder, enter the relative path here:", "", "pdf/");

       

      this.importDataObject("CSV Data");

      var dataObject = this.getDataObjectContents("CSV Data");

      var csvData = new CSV(util.stringFromStream(dataObject, 'utf-8'), ',');

      var pagesPerRecord = this.numPages / csvData.length();

      if (isInt(pagesPerRecord)) {

          for (var i = 0; i < this.numPages; i ++) {

              var pageStart = i*pagesPerRecord;

              var pageEnd = (i+1)*pagesPerRecord - 1;

              var recordIndex = (i + pagesPerRecord) / pagesPerRecord;

              var filename = csvData.getRowAndColumn(i, "filename");

              if (!filename) {

                  app.alert('No filenames found - using "file-XX.pdf". Press Escape after continuing to cancel.');

                  filename = "file-" + i;

              }

              var settings = {nStart: pageStart, nEnd: pageEnd, cPath: pathStr+prepend+filename+append+'.pdf'};

              this.extractPages(settings);

          }

      } else {

          var message = "The number of pages per row is not an integer (" + pagesPerRecord;

          message += ", " + this.numPages + " pages, " + csvData.length() + " rows).";

      }