5 Replies Latest reply on Jul 3, 2014 8:52 AM by malhar1991

    Trouble in creating text objects

    malhar1991

      OPTION 1:

      var doc = app.activeDocument,

         

          i, j, k, l, a, _rows, _cells, content="", myText, myStory;

      content += doc.stories.everyItem().tables.everyItem().cells.everyItem().contents;

      alert(content);

      myStory = doc.stories.item(0);

      myText = myStory.item(0).texts.itemByRange(0, content.length);

      var myTable = myText.convertToTable();

       

      OPTION 2:

      var doc = app.activeDocument,

         

          i, j, k, l, a, _rows, _cells, content="", myText, myStory;

      content += doc.stories.everyItem().tables.everyItem().cells.everyItem().contents;

      alert(content);

      myStory = doc.stories.item(0);

      myText = doc.stories.item(0).texts.itemByRange(0, content.length);

      var myTable = myText.convertToTable();

       

      OPTION 3:

      var doc = app.activeDocument,

         

          i, j, k, l, a, _rows, _cells, content="", myText, myStory,

          myFrame=doc.pages.item(0).textFrmaes.add();

         myFrame.geometricBounds=["6p", "6p", "12p", "12p"];

      content += doc.stories.everyItem().tables.everyItem().cells.everyItem().contents;

      alert(content);

      myFrmae.contents=content;

      myText:Text = doc.stories.item(0).texts.itemByRange(0, content.length);

      var myTable:Table = myText.convertToTable();

       

      I have tried these 3 options written above. Here I am experimenting on how to create a table from text. I have a document which already has a table so I am copying all data from that table into a variable content. But still I am stuck. Help me out.

        • 1. Re: Trouble in creating text objects
          Chinnadk Level 4

          Try this.

           

          var doc = app.activeDocument,
          i, j, k, l, a, _rows, _cells, content="", myText, myStory,
          myFrame=doc.pages.item(0).textFrames.add();
          myFrame.geometricBounds=["6p", "6p", "12p", "12p"];
          content += doc.stories.everyItem().tables.everyItem().cells.everyItem().contents;
          myFrame.contents=content;
          var myText = myFrame.parentStory.characters.itemByRange(-1, -content.length)
          var myTable = myText.convertToTable();
          

           

           

          Regards,

          Chinna

          • 2. Re: Trouble in creating text objects
            Laubender Adobe Community Professional & MVP

            @malhar1991 – if you need all contents from a table, you could use the contents property of that table. It will return an array of the contents (unformatted) of all cells of that table.

             

            Try the following and have fun. Select all cells of a table (that is: select the table) and run this script:

             

            //A table object has a contents property:
            //Will return an array of all cells contents
            
            //Select a table:
            var mySourceTable = app.selection[0];
            var myContentsArray = mySourceTable.contents;
            
            //Adding a text frame and a table (in this case a table with 1 cell):
            var newTable = app.documents[0].textFrames.add
                (
                    {geometricBounds:[0,0,"100 mm","200 mm"]}
                ).
               
                insertionPoints[0].
                tables.add
                (
                    {
                        bodyRowCount:1,
                        columnCount:1,
                        contents:myContentsArray
                    }
                );
            

             

            Change the numbers of rows and columns at your will. If the number of cells of your source exceeds the number of cells in your target, you'll get some empty cells. If not, no new cells will be added to the new table. Cells are filled with contents, if possible.

             

            Uwe

            • 3. Re: Trouble in creating text objects
              Laubender Adobe Community Professional & MVP

              Note 1: this is also working with some cells selected. The returned array of the contents property of the selected cells will be used as the contents of the new table.


              Note 2: assigning contents transfers unformatted contents, that also means, inline and anchored objects will not travel over etc.pp.

               

              Uwe

              • 4. Re: Trouble in creating text objects
                malhar1991 Level 1

                Hey,

                 

                Both the solutions are working for me but one thing I don't understand is that why my solutions are not working when I am using them as they are written in Adobe Indesign CS6 Javascript Scripting Guide.

                • 5. Re: Trouble in creating text objects
                  malhar1991 Level 1

                  Even your solution is working. If we add column and row seperator to convertToTable() method, it is working perfectly for me otherwise it is putting all the content of mySourceTable into one cell of the table. Thank you for your guidence