15 Replies Latest reply on Oct 27, 2016 8:49 AM by Obi-wan Kenobi

    Setting Table Column Widths for particular Table Styles

    paulb22558018

      I'll start by saying that I'm new to both inDesign and to scripting. I've learned a lot in the last few weeks of muddling though on my own and have almost everything I need in place for inDesign to be a very effective tool for my purposes. However, there is one thing that has been driving me crazy for about two weeks.

       

      For the document that I'm working I have created several different Table Styles set at several different indenture levels. I will be using this document as a baseline. Meaning that I will repeat this document over and over again with minor tweaks to each new usage. These documents will then be combined into a larger book. All of the Table Styles will be repeated very often and I would like them all to be consistent throughout. As there is no way to set a column width in the Table Styles panel, I would like to have a script that will automatically size the columns for each specific Table Style.

       

      Easier said than done, I'm afraid.

       

      I've managed to piece together the below script from several searches on this and other forums. I may have things in there that I don't need, or that could be handled in a more effective manner. I'm open to suggestions on how to improve. Currently this script will maintain the Table Style that is set in inDesign and it will resize the columns. However, each new set of parameters overrides the previous one. I know I'm close. I may have taken a round-about way to get there, but I'm close. I just need a little help for the final stretch. I am truly grateful for any assistance that you can provide.

       

      Thanks,

       

      Paul

       

        • 1. Re: Setting Table Column Widths for particular Table Styles
          SJRiegel Adobe Community Professional & MVP

          I don't script, so I can't tell what your code it doing, but you might find the script that is linked in this discussion useful

           

          Re: column width in cell specs

           

          You start with a table that has a style set, and adjust the columns to what you want. You then can save the column widths inside the style and apply them to any table that has the style applied. 

          • 2. Re: Setting Table Column Widths for particular Table Styles
            paulb22558018 Level 1

            SJRiegel,

             

            Actually, I've already installed that script. It works really well, however the values don't travel from one file to the next. If I open a new file, I have do reenter the values. Some of the books that I'm working on will have 50 or 60 files in them. I'd like to find a way to embed the values in the script so that I don't want to have to reenter them in each file.

             

            Thanks for the suggestion, though. I do appreciate it. If I weren't working with so many files in each book, I would absolutely use that script.

            • 3. Re: Setting Table Column Widths for particular Table Styles
              SJRiegel Adobe Community Professional & MVP

              In  a quick test I just did, after saving the valves in a table style, I copied a table with that style into a new document. I then applied the style to a new table in the new document, and the script worked to apply the widths saved in the style to the new table.

              • 4. Re: Setting Table Column Widths for particular Table Styles
                Obi-wan Kenobi Adobe Community Professional

                Paul,

                 

                I let you finish!

                 

                 

                var myDoc = app.activeDocument;  
                var myTables = myDoc.stories.everyItem().tables.everyItem().getElements();
                  
                for ( var t = 0; t < myTables.length; t++ ) {
                    if ( myTables[t].appliedTableStyle.name == "xxx" ) {  
                                myTables[t].columns[0].width = "20mm";
                                myTables[t].columns[1].width = "30mm";
                                myTables[t].columns[2].width = "10mm";
                                }
                    if (  myTables[t].appliedTableStyle.name == "yyy" ) {  
                                myTables[t].columns[0].width = "40mm";
                                }
                    if (  myTables[t].appliedTableStyle.name == "zzz" ) {  
                                myTables[t].columns[0].width = "50mm";
                                }  
                }  
                

                 

                 

                (^/) 

                • 5. Re: Setting Table Column Widths for particular Table Styles
                  paulb22558018 Level 1

                  When I do the same thing, the "Load from Style" button is greyed out and not selectable.

                   

                  I have all of my Paragraph, Character, Object, and Table Styles saved in a master document that I load all of my styles from. When I load the Table Styles from this document, it doesn't import the column values from that script. Also, if I copy a table and paste it into a new document, it will import the table with those values but then I have to save them against that Table Style again before the "Load from Style" button becomes usable.

                   

                  Maybe I did something wrong when I installed that script.

                  • 6. Re: Setting Table Column Widths for particular Table Styles
                    paulb22558018 Level 1

                    I'm sure you've never heard this before and that I am not at all original, but I'm going to say it anyway...

                     

                    Thank you Obi-Wan. You were my only hope.

                     

                    Works great. I really appreciate the help.

                    • 7. Re: Setting Table Column Widths for particular Table Styles
                      Obi-wan Kenobi Adobe Community Professional

                      Paul,

                       

                      You could go on with Suzan and Gerald's very useful script to be known!

                       

                      (^/)

                      • 8. Re: Setting Table Column Widths for particular Table Styles
                        Laubender Adobe Community Professional & MVP

                        paulb22558018 wrote:

                        … however the values don't travel from one file to the next. If I open a new file, I have do reenter the values. …

                        Hi Paul,

                        I have to see why the values cannot travel from document to document.
                        So I just tried this with Gerald's AutoSpalte2.jsxbin script and InDesign CS6 v8.1.0 on Mac OSX 10.6.8.

                        And it worked as following:

                         

                        1. Define a new table style. Do not use the default one.

                        2. Apply the new table style to a table.

                        3. Format the width of the columns of that table using AutoSpalte2

                        4. Save the style to the format using AutoSpalte2 with "Speichern in Format 'xyz'"

                        5. Copy the table

                        6. Paste the table to a new document and introduce the new table format to that document

                        7. Load the predefined set of column widths from the table style using AutoSpalte2 with "Aus Format laden"

                         

                        At least that was working in my situation.

                         

                        Regards,
                        Uwe

                        • 9. Re: Setting Table Column Widths for particular Table Styles
                          paulb22558018 Level 1

                          Uwe,

                           

                          That's exactly how I did it. I still don't understand why the values won't travel for me. However, Obi-wan Kenobi was able to help me with a script that embeds the values into the script, which is what I was looking for. I'll still use the AutoSplate2 script for the occasional one-off table in my documents, it works really well for that. But the script that Obi-wan came up with was exactly what I was looking for. Thanks for the time you spent testing this for me. I really appreciate it.

                           

                          Best,

                           

                          Paul

                          • 10. Re: Setting Table Column Widths for particular Table Styles
                            Laubender Adobe Community Professional & MVP

                            Hi Paul,

                            Gerald's script will save the values with a label property/value pair with the table style.

                            You could detect this if you export a selected text frame with a table to the IDMS snippet format.

                             

                            Just did this and looked after the keyword "Label" after opening the snippet in my text editor.

                             

                            <TableStyle Self="TableStyle/TableStyle-1" Name="TableStyle-1" KeyboardShortcut="0 0">
                                <Properties>
                                    <BasedOn type="string">$ID/[No table style]</BasedOn>
                                    <Label>
                                        <KeyValuePair Key="gs_autospalte" Value="20#20#20#auto#auto" />
                                    </Label>
                                </Properties>
                            </TableStyle>
                            

                             

                            So you can see, that in my example there are defined:
                            5 values for five columns separated by: #

                            20#20#20#auto#auto

                             

                            The first three are fixed values and the other two are auto values.

                            Maybe that will help you to solve your problem with AutoSpalte2.jsxbin.

                             

                            Cheers,
                            Uwe

                            • 11. Re: Setting Table Column Widths for particular Table Styles
                              paulb22558018 Level 1

                              So...now that I have the columns the correct size (thank you Obi-wan), is there a way to automatically insert a column heading into each of these Table Styles?

                               

                              If, for example, I wanted:

                               

                              row[0].column[0] to read "IDENTIFICATION",

                              and

                              row[0].column[1] to read "DESCRIPTION"

                               

                              for a specific Table Style when I ran this script. Could I add that into the same section as the column widths, or would I need to start a new function after that was completed?

                              • 12. Re: Setting Table Column Widths for particular Table Styles
                                Obi-wan Kenobi Adobe Community Professional

                                Last time, Paul!

                                 

                                If headers in tables, the script passes away.

                                If not, it plays its cool game!…

                                 

                                I let you finish! 

                                 

                                var myDoc = app.activeDocument;    
                                var myTables = myDoc.stories.everyItem().tables.everyItem().getElements();  
                                    
                                for ( var t = 0; t < myTables.length; t++ ) {
                                    var myRow = myTables[t].rows[0];
                                    if ( myRow.rowType !== RowTypes.headerRow ) {
                                            if ( myTables[t].appliedTableStyle.name == "xxx" ) {
                                                  myTables[t].rows.add(LocationOptions.BEFORE, myRow);   
                                                  myTables[t].rows[0].rowType = RowTypes.headerRow;
                                                  myTables[t].rows[0].cells[0].contents = "aaa";
                                                  myTables[t].rows[0].cells[1].contents = "bbb";
                                                  myTables[t].rows[0].cells[2].contents = "ccc";
                                                  myTables[t].rows[0].cells[3].contents = "ddd";
                                            }  
                                            if ( myTables[t].appliedTableStyle.name == "yyy" ) {
                                                  myTables[t].rows.add(LocationOptions.BEFORE, myRow);   
                                                  myTables[t].rows[0].rowType = RowTypes.headerRow;
                                                  myTables[t].rows[0].cells[0].contents = "eee";
                                                  myTables[t].rows[0].cells[1].contents = "fff";
                                            }  
                                            if ( myTables[t].appliedTableStyle.name == "zzz" ) {
                                                  myTables[t].rows.add(LocationOptions.BEFORE, myRow);   
                                                  myTables[t].rows[0].rowType = RowTypes.headerRow;
                                                  // …………
                                            }  
                                    }
                                }
                                

                                 

                                [ … To be validated!  ;-) ]

                                 

                                (^/)

                                • 13. Re: Setting Table Column Widths for particular Table Styles
                                  paulb22558018 Level 1

                                  Obi-wan,

                                   

                                  Forget Jedi. You, Sir, are a god. Thank you so much. You have no idea how much future work you just saved me. Thanks for your help.

                                  • 15. Re: Setting Table Column Widths for particular Table Styles
                                    Obi-wan Kenobi Adobe Community Professional

                                    Just for comment! About post#4, is it possible to concentrate more … and of course make it work!! 

                                     

                                    var myTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();  
                                        
                                    for ( var t = 0; t < myTables.length; t++ ) {
                                            if ( myTables[t].appliedTableStyle.name == "xxx" && myWidths = [30, 10, 25, 30] )
                                                for (var c = 0; c < myWidths.length; c++) myTables[t].columns[c].width = myWidths[c];
                                            if ( myTables[t].appliedTableStyle.name == "yyy" && myWidths = [10, 20] )
                                                for (var c = 0; c < myWidths.length; c++) myTables[t].columns[c].width = myWidths[c];
                                            if ( myTables[t].appliedTableStyle.name == "zzz" && myWidths = [10, 50, 30] )
                                                for (var c = 0; c < myWidths.length; c++) myTables[t].columns[c].width = myWidths[c];
                                    }
                                    

                                     

                                    (^/)