3 Replies Latest reply on Sep 16, 2016 7:20 AM by williamadowling

    Creating Shapes and Text using Excel created files

    ic13400

      Hi,

       

      I have little experience in Illustrator so excuse me asking a question that might be obvious, I would greatly appreciate a break down of the steps required to create shapes and text in Illustrator with a file created on Excel. I have researched this and tried it several times but I seem to be missing a vital step in the process.

       

      I have dimensions of the rectangles in separate columns and text in another how do I combine this to look like this. I would also be interested in changing the font style of the text on Illustrator with an entry on the CSV file. Any ideas on this?Capture.PNG

       

        • 1. Re: Creating Shapes and Text using Excel created files
          o-marat Level 3

          The simplified algorithm:

          • take a cvs file from the disk;
          • reading the lines to create a two-dimensional array:
          [
          [width_0, height_0, txt_0], // line 0
          // ...
          [width_n, height_n, txt_n] // line n
          ]
          
          • in the cycle to substitute elements of the array to a function to create objects
          • 2. Re: Creating Shapes and Text using Excel created files
            akinuri Level 1

            Can you post your excel/csv file or an excerpt?

            • 3. Re: Creating Shapes and Text using Excel created files
              williamadowling Level 4

              /*Assuming the example image you posted, it would look a little bit like this.

               

               

              //The CSV file:

               

               

              x,y,height,width,text

              50,50,100,200,text inserted here from file

              50,250,50,100,other text from file

              50,500,200,200,this one would make a square

               

               

              //etc...

               

               

               

              //The JavaScript*/

               

               

              function writeFromCSV()
              {
                  var docRef = app.activeDocument;
                 
                  //define the location of the csv file
                  // var path = "~/Path/To/CSV/file.csv";
                  var path = "~/Desktop/";
                  var theFile = new File(path + "test.csv");
              
                  //"open" the file and save contents to variable
                  theFile.open();
                  var contents = theFile.read();
                  theFile.close();
              
              
                  //save array of lines from the csv split by line breaks
                  var lines = contents.split('\n');
              
                  //loop each line and perform the shape creation
                  //initialize loop variable as 1 instead of 0 to disregard the title lines, (x,y,height,width,text)
                  for(var a=1;a<lines.length;a++)
                  {
                      var shape = lines[a];
              
                      //split the line by comma
                      //*note* you may need an extra function here to handle the possibility that
                      //the string you want to use inside the box contains a comma.
                      var elem = shape.split(",");
                      var x = elem[0]*1;
                      var y = elem[1]*1;
                      var h = elem[2]*1;
                      var w = elem[3]*1;
                      var txt = elem[4];
              
                      var thisGroup = docRef.groupItems.add();
                      thisGroup.name = "Group " + (a+1);
              
                      var rect = thisGroup.pathItems.rectangle(x,y,w,h);
                      var txtFrame = thisGroup.textFrames.add();
                      txtFrame.contents = txt;
                      // txtFrame.left = rect.left;
                      // txtFrame.top = rect.top;
                      var rectHCenter = rect.left + (rect.width/2);
                      txtFrame.left = rectHCenter - (txtFrame.width/2);
                      var rectVCenter = rect.top - (rect.height/2);
                      txtFrame.top = rectVCenter - (txtFrame.height/2) + txtFrame.height;
                  }
              }
              writeFromCSV();