5 Replies Latest reply on Apr 7, 2015 2:45 AM by Jump_Over

    Script has issues with merged cells in tables

    KatBots

      Brand new to javascript so apologies in advance for the sloppy methodology. I'm going through a huge document trying to streamline the styles, and ran into a wall when it comes to the tables. In order to clear the stroke/fill formatting from specific tables (only defined by their internal paragraph styles) and apply my own, I need to override the current settings with a cell style. This is made more difficult by the fact that there's no standard number of rows or columns for the tables. I managed to cobble together something close to what I need, as given here:

       

      if ((table.rows.length>0) && (table.rows[1].cells[0].texts[0].appliedParagraphStyle.name === "CATALOGID"))
        {
             for (i=0; i<table.rows.length; i++)
             {
                  if (table.rows[i].cells[0].texts[0].appliedParagraphStyle.name === "Categories")
                  {
                       for (v=0; v<table.columns.length; v++) 
                       {          
                       table.rows[i].cells[v].appliedCellStyle = "Header";
                       table.rows[i].cells[v].clearCellStyleOverrides(true);
                       }
                  }
      
                  else if (table.rows[i].cells[0].texts[0].appliedParagraphStyle.name === "CATALOGID")
                       {
                       table.rows[i].cells[0].appliedCellStyle = "Catalog ID";
                       table.rows[i].cells[0].clearCellStyleOverrides(true);
                       for (j=1; j<table.rows[i].cells.length; j++) 
                            {        
                            table.rows[i].cells[j].appliedCellStyle = "Table Body";
                            table.rows[i].cells[j].clearCellStyleOverrides(true);
                            }
                       }
                  else {}  
              }
        }
      else{}
      

       

      It works fine up until it hits a merged cell, at which point it quits and won't go any further in the document. I'm guessing it's because of some issue in counting cell/column numbers consistently. I've tried a few workarounds by checking the rowSpan property but they just end up breaking the script worse. If someone could point me in the right direction for getting this thing working, even if it just ignores merged cells/any tables with merged cells, that'd be great.

        • 1. Re: Script has issues with merged cells in tables
          Jump_Over Level 5

          Hi,

           

          In the line #7 I would change table.columns.length to table.rows[i].cells.length

           

          Jarek

          • 2. Re: Script has issues with merged cells in tables
            KatBots Level 1

            Ah yeah, I missed that.

             

            I've changed line 7 in the script but it's still running into some exception that's stopping it. The error I get is "object is invalid" for

            "if ((table.rows.length>0) && (table.rows[1].cells[0].texts[0].appliedParagraphStyle.name === "CATALOGID"))"

             

            it's...very helpful. I assume this process would be a lot easier if there weren't so many conditionals for finding these types of tables.

            • 3. Re: Re: Script has issues with merged cells in tables
              Jump_Over Level 5

              Hi,

               

              Compare it to my code. I assumed mTable is the 1st table of 1st story of active document (modify it for your document)

               

              var
                mTable = app.activeDocument.stories[0].tables[0],
                mCat = app.activeDocument.paragraphStyles.item("Categories"),
                mID = app.activeDocument.paragraphStyles.item("CATALOGID");
              
              for (var k = 0; k < mTable.rows.length; k++)
                formatRow (mTable.rows[k]);
              
              function formatRow (mRow) {
                switch (mRow.cells[0].texts[0].appliedParagraphStyle) {
                     case mCat:
                          mRow.cells.everyItem().appliedCellStyle = app.activeDocument.cellStyles.item("header");
                          break;
                     case mID:
                          mRow.cells.everyItem().appliedCellStyle = app.activeDocument.cellStyles.item("Table Body");
                          mRow.cells[0].appliedCellStyle = app.activeDocument.cellStyles.item("Catalog ID");
                          break;
                     }
                mRow.cells.everyItem().clearCellStyleOverrides(true);
                }
              // so each processed cell is cleared with cellStyle overrides but
              // to clear paraStyle and charStyles overrides enable below line ;
              // mTable.cells.everyItem().texts.everyItem().clearOverrides(OverrideType.ALL);
              
              

               

              Jarek

              • 4. Re: Script has issues with merged cells in tables
                Poleroad
                Hi Jump_Over,
                This is a great script. Could I ask you to help a bit more to make it run through an entire document. I have hundreds of tables and wish I could remove all the overrides in one run. I added my own var but it is not good. I am an Java Script illiterate, but trying. Here what I came up with:

                //Weblink: https://forums.adobe.com/thread/1506935

                 

                var d = app.activeDocument;

                var i = 0

                var 

                      mTable = app.activeDocument.stories[0].tables[i],

                      mCat = app.activeDocument.paragraphStyles.item("Table Cell Text"), 

                      mID = app.activeDocument.paragraphStyles.item("CATALOGID"); 

                    for (var i = 0; i > app.activeDocument.stories[0].tables[i]; i++)

                    for (var k = 0; k < mTable.rows.length; k++) 

                      formatRow (mTable.rows[k]); 

                 

                function formatRow (mRow) { 

                      switch (mRow.cells[0].texts[0].appliedParagraphStyle) { 

                           case mCat: 

                                mRow.cells.everyItem().appliedCellStyle = app.activeDocument.cellStyles.item("HeaderCell"); 

                                break; 

                           case mID: 

                                mRow.cells.everyItem().appliedCellStyle = app.activeDocument.cellStyles.item("Cell Style"); 

                                mRow.cells[0].appliedCellStyle = app.activeDocument.cellStyles.item("Catalog ID"); 

                                break; 

                           } 

                      mRow.cells.everyItem().clearCellStyleOverrides(true); 

                     

                //     mTable.cells.everyItem().texts.everyItem().clearOverrides(OverrideType.ALL);

                      } 

                    // so each processed cell is cleared with cellStyle overrides but 

                    // to clear paraStyle and charStyles overrides enable below line ; 

                    // mTable.cells.everyItem().texts.everyItem().clearOverrides(OverrideType.ALL); 

                 

                Martin

                • 5. Re: Script has issues with merged cells in tables
                  Jump_Over Level 5

                  Hi,

                   

                  You need to call function formatRow() for each tabel.row found in your doc.

                   

                  Something like:

                  var
                      mRows = app.activeDocument.stories.everyItem().tables.everyItem().rows.everyItem().getElements(),
                      cRow;
                  while ( cRow = mRows.pop() )
                      formatRow(cRow);
                  
                  function formatRow(mRow) {
                      //....
                      }
                  

                   

                  Jarek