15 Replies Latest reply on Oct 11, 2015 8:04 AM by Peter Kahrel

    Table "width"

    sPretzel Level 1

      Hi,

       

      I created a table style and a cell style, both of which have zero stroke size. When I insert the table style in a text frame, the table width is slightly shorter than the width of the text frame (the cell inside, it's a one-cell table, is shorter by 1pt). I expect it to be the same. I don't understand why it's not. By the way, is there a place in Adobe's online documentation that stated that inserting a table in a text frame would default to the width of the text frame?

       

      I do realise that I cannot set the width of a table but can only set the width of a cell. However, that can't be done in a cell style.

       

      Any insight would be helpful as to how to automatically make the table's width the same as the text frame, at the time of inserting the table. This is for CS4.

       

      Thanks.

        • 1. Re: Table "width"
          Peter Kahrel Adobe Community Professional & MVP

          You could fit the frame to its content (Cmnd/Ctrl+Alt+C).

          • 2. Re: Table "width"
            sPretzel Level 1

            Hi. Not in my case because the text frame has a constant width, so I have to "fit" the table (content) to the text frame's width.

            • 3. Re: Table "width"
              Obi-wan Kenobi Adobe Community Professional

              Peter,

               

              I let you comment [ as you know, I'm not a [JS] specialist!  ]

               

              InDesign JSX User Script to Adjust Table Widths Relative to Frame Width · GitHub

              • 4. Re: Table "width"
                Peter Kahrel Adobe Community Professional & MVP

                If it works, it works!

                 

                P.

                • 5. Re: Table "width"
                  Obi-wan Kenobi Adobe Community Professional

                  Aha! Peter, it's an excellent answer! I've tested it! It works! 

                  • 6. Re: Table "width"
                    Obi-wan Kenobi Adobe Community Professional

                    If selected table:

                     

                    myTable = app.selection[0].parent;

                    if (!myTable instanceof Table){

                      myTable = myTable.parent;

                    }

                    if (!myTable instanceof Table){

                      alert("Select a cell or place your cursor in a cell, and try again");

                      exit();

                    }

                    myTable.columns.everyItem().width = "80";

                     

                    Other way given by Ariel, from Id-Extras.com in How can I maintain (1) column widths and (2) a header when replacing a table?

                    • 7. Re: Table "width"
                      Obi-wan Kenobi Adobe Community Professional

                      In this second way, the deal is simple: create your table and, as the insertion point is in the table cell, just play a keyboard shortcut (associated to the script)! 

                      • 8. Re: Table "width"
                        sPretzel Level 1

                        Hi. Thanks for those workarounds. But is that 1pt delta between the frame and the table widths anywhere to be set in ID? The default table comes with a 1pt border which results in a 1pt delta, when there is a border in place, but it's weird this remains when all border stroke sizes are set to zero.

                        • 9. Re: Table "width"
                          Peter Kahrel Adobe Community Professional & MVP

                          Looks like a bug to me.

                          • 10. Re: Table "width"
                            Obi-wan Kenobi Adobe Community Professional

                            Peter,

                             

                            When we create a table without style, by default, it has a stroke 1 pt all around! can we modify this "by-default"?

                            • 11. Re: Table "width"
                              Peter Kahrel Adobe Community Professional & MVP

                              That doesn't work. Even when you set the border of the [Basic Table] style to 0 points, the gap still appears. But this script adjusts the table's columns so that the table fits its frame:

                               

                              // Fit a frame's table to the frame's width.
                              // The frame can be wider or narrower than the table.
                              
                              (function () {
                                try {
                                  var frame = app.selection[0];
                                  var diff = frame.geometricBounds[3] - frame.geometricBounds[1] - frame.tables[0].width;
                                  var columns = frame.tables[0].columns.everyItem().getElements();
                                  var space = diff/columns.length;
                                  for (var i = columns.length-1; i >= 0; i--) {
                                    columns[i].width += space;
                                  }
                                } catch (_) {
                                  // Fail silently
                                }
                              }());
                              

                               

                              Assign this to a keyboard shortcut. Select a frame with a table and the table will be spaced proportionally to fit its parent frame. Not tested with merged cells.

                              The table is fitted to the frame irrespective of the frame's width relative to the table's width.

                               

                              Peter

                              • 12. Re: Table "width"
                                Obi-wan Kenobi Adobe Community Professional

                                Peter,

                                 

                                It won"t work if several tables in the frame!

                                What we need first is: I create a table and, as the insertion point is in this table, I just play a keyboard shortcut (associated to the script) to adjust its width!

                                Imagine now we want to enlarge all the tables of a story, in the entire doc or simply a selection!

                                 

                                I know! It's more complex that we imagined earlier! 

                                • 13. Re: Table "width"
                                  Peter Kahrel Adobe Community Professional & MVP

                                  > It won't work if several tables in the frame!

                                   

                                  Well, that's not what sPretzel asked about, but adjusting the script to deal with all tables in the frame is not difficult. The next question is whether the script should target just the selected frame or the whole story, which may take more than one frame.

                                   

                                  P.

                                  • 14. Re: Table "width"
                                    Obi-wan Kenobi Adobe Community Professional

                                    Peter,

                                     

                                    I have a question: Can we insert this code, written by Jongware, to detect if we want to treat one table or all of them, and finally have only one script? Thanks in advance! 

                                     

                                     

                                    function checkWhichTable()

                                    {

                                    // ensure the user made a selection

                                    if (app.selection.length != 1)

                                    return null;

                                    var currentTable = app.selection[0];

                                    if (currentTable.hasOwnProperty("baseline"))

                                    {

                                    currentTable = app.selection[0].parent;

                                    }

                                    while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column)

                                    currentTable = currentTable.parent;

                                    if (!(currentTable instanceof Table))

                                    {

                                    // No table selected

                                    return null;

                                    }

                                    return currentTable;

                                    }

                                     

                                    http://indesignsecrets.com/tackling-tables-through-scripting.php

                                    • 15. Re: Table "width"
                                      Peter Kahrel Adobe Community Professional & MVP

                                      Jongware's code gives you the selected table, or nothing if the selection doesn't involve any table, so you need something different. Something like this:

                                       

                                      // No selection at all: do all tables in the active document
                                      // Selection is insertion point in cell: do only the current table
                                      // Selection is a text frame or an insertion point outside a table: do all the story's tables
                                      
                                      (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 fitTables (tables) {
                                          for (var i = tables.length-1; i >= 0; i--) {
                                            try {
                                              var frame = tables[i].storyOffset.parentTextFrames[0];
                                              var diff = frame.geometricBounds[3] - frame.geometricBounds[1] - tables[i].width;
                                              var columns = tables[i].columns.everyItem().getElements();
                                              var space = diff/columns.length;
                                              for (var j = columns.length-1; j >= 0; j--) {
                                                columns[j].width += space;
                                              }
                                            } catch (_) {
                                              // Fail silently
                                            }
                                          }
                                        }
                                      
                                        if (app.documents.length > 0) {
                                          fitTables (getTables());
                                        }
                                      
                                      }());
                                      

                                       

                                      A situation not handled by the script is when you select two or more unthreaded text frames, each with a table: the script processes just the first frame in the selection.

                                       

                                      P.