13 Replies Latest reply on Apr 7, 2013 11:46 PM by Muthuraj Durairaj

    [JS IDCS5] Resize Tables proportionally

    stoereee

      Hey,

       

      When I use:

       

      myTable.width = "200mm"; 

       

      the table sizes to 200mm width, but resets all the column widths to same width sizes.

       

      How to resize a table so columns keep there own proportional width?

       

      Regards, Sjoerd

        • 1. Re: [JS IDCS5] Resize Tables proportionally
          csm_phil Level 4

          Hi Sjord,

           

          You have try this js line:

           

          var myTabColumn= app.activeDocument.stories.everyItem().tables.everyItem().columns.everyItem();

          // you can chage what width you need?
          myTabColumn.width="50mm";

           

          thx

          csm_phil;

          • 2. Re: [JS IDCS5] Resize Tables proportionally
            stoereee Level 1
            myTabColumn.width="50mm";


            This will size all the columns within a table to 50mm width. So when a table has got 4 columns, the size of the table will be 200mm.

             

            I want to size a table where the columns keep there own proportional width.

             

            For example:

            When a table (width size of 100mm) that includes 3 columns (40mm, 40mm, 20mm) resizes to 200mm, the columns have to resize to 80mm, 80mm and 40mm.

            • 3. Re: [JS IDCS5] Resize Tables proportionally
              [Jongware] Most Valuable Participant

              Strange, somehow it occurred to me this coulda/shoulda/woulda worked! I'm even pretty sure I used this, in the past. But on my CS4 I cannot get it to work either!

               

              You'll have to use this workaround:

               

              myTable = app.selection[0].parent;
              factor = 115/myTable.width;
              for (col=0; col<myTable.columns.length; col++)
               myTable.columns[col].width *= factor;
              

               

               


              (Edit: You'll have to substitute the "115" in ln 2 for your own target width, of course)

              • 4. Re: [JS IDCS5] Resize Tables proportionally
                stoereee Level 1

                Strange, somehow it occurred to me this coulda/shoulda/woulda worked! I'm even pretty sure I used this, in the past. But on my CS4 I cannot get it to work either!

                 

                You'll have to use this workaround:

                 

                myTable = app.selection[0].parent;
                factor = 115/myTable.width;
                for (col=0; col<myTable.columns.length; col++)
                myTable.columns[col].width *= factor;

                 

                It's pretty close to what I want. Only the result of the total width of the tabel is not always 115 (ln 2). The totaal width depends on the widths of the columns

                • 5. Re: [JS IDCS5] Resize Tables proportionally
                  [Jongware] Most Valuable Participant

                  stoereee wrote:

                  It's pretty close to what I want. Only the result of the total width of the tabel is not always 115 (ln 2). The totaal width depends on the widths of the columns

                   

                  What does it depend on, exactly? For example, it's a quick and easy fix to size your tables to its containing frame:

                   

                  myTable = app.selection[0].parent;
                  parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];
                  factor = parentWidth/myTable.width;
                  for (col=0; col<myTable.columns.length; col++)
                   myTable.columns[col].width *= factor;
                  
                  • 6. Re: [JS IDCS5] Resize Tables proportionally
                    stoereee Level 1
                    factor = 115/myTable.width;

                     

                    When I run the script with factor = 115/myTable.width, the result of the total width of the table is not exact 115.

                    • 7. Re: [JS IDCS5] Resize Tables proportionally
                      Dave Saunders Level 4

                      How much does it miss by? A very small fraction? If so, adjust the width of the last (or first, or widest) column by that amount for an exact fit.

                       

                      Dave

                      • 8. Re: [JS IDCS5] Resize Tables proportionally
                        [Jongware] Most Valuable Participant

                        I suspect it has to do with the outside stroke widths. I think I noticed something like that once, but the tables I usually work with don't come with vertical lines.

                         

                        (You can test this with your measurement units set to points and see if the difference equals something like twice your stroke width. Or half of that. Or sth.)

                        • 9. Re: [JS IDCS5] Resize Tables proportionally
                          Muthuraj Durairaj Level 1

                          Hi Jongware,

                           

                          I have a need to find all the tables in an document and resize the table width to its containing frame. I tried the code you provided and it is not working for me (Indesign CS6 v8.0.1)

                           

                          Below is the code you provided. Please let me know what I did wrong. Thanks.

                           

                           

                          myTable = app.selection[0].parent; parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1]; factor = parentWidth/myTable.width; for (col=0; col<myTable.columns.length; col++) myTable.columns[col].width *= factor;

                           

                           

                          Regards,

                          Muthuraj. D

                          • 10. Re: [JS IDCS5] Resize Tables proportionally
                            [Jongware] Most Valuable Participant

                            Muthuraj Durairaj wrote:

                             

                            I have a need to find all the tables in an document and resize the table width to its containing frame. I tried the code you provided and it is not working for me (Indesign CS6 v8.0.1)

                             

                            You'll have to explain exactly what "is not working".

                            • 11. Re: [JS IDCS5] Resize Tables proportionally
                              Muthuraj Durairaj Level 1

                              Hi Jongware,

                               

                              Apologies.

                               

                              I am receiving the error message

                               

                              Error number: 55

                              Error String: Object does not support the property or method 'geometricBounds'

                              Line: 2

                              Source: parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];

                               

                               

                              I did just copy and paste the code in a jsx file and tried to run and got the above error message.

                               

                              myTable = app.selection[0].parent;

                              parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];

                              factor = parentWidth/myTable.width;

                              for (col=0; col<myTable.columns.length; col++)

                              myTable.columns[col].width *= factor;

                               

                              Do we need to add anything more other than this? Please help me on this.

                              Moreover, I need to loop through every table frames in a document and need to fit them within the text frame.

                               

                              Regards,

                              Muthuraj. D

                              • 12. Re: [JS IDCS5] Resize Tables proportionally
                                [Jongware] Most Valuable Participant

                                Muthuraj Durairaj wrote:

                                I am receiving the error message

                                 

                                Error number: 55

                                Error String: Object does not support the property or method 'geometricBounds'

                                Line: 2

                                Source: parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];

                                 

                                That happens in (I think) three different circumstances. First, maybe you don't have anything selected at all. In that case, the script has nothing to do ... Second, if your table is entirely inside overset text -- overset text doesn't have a 'parent' frame to get its width from. Third, when you have nested tables and selected the inner one. In that case, its parent is a "Cell", rather than a text frame (which the current script assumes).

                                 

                                I did just copy and paste the code in a jsx file and tried to run and got the above error message.

                                 

                                In that case, you may not have selected a table. The script needs to know what table to run on.

                                 

                                Moreover, I need to loop through every table frames in a document and need to fit them within the text frame.

                                 

                                A quick Google shows Peter Kahrel addresses this in his O'Reilly title "Scripting InDesign CS3/4 with JavaScript". Another script that shows how to process all tables can be found on http://indesign.hilfdirselbst.ch/tabellen/allen-tabellen-eines-dokuments-ein-tabellenforma t-zuweisen.html

                                • 13. Re: [JS IDCS5] Resize Tables proportionally
                                  Muthuraj Durairaj Level 1

                                  Hi Jongware,

                                   

                                  Thanks a lot for your help. I have combined both the scripts you have provided and modified to the following. It will fit the frame to the table width for each table in the document.

                                   

                                  var myDocument=app.activeDocument;

                                     

                                  for (s=0; s<myDocument.stories.length; s++) {

                                   

                                   

                                          for (t=0; t<myDocument.stories[s].tables.length; t++) {

                                         

                                              var myTable = myDocument.stories.item(s).tables.item(t);

                                              var myTableParent = myTable.parent;

                                              var parentWidth = myTableParent.geometricBounds[3] - myTable.parent.geometricBounds[1];

                                              var factor = parentWidth/myTable.width;

                                              for (col=0; col<myTable.columns.length; col++) {

                                                  myTable.columns[col].width *= factor;

                                                  }

                                          }

                                  }

                                   

                                   

                                  It is working perfectly by looping through all the tables in the document. Once again, thanks a lot for your help

                                   

                                  Regards,

                                  Muthuraj. D