8 Replies Latest reply on Sep 18, 2015 1:53 PM by mbrannon47

    How can I maintain (1) column widths and (2) a header when replacing a table?

    mbrannon47 Level 1

      I am attempting to format a series of tables using simple, two-column Excel files that look like this:

       

      Screen Shot 2015-09-07 at 4.04.36 PM.png

       

      I have already created a table style, but I am finding myself having to do two additional steps each time I replace existing data with a new Excel file:

       

      Challenge #1: Manually set each column width to 3 inches.

      Challenge #2: Manually convert the top row to a header.

       

      Each of these tasks only takes a few seconds to set up, but I'm having to do hundreds of these and the time adds up. Here is my existing workflow:

       

      1. Select the existing, properly-formatted table.

       

      Screen Shot 2015-09-07 at 4.09.59 PM.png

       

      2. File > Place... (Replace Selected Item and Show Import Options checked)

       

      Screen Shot 2015-09-07 at 4.10.44 PM.png

       

      3. Import as an Unformatted Table using the custom Table Style 1 created for this document.

       

      Screen Shot 2015-09-07 at 4.08.54 PM.png

       

      Note: Once dropped in, the previous column widths are lost and there are no identified headers:

       

      Screen Shot 2015-09-07 at 4.08.06 PM.png

       

      4. I manually adjust the column widths to 3 inches.

       

      Screen Shot 2015-09-07 at 4.08.23 PM.png

       

      5. Finally, I convert the top row to a header.

       

      Screen Shot 2015-09-07 at 4.08.34 PM.png

       

      The result is a perfectly formatted table, precisely 6 inches wide, each column taking up half the width.

       

      Screen Shot 2015-09-07 at 4.08.40 PM.png

       

      Any ideas for maintaining the column width upon replacing the Excel file and/or automatically formatting the top row as a header would be greatly appreciated!

        • 1. Re: How can I maintain (1) column widths and (2) a header when replacing a table?
          TᴀW Adobe Community Professional & MVP

          Something like this should do the trick. Copy the script into a plain text editor, save with .jsx extension, and place into your InDesign scripts folder. If you're doing this a lot, it would be worthwhile to assign a shortcut key to the script.

           

          To use, place your cursor in a cell in the table, or select a cell or a row, and run the script.

           

           

          //BS"D
          // Copyright (c) 2015 by www.Id-Extras.com
          // Free to use, but please do not remove this notice.
          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 = "3 in";
          myTable.rows[0].rowType = RowTypes.HEADER_ROW;
          
          
          • 2. Re: How can I maintain (1) column widths and (2) a header when replacing a table?
            Willi Adelberger Most Valuable Participant

            I do it with guides, which I create before I adjust the table.

            • 3. Re: How can I maintain (1) column widths and (2) a header when replacing a table?
              Laubender Adobe Community Professional & MVP

              Did you test your script with InDesign CS5.5 ?

               

              myTable.rows[0].rowType = RowTypes.HEADER_ROW;
              

               

              will not work, if you select a cell and run the script from the ESTK with CS5.5.

              The error message in my German version of InDesign is:

               

              "Der Zeilentyp kann nicht festgelegt werden."

              "The cell type cannot be declared" (or "assigned" maybe, I'm not sure how this message will translate to English).

               

              Further, if you select the whole table and run the script from the ESTK a different error message will be thrown in line 12:

              "Object does not support property or method columns"

               

              Uwe

              • 4. Re: How can I maintain (1) column widths and (2) a header when replacing a table?
                TᴀW Adobe Community Professional & MVP

                Laubender wrote:

                 

                Did you test your script with InDesign CS5.5 ?

                 

                No, certainly not. It would have been either CS6 or CC2015.

                Also I suppose the other errors you mention are because of CS5.5. Works fine in CS6.

                I didn't notice that the OP mentioned CS5.5, though. It's 4 versions old!

                • 5. Re: How can I maintain (1) column widths and (2) a header when replacing a table?
                  mbrannon47 Level 1

                  Indeed, I am using CC 2015. I just saw your reply above and I'm beyond excited to try this out. Thank you so much for your help!

                  • 6. Re: How can I maintain (1) column widths and (2) a header when replacing a table?
                    mbrannon47 Level 1

                    This seems to work ALMOST perfectly, with the exception being the second column maintains its original width. Any ideas?

                     

                    EDIT: If I select BOTH columns before running the script, it works fine. I'm not sure if there's a quick adjustment I can make, but selecting both columns is NOT a deal breaker.

                    • 7. Re: How can I maintain (1) column widths and (2) a header when replacing a table?
                      Laubender Adobe Community Professional & MVP

                      @mbrannon47 – I tested again and I also can see this issue. Could be a bug.
                      Maybe the second column should be also accessed explicitly.

                       

                      For my other issue with rowType:
                      It did not work, because I already had one header row assigned when I ran the script.

                       

                      Another limitation assigning rowType to header row is the case of a table with only one row.

                      That would also throw an error.

                       

                      So I rewrote that script. It would also assign a table style.

                      And if the style is not in the document create a new one with the name you are showing in one of your screen shots.

                      If you do not want this, just remove line 43 of the script.

                      You can also undo the whole formatting in one go:

                       

                      // 150910-1-FormatSelectedTable.jsx
                      // Uwe Laubender
                      
                      /**
                      * @@@BUILDINFO@@@ 150910-1-FormatSelectedTable.jsx !Version! Thu Sep 10 2015 06:39:11 GMT+0200
                      */
                      
                      
                      app.doScript
                          (
                         
                          formatSelectedTable,
                          ScriptLanguage.JAVASCRIPT,
                          [],
                          UndoModes.ENTIRE_SCRIPT,
                          "Format Selected Table | SCRIPT"
                         
                          );
                      
                      function formatSelectedTable(){
                      
                          var myTableStyleName = "Table Style 1";
                          var myColumnsWidth = "3 in";
                          var myHeaderRowCount = 1;
                         
                          if(app.selection.length != 1){return};
                      
                          var mySelection = app.selection[0];
                          var myTable = null;
                      
                          if(!app.documents[0].tableStyles.itemByName(myTableStyleName).isValid){
                              app.documents[0].tableStyles.add({name : myTableStyleName});
                              };
                      
                          if(mySelection.constructor.name == "Table"){myTable = mySelection};
                          if(mySelection.parent.constructor.name == "Table"){myTable = mySelection.parent};
                          if(mySelection.parent.parent.constructor.name == "Table"){myTable = mySelection.parent.parent};
                          if(myTable === null){
                              alert("Select a table, a cell or place your cursor in a cell, and try again.");
                              return;
                              };
                      
                          myTable.appliedTableStyle = myTableStyleName;
                         
                          for(var n=0;n<myTable.columns.length;n++){
                              myTable.columns[n].width = myColumnsWidth;
                              };
                      
                          for(var n=0; n<myHeaderRowCount;n++){
                              try{
                              myTable.rows[n].rowType = RowTypes.HEADER_ROW;
                              }catch(e){};
                              };
                      
                      };
                      

                       

                      Uwe

                      • 8. Re: How can I maintain (1) column widths and (2) a header when replacing a table?
                        mbrannon47 Level 1

                        Laubender, I can't thank you enough for your kindness, generosity, and insane InDesign skills. Your assistance is greatly appreciated. Not only did it work...it worked perfectly. Thank you again.