20 Replies Latest reply on Oct 21, 2016 1:43 AM by Laubender

    Table paragraph split and move

    tpk1982 Level 4

      Hi All,

       

      I hope it can doable in script. But am struck from a point.

       

      Requirement:

       

      If a table cell contains more than one line then we need to insert the rows and move the content

       

      Logic used:

       

      1) Count the line in each cell

       

      2) If it contains more than one line then add the rows before

       

      3) move the contents

       

      Screen Shot 2016-10-13 at 2.52.04 PM.png

       

      Tried coding (Code got from forum, thanks to Chinna):

       

      var doc = app.activeDocument,           
          _tables = doc.stories.everyItem().textStyleRanges.everyItem().getElements(),     
          i, j, k, l, _columns, _cells, tempVar = "", page;  
           
      for (i = 0; i < _tables.length; i++)  
      {  
          for (j = 0; j < _tables[i].tables.length; j++)  
          {  
              tempVar += "\r";  
              _columns = _tables[i].tables[j].columns;  
              for (k = 0; k < _columns.length; k++)  
              {  
                  _cells = _columns[k].cells;  
                  tempVar += "\r";  
                  for (l = 0; l < _cells.length; l++)  
                  {  
                      if(_cells[l].paragraphs.length>1){
                          alert(_cells[1].contents)
      //~                     tempVar += _cells[l].paragraphs + "\t";          //Copy contents from table cell to a temporary variable  
                          _cells[l].rows.add( LocationOptions.BEFORE, _cells[l].rows[0] );
                          
                      }
                  }  
              }  
          }  
      }     
      

       

      Thanks,

      K

        • 1. Re: Table paragraph split and move
          tpk1982 Level 4

          Any suggestions please?

          • 2. Re: Table paragraph split and move
            Obi-wan Kenobi Adobe Community Professional

            Cool and interesting topic!  I'll take a look to a script this WE! 

             

            (^/)

            • 3. Re: Table paragraph split and move
              tpk1982 Level 4

              Thanks Obi

              • 4. Re: Table paragraph split and move
                Skemicle Level 3

                I may have over complicated this a bit.. but here is my script:

                /*
                scripted by Skemicle
                11:42 AM 10/14/2016
                */
                if(parseFloat(app.version) < 6){
                    main();
                }else{
                    app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Seperate Table Rows");
                }
                function main(){
                    var doc = app.activeDocument,
                    text = doc.textFrames;
                    for(t=0;t<text.length;t++){
                        table = text[t].tables;
                        for(T=0;T<table.length;T++){
                            row = table[T].rows;
                            for(r=0;r<row.length;r++){
                                cell = row[r].cells;
                                if(cell[0].lines.length > 1){
                                    table[T].rows.add(LocationOptions.AFTER, row[r])
                                    for(c=0;c<table[T].columns.length;c++){
                                        row[r+1].cells[c].contents = cell[c].lines[-1].contents;
                                        cell[c].lines[-1].contents = "";
                                        cell[c].characters[-1].contents = "";
                                    }r--;
                                }
                            }
                        }
                   
                    }
                }
                

                This could be a good starting point for modifications Obi-wan.

                1 person found this helpful
                • 5. Re: Table paragraph split and move
                  tpk1982 Level 4

                  Hi Skemicle

                   

                  Exactly this is i want.. thank you so much

                   

                  Thanks Obi for your interest on this thread

                   

                  Regards,

                  K

                  • 6. Re: Table paragraph split and move
                    Obi-wan Kenobi Adobe Community Professional

                    Hi Skemicle,

                     

                    You're right! It's a excellent starting point for beginning! … and modifications! 

                     

                    It only works correctly if row cells have a same paras number!

                     

                    First, it would be cool for the user to be able to select what tables he wants to be played! with something like this code.

                    A new real need to be included: treat a cells selection (contiguous or not, in the same table or not, or text selection (maybe manually marked with a condition).

                    Eventually, the possibility to treat tables defined by a table style or marked with a condition.

                     

                     

                     

                     

                    function main()      
                    {
                    
                    
                    (function () {  
                      
                        function getTables () {  
                            if (app.selection.length == 0) {  
                                return app.documents[0].stories.everyItem().tables.everyItem().getElements();  
                            } else if (app.selection[0].parent instanceof Cell) {  
                                return [app.selection[0].parent.parent];  
                            } else if (app.selection[0].hasOwnProperty ('parentStory')) {  
                                return app.selection[0].parentStory.tables.everyItem().getElements();  
                            }  
                        }  
                      
                        function treatTables (tables) {  
                            …………
                        }  
                       
                        if (app.documents.length > 0) {  
                            treatTables (getTables());  
                        }  
                      
                    }());
                    
                    
                    }
                    

                     

                     

                    Then, the next step would be for the user to be able to treat a table like this:

                    Capture d’écran 2016-10-15 à 10.06.51.png

                    (^/)

                     

                    [To be continued! …]

                    • 7. Re: Table paragraph split and move
                      Laubender Adobe Community Professional & MVP

                      Hi Skemicle,

                      just tested your script on table with an empty column.


                      It's throwing an error "Object is not valid" in line 24.

                       

                      The least thing you can do is something like that:

                      try{cell[c].characters[-1].contents = ""; }catch(e){$.writeln(table[T].id+"\t"+cell[c].name+"\t"+e.message)};
                      

                       

                      And there are other things to consider:

                      1. Adding rows in the first column will affect text in the second column.

                      BeforeAfter-Running-Snippet-SeparateLinesToCells.png

                      2. And I would not assign the contents, but move() the text so that its formatting can survive.

                      As you can see in the sample table above formatting will be a problem.

                       

                      What I would do is:

                       

                      1. Analyzing ( and not changing ! ) the table to get the number of rows needed.

                      2. Adding a new table with the needed columns and rows.

                      3. Move the formatted text line by line to the added table using move().

                      4. Add sufficent rows to the original table.

                      5. Move back the formatted text to the original table.

                      6. Remove the added table.

                       

                      What could make this process complicated?
                      Merged cells.

                       

                      Regards,
                      Uwe

                      1 person found this helpful
                      • 8. Re: Table paragraph split and move
                        Obi-wan Kenobi Adobe Community Professional

                        Hi Uwe,

                         

                        When I've tested Skemicle's script, I saw this problem!

                        I've fixed it with a simple trick: counting the paras number per cell on each row and temporary make them equal!! As inserting an "@":

                        Capture d’écran 2016-10-15 à 11.05.25.png

                        Just finally play: find ^@ and replace by nothing!

                         

                        (^/)

                        • 9. Re: Table paragraph split and move
                          tpk1982 Level 4

                          Hi Skemicle

                           

                          I just noticed what Uwe said. Yes it is a big problem.

                           

                          Is it possible to help with that?  I tried and failed. Maybe we can use paragraphs ("/r") instead of lines?

                           

                          Screen Shot 2016-10-18 at 12.25.21 PM.png

                           

                          Regards,

                          K

                          • 10. Re: Table paragraph split and move
                            tpk1982 Level 4

                            I changed the line 19 to

                             

                            if(cell[0].paragraphs.length > 1){

                             

                            and it works now as expected for cell contain one paragraph..

                             

                            But if a cell contains two paragraph and 3 lines, it split as 3

                             

                            Screen Shot 2016-10-18 at 2.54.32 PM.png

                            • 11. Re: Table paragraph split and move
                              Laubender Adobe Community Professional & MVP

                              Maybe I did not understand your requirements.
                              I thought that every single line should get its own cell.

                               

                              And you still have the formatting issue.

                              Set some text in line 2 or above to e.g. to "Bold" and run the script again.
                              You'll see, that the format cannot travel over to the new cell. It will get the basic format of the cell, that the first insertion point will dictate.

                               

                              Moving the text with the move() method will do that appropriately.
                              Assigning the contents is the wrong way.

                               

                              Regards,
                              Uwe

                              • 12. Re: Table paragraph split and move
                                tpk1982 Level 4

                                Hi Uwe,

                                 

                                Sorry if i was not explained my requirement clearly.

                                 

                                I need to split the cell contents paragraph by paragraph NOT line by line. Also i was not worried about formatting while you said. But now i am worried it may become challenge in future.

                                 

                                I will try move() method, and let you know if i need any help.

                                 

                                Thanks for your help again.

                                 

                                Regards,

                                K

                                • 13. Re: Table paragraph split and move
                                  Laubender Adobe Community Professional & MVP

                                  Hm. I cannot see a difference between paragraph by paragraph and line by line yet.

                                  Maybe you can post an example where this is apparent.

                                   

                                  Regards,
                                  Uwe

                                  • 14. Re: Table paragraph split and move
                                    tpk1982 Level 4

                                    Hi Uwe,

                                     

                                    Please the below screengrab for better explanation:

                                     

                                    Screen Shot 2016-10-18 at 4.20.00 PM.png

                                    • 15. Re: Table paragraph split and move
                                      Skemicle Level 3

                                      If you're changing line 19 to check: if(cell[0].paragraphs.length > 1){, You must also change lines 22 and 23 to reflect paragraphs rather than lines.

                                      When I get some time I will look into Uwe's move idea but that may take a while.

                                      • 16. Re: Table paragraph split and move
                                        tpk1982 Level 4

                                        Thank you Skemicle

                                         

                                        The below is working but as Uwe said the formatting lost..

                                         

                                        I am also trying to use move() the best results.. looking forward from your end also

                                         

                                        if(parseFloat(app.version) < 6){  
                                            main();  
                                        }else{  
                                            app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Seperate Table Rows");  
                                        }  
                                        function main(){  
                                            var doc = app.activeDocument,  
                                            text = doc.textFrames;  
                                            for(t=0;t<text.length;t++){  
                                                table = text[t].tables;  
                                                for(T=0;T<table.length;T++){  
                                                    row = table[T].rows;  
                                                    for(r=0;r<row.length;r++){  
                                                        cell = row[r].cells;  
                                                        if(cell[0].paragraphs.length > 1){  
                                                            table[T].rows.add(LocationOptions.AFTER, row[r])  
                                                            for(c=0;c<table[T].columns.length;c++){  
                                                                row[r+1].cells[c].contents = cell[c].paragraphs[-1].contents;  
                                                                cell[c].paragraphs[-1].contents = "";  
                                                                cell[c].characters[-1].contents = "";  
                                                            }r--;  
                                                        }  
                                                    }  
                                                }  
                                             
                                            }  
                                        }  
                                        
                                        • 17. Re: Table paragraph split and move
                                          tpk1982 Level 4

                                          Hi Skemicle

                                           

                                          If you did move() method, please share here.. so it make good learning for me..

                                           

                                          Regards,

                                          K

                                          • 18. Re: Table paragraph split and move
                                            Skemicle Level 3

                                            I haven't attempted just yet. Would you care to share what you've come up with in your attempts?

                                            • 19. Re: Table paragraph split and move
                                              tpk1982 Level 4

                                              Hi Skemicle

                                               

                                              Sorry i tried and get some help from forum if anything posted before with move method. I hardly dont found anything. I was not did more script in tables. So little struggled and asking help from you.

                                               

                                              if(parseFloat(app.version) < 6){   
                                                  main();   
                                              }else{   
                                                  app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Seperate Table Rows");   
                                              }   
                                              function main(){   
                                                  var doc = app.activeDocument,   
                                                  text = doc.textFrames;   
                                                  for(t=0;t<text.length;t++){   
                                                      table = text[t].tables;   
                                                      for(T=0;T<table.length;T++){   
                                                          row = table[T].rows;   
                                                          for(r=0;r<row.length;r++){   
                                                              cell = row[r].cells;   
                                                              if(cell[0].paragraphs.length > 1){   
                                                                  table[T].rows.add(LocationOptions.AFTER, row[r])   
                                                                  for(c=0;c<table[T].columns.length;c++){   
                                                                      row[r+1].cells[c].contents.move(LocationOptions.AT_BEGINNING, cell[c].paragraphs[-1].contents);   
                                                                      cell[c].paragraphs[-1].contents = "";   
                                                                      cell[c].characters[-1].contents = "";   
                                                                  }r--;   
                                                              }   
                                                          }   
                                                      }   
                                                   
                                                  }   
                                              }   
                                              

                                               

                                               

                                              Regards,

                                              K

                                              • 20. Re: Table paragraph split and move
                                                Laubender Adobe Community Professional & MVP

                                                Simply test the move() method on two text frames with differently formatted text on a page to get your feet wet:

                                                 

                                                var tf1 = app.documents[0].pages[0].textFrames[0];
                                                var tf2 = app.documents[0].pages[0].textFrames[1];
                                                
                                                tf1.texts[0].move( LocationOptions.AFTER , tf2.texts[0].insertionPoints[-1] );
                                                

                                                 

                                                Or try something like that:

                                                 

                                                tf1.texts[0].lines[0].move( LocationOptions.AFTER , tf2.texts[0].insertionPoints[0] );
                                                

                                                 

                                                Also see for details:

                                                Adobe InDesign CS6 (8.0) Object Model JS: Text

                                                 

                                                Regards,
                                                Uwe