13 Replies Latest reply on Mar 3, 2013 2:12 PM by Loic.Aigon

    [JS][CS5.5] Duplicate table




      On this forum, I found a great piece of code for duplicating a table. It works great. However, how do I get the new table object?

                  var myOldTable = app.selection[0].parent.parent;

                  var myOldTablePosition = myOldTable.storyOffset;

                  var myTextFrame = app.selection[0].parent.parent.parent;

                  var myNewTablePosition = myTextFrame.insertionPoints.nextItem(myOldTablePosition);


                  var myInsertionPoint = myOldTable.storyOffset;

                  var imyInsPointIndex = myInsertionPoint.index;

                  var myStory = myInsertionPoint.parentStory;


                  var myCharThatHoldsMyTable = myStory.characters[imyInsPointIndex];


                  myCharThatHoldsMyTable.duplicate(LocationOptions.AFTER,myNewTablePosition );


                  var myNewTable = ???; (as table object)


        • 1. Re: [JS][CS5.5] Duplicate table
          JohnDevlon Level 1

          -- UPDATE --




          After doing some testing I noticed that, when duplicating the table after the original one, the index of the new table in the table collection of the textframe, is the one directly after the original one (and not @ the end of the collection).


          So I created a small function to get the next table.



          function getNextTable(myTextFrame, myTable){

             for(var i=0; i<myTextFrame.tables.length;i++){

                    if (myTextFrame.tables[i].index == myTable.index){

                              var NextTable = myTextFrame.tables[i+1];




               return NextTable;



          Can some tell me if this is the correct approach and failproof?



          • 2. Re: [JS][CS5.5] Duplicate table
            JohnDevlon Level 1

            -- UPDATE 2 --


            Getting the next table in the story instead of in the textframe works better when using a multi textframe story flow...


            function getNextTableInStory(myStory, myTable){

                 for(var i=0; i<myStory.tables.length;i++){

                      if (myStory.tables[i].index == myTable.index){

                                var NextTable = myStory.tables[i+1];




                 return NextTable;


            • 3. Re: [JS][CS5.5] Duplicate table
              Marc Autret Level 5

              Hi John,


              An interesting way to avoid exploring parent story and textframes when you need to convert an insertion point into a character, is to simply rewrite the specifier of the InsertionPoint so that it points out to a Character. In the following code I just replace 'insertion-point' by 'character':



              function dupTable(/*Cell|Table*/obj, /*bool=false*/DUP_BEFORE)
                  var LO = +LocationOptions[DUP_BEFORE ? 'before' : 'after'],
                  // Make sure that obj is
                  // a Cell, Table, Row, or Column
                  // ---
                  if( !obj.hasOwnProperty('rows') ) return null;
                  // Seek the table
                  // ---
                  while( !(obj instanceof Table)){obj=obj.parent;}
                  // Get the character that holds the table
                  // ---
                  c = resolve(
                      obj.            // Table
                      storyOffset.    // InsertionPoint
                      toSpecifier().    // contains "insertion-point" once
                  // Duplicate the character
                  // ---
                  c = c.duplicate(LO,c);
                  // Return the dup table
                  // ---
                  return c.tables[0];
              // ---
              // Assumed that a table or some cell is selected
              // ---
              var newTable = dupTable(app.selection[0]);




              • 4. Re: [JS][CS5.5] Duplicate table
                [Jongware] Most Valuable Participant

                Slightly easier: the duplicate method returns the duplicate. In your case, the newly created table is table #0 (the first--and only) 'inside' this single character:


                newCharThatHoldsTheTable = myCharThatHoldsMyTable.duplicate(LocationOptions.AFTER,myNewTablePosition );
                newTable = newCharThatHoldsTheTable.tables[0];


                Alternatively: the index for tables is a simple incremental counter. Therefore, if you know the index of your original table (myOldTable), the index of the next one is myOldTable.index+1:


                alert (myOldTable.index);


                It's good, though, to see you're having a good time browsing through all of the various objects Time spent learning is time spent well.

                1 person found this helpful
                • 5. Re: [JS][CS5.5] Duplicate table
                  JohnDevlon Level 1

                  Thanx Marc. Your solution works great....

                  • 6. Re: [JS][CS5.5] Duplicate table
                    JohnDevlon Level 1

                    Hi Jongware,


                    Thanx for your feedback. The information you provided is very useful ....



                    • 7. Re: [JS][CS5.5] Duplicate table
                      Loic.Aigon Adobe Community Professional

                      Hi Marc,


                      First of all, thanks for this amazing snippet. Awesome


                      I have a question regarding to duplicating a row or a column from table A to table B. These items can have merged cells and I wonder what's the best approach to identically copy a row/column structure from one table to another. I have started working on some snippet but not sure it's the best way.


                      So far my option are :


                      A) loop through every cell of row/column of table A. Look at some possible rowSpan and columnSpan and do some cooking merging in table B ( preceedingly adding the necessary row/columns )

                      I think i can get something in this direction but I can already say that it will be unrespectful of any designs ( background if any… )


                      B) Copy/Paste after selection : this one gives me chills just thinking about performance and possible leaks


                      C) Externalizing the duplication by playing with some icml/snippet content


                      Or do you have another toSpecifier magic ?


                      Thanks for any hint


                      Best to all

                      • 8. Re: [JS][CS5.5] Duplicate table
                        Laubender Adobe Community Professional & MVP

                        @Loic – option "B" would give me headaches, too.
                        But for now I think it's the "easiest" option we have…


                        And: there are issues with "B" other than "performance":
                        The target of the pasted row seems always to win in regards to cell height, because the "minimum" or "exact" values of the original are not honoured!


                        This is really annoying…

                        I have to investigate more time for checking the exact rules here…



                        • 9. Re: [JS][CS5.5] Duplicate table
                          Loic.Aigon Adobe Community Professional



                          Thanks a lot Uwe. I was fearing that answer



                          • 10. Re: [JS][CS5.5] Duplicate table
                            Marc Autret Level 5

                            Hi Loïc,


                            InDesign table's structure sucks. I don't know any clean algorithm to duplicate part of a table into another one with respect to spans, styles etc. Even with copy/paste approach you probably will meet tricky issues, as pasting cells to cells is subject to various restrictions.


                            I suppose John Hawkinson would have suggested option (C)—playing w/ ICML—because at least you could handle and rearrange cells via XML. However, reinjecting ICML "on the fly" in the active document looks like black magic to me.




                            • 11. Re: [JS][CS5.5] Duplicate table
                              Loic.Aigon Adobe Community Professional

                              Hi Marc,


                              How you sum things up right ! I may have a look at the C option. Maybe playing with a snippet can do it.



                              • 12. Re: [JS][CS5.5] Duplicate table
                                Loic.Aigon Adobe Community Professional

                                Hi all,


                                For what it's worth, the snippet approach looks promising but I can't think of all the side effects right now.



                                • 13. Re: [JS][CS5.5] Duplicate table
                                  Loic.Aigon Adobe Community Professional

                                  Hi all,


                                  Just a heads up for who might be interested into this. I have solved this duplication concern by using the C option. I combine icml files and eventually get my merged table in short times and 100% reliability. I don't even care about table/row/cells graphic attributes because they are all embedded within the icml xml nodes. Only sensible part is to shift footer rows if any and identify row/columns that host merged cells.


                                  So I think it's definitively the way to go