1 Reply Latest reply on Jan 15, 2011 3:48 PM by Haakenlid

    CS5(js) script to read a spreadsheet and create a document

    tjprinter

      I need to write a script to read a csv file and create pages on an InDesign file.(a data merge) However after it creates the pages I need it to change the objects on the page based on the information on that line of the spreadsheet. After all the changes are made each page needs to be exported as a PDF and named a specific name from the spreadsheet. I am not even sure this can be done much less where to start.... any help would be greatly appreciated.

       

      Thanks TJ

        • 1. Re: CS5(js) script to read a spreadsheet and create a document
          Haakenlid Level 3

          It can be done.

           

          I have done some similar stuff earlier, and have some functions that might be useful. I did not script the data merge itself, but that should be possible as well.

           

          I made this function for exporting separate PDFs after a data merge. It had some logic to give files clever file names based on the contents of a certain text frame. So that can be implemented.

           

          function eksportMerged(numberOfPages){ // exports the document into several pdfs with [numberOfPages] pages each.
               var myDoc = app.activeDocument;
               var allPages = myDoc.pages.length;
               var numberOfPDFs = Math.floor(allPages/numberOfPages);
               var mappe = new Folder;
               mappe = Folder.selectDialog("Export "+myDoc.name+" to which folder?");
          
               var filestart = "File_number_";
               var filename = "";
               var myFile;
               var eksportrange = "";
          
               for (var n = 0; n< numberOfPDFs; n++){
                    filename = filestart+("00"+(n+1)).slice (-3)+".pdf";
                    myFile = new File(mappe.fullName+"/"+filename);
                    eksportrange = (n*numberOfPages)+1+"-"+((n*numberOfPages)+numberOfPages);
                    eksportPDF(myDoc,eksportrange, myFile);
               }
          
               function eksportPDF(dok,page,path){
                    var myPDFpref = app.pdfExportPresets.itemByName("[High Quality Print]");
                    app.pdfExportPreferences.pageRange=page;
                    app.pdfExportPreferences.viewPDF=false;
                    try{
                         dok.exportFile(ExportFormat.pdfType, path, false, myPDFpref,"",true);
                    } catch(e){
                         throw(e);
                    }
               }
          }
          

           

          This function will read a CSV and returns an an Array (rows) of Arrays (columns) of Strings (cells). This works with CSVs exported from Google Docs. They have commas as cell separators and newline characters as row separators. Some CSVs use semicolons instead of commas.

           

          function parseCSV(filePath){ // reads a CSV, returns an array of contents.
               var csvfil;
               var result;
               var fileContent;          
               csvfil=new File(filePath);
               csvfil.open();
               fileContent = csvfil.read();
               csvfil.close();
               eval("result=[['"+fileContent.replace(/,/g, "','").replace (/\n/g, "'],['")+"']];");
               return result;
          }