11 Replies Latest reply on Dec 2, 2015 7:39 AM by briank83163125

    Adding row at a table in indesign CS6 from javascript

    MahaGh

      I have a table in my indesign page.It has one column and several rows. I want to add a row at the begining of the table  and add some text content to it.

      I tried some provided code but none have worked.

      This is how i reference the table object.

       

      var mytable =myPage.textFrames.item(0).tables.item(0);

      I tried each the following but none worked and gave an error

       

         mytable.rows[0].cells[0].insertionPoints[0].contents =  "TEST TEST";

                                                        

         mytable.rows.add( LocationOptions.AFTER, mytable.rows[-1] );

                                                       

         mytable.rows.add( LocationOptions.Before, mytable.rows[0] );

       

      What should i do?

        • 1. Re: Adding row at a table in indesign CS6 from javascript
          Peter Spier Most Valuable Participant (Moderator)

          Moved to the scripting forum....

          • 2. Re: Adding row at a table in indesign CS6 from javascript
            Jump_Over Level 5

            Hi,

             

            JS is case sensitive, so "LocationOptions.Before" gives an error.

             

            use this (assumes  that proper textFrames[0] is on myPage)

             

            MT = myPage.textFrames[0].tables[0];
            MR = MT.rows.add( LocationOptions.BEFORE, MT.rows[0] );
            MR.cells[0].contents = "test TEST";
            

             

            Jarek

            • 3. Re: Adding row at a table in indesign CS6 from javascript
              MahaGh Level 1

              I used this

               

              myPage = myDocument.pages.item(x); 

               

              var MT = myPage.textFrames[0].tables[0];

              var MR = MT.rows.add( LocationOptions.BEFORE, MT.rows[0] );

              MR.cells[0].contents = "test TEST";       

               

              But i got on  line var MR = MT.rows.add( LocationOptions.BEFORE, MT.rows[0] ); an error saying:

               

              Object is Invalid

               

              Any help?

              • 4. Re: Adding row at a table in indesign CS6 from javascript
                Jump_Over Level 5

                Hi,

                 

                include as a 2nd line:

                myPage.textFrames[0].select();
                

                 

                which frame is selected?

                Is there any table inside?

                 

                Jarek

                • 5. Re: Adding row at a table in indesign CS6 from javascript
                  MahaGh Level 1

                  The table is inside the frame of the page.There is only one frame in the page.

                   

                  When i have a table inside the frame  i have frame content=""

                   

                  myTextFrame = myPage.textFrames.item(0);

                  tempframe = myTextFrame.contents;  // when we have a table it is = ""

                   

                  and by using this

                  var mytable =myPage.textFrames.item(0).tables.item(0);

                  alert(mytable);

                   

                  I got "objecttype Table"

                  • 6. Re: Adding row at a table in indesign CS6 from javascript
                    Vamitul Level 4

                    try alert(mytable.isValid)

                    • 7. Re: Adding row at a table in indesign CS6 from javascript
                      MahaGh Level 1

                      var mytable =myPage.textFrames.item(0).tables.item(0);

                      alert(mytable.isValid);

                       

                      gave false

                      • 8. Re: Adding row at a table in indesign CS6 from javascript
                        Vamitul Level 4

                        that means that your table does not reside in the textframes[0].

                         

                        does your document has textframes on the master page applied to the said page? those apear too in the textFrames collection. 

                        Also, keep in mind that the textframes collection is orederd not by z-index, position or anything, but by the order of creation.

                        as i don't know your document, or what exactly you are trying to achieve, i can't give you much help. If you are trying to manipulate all the tables in a story, you can use something like myStory.tables.everyItem().getElements()

                        If you are trying to manipulate a table inside a selected textFrame you can use something like myFrame=app.activeDocument.selection[0]

                        • 9. Re: Adding row at a table in indesign CS6 from javascript
                          MahaGh Level 1

                          I am actually looping over every page in my indesign file, every page contains 1 textframe with text inside it. I add a new textframe to the page and then move the new textframe content into the begginning of the existing textframe then delete the created one

                           

                          myPage = myDocument.pages.item(x);

                          myTextFrame = myPage.textFrames.item(0);

                           

                          newframe = myPage.textFrames.add();

                          newframe.contents ="TEST TEST" ;

                          newframe.paragraphs[0].appliedFont = app.fonts.item("Times New Roman");

                          newframe.paragraphs[0].fontStyle = "Bold";

                                                                     

                                                                      newframe.parentStory.paragraphs.item(0).words.item(0).move(LocationOptions.AT_BEGINNING,m yTextFrame);

                          newframe.remove();

                           

                          This was working correctly on all files until i faced one with a table. The code stoppes at this line

                           

                          newframe.parentStory.paragraphs.item(0).words.item(0).move(LocationOptions.AT_BEGINNING,my TextFrame);

                           

                          and Indesign becomes Not Responding

                           

                          The table is visually inside the page textframe. I need to do the same logic when having a table i couldnt call the move function when the page has a table and not text in its textframe. I thought abt accessing the table in the page and adding a row at the beginning then writing in it what i need, alternative to what am doing with normal text instead of creating new frame and then moving text to original textframe at beginning.

                           

                          When my textframe.content="" i know that there is a table and i need to alter it. Otherwise i do add new frame ,move content then delete.

                           

                          Any suggestions?

                          • 10. Re: Adding row at a table in indesign CS6 from javascript
                            Vamitul Level 4

                            Oh my...

                             

                            You are overcomplicating things a lot. more than a lot actualy.

                             

                            First of all, you don't NEED another textframe just to add some text to another frame.

                            myTextFrame.insertionPoints[0].appliedFont="Times New Roman\tBold";

                            myTextFrame.insertionPoints[0].contents="Test Test test\r";

                            second of all, myTextFrame.contents=="" means that the said textframe has ABSOLUTLY NO CONTENT!! not even a table. From here on, things get a bit murky. In a story, a table is considered a single character, so if a table starts on let's say page 3 and spans 4 pages, the table's parent text frame is the one on page 3, and the rest of the frames are considered empty. From your description, i'm guessing this is your problem too.

                            "fixing" it is an even trickier.

                            simplifying a bit:

                            compare the parentTextFrames property of the first InsertionPoint of the cells in the table, and when the parentTextFrames is different, you reached a new frames and page, so insert the row before the second cell, AND set that new row's keepWithNextRow property to true.

                            • 11. Re: Adding row at a table in indesign CS6 from javascript
                              briank83163125 Level 1

                              This worked for me.

                              This will work for all tables in your doc.  I assume you want to do this for multiple tables otherwise why wouldn't you just click on the row and insert a row.

                               

                              app.activeDocument.stories[s].tables[t].rows.add(LocationOptions.AT_BEGINNING);

                               

                              I finally found some success with this method.  I found a string here: https://indesignsecrets.com/tackling-tables-through-scripting.php where adding AT_END worked for adding a row to the bottom, and just kept trying words like "TOP" "FRONT" until "BEGINNING" worked.  Hope this helps someone else since it's been 2 years since your original post.