9 Replies Latest reply on Dec 1, 2014 2:12 PM by myDavey

    Dealing with merged cells in a table

    myDavey Level 1

      Hello

      I've been using tables pretty often in InDesign and I've constantly run into the same problem with scripting - dealing with merged cells

      In many rows, I have a few cells that are merged horizontally

      As I go through the cells in my selection, I want to test the row with the contents of the first cell in that row - I.e., the row key

       

      In short, this is what I want to do:

      1. Select cells (manually)

      2. Run the script which will check each cell in the selection to test if it is in a row that starts with "TOTAL" - and then, act accordingly

       

      My problem is that the results are never turning out correct since some rows have merged cells

      In turn, column 1 might have 50 rows, but column 2 can have only 48 rows (because it has 2 merges)

      Therefore, when I check the contents of the first row as such:

      cells[1].parentRow.cells[0].contents
      

       

      it doesnt come out a match - even if it is in the same row.

       

       

      What is the best way to get around this issue?

      If anyone can give some help on this matter, I would greatly appreicate it

       

      Thanks

      Davey

        • 1. Re: Dealing with merged cells in a table
          [Jongware] Most Valuable Participant

          Can you add an image of a table + selection where this problem shows up? I'm having a hard time picturing it.

          • 2. Re: Dealing with merged cells in a table
            romanobstuder Level 1

            Hi Davey

            Would it be possible to (maybe only temporary) un-merge every cell in the table.

            You could then run the content of every cell into an 2 dimensional array. Now you got all data in a "clean" spot.

            From here you do you further script.

            If this would work for you, I think I got some script fragments where I did lots of cell operations...

            let me know

            Regards

            Romano

            • 3. Re: Dealing with merged cells in a table
              myDavey Level 1

              Hi guys

              Thanks for your replies

               

              @ Jongware

              I am going to attach a screenshot shortly

               

              @Romano

              I though of that, but I wasnt sure how to 'remember' where they were to remerge them later on

              Do you have any ideas?

               

               

              Thanks

              Davey

              • 4. Re: Dealing with merged cells in a table
                myDavey Level 1

                @ Jongware

                 

                I ran a script on my table to insert values into the cells so we can see the issues

                Here is a screen shot depicting the problems (I indicated them with red boxes)

                screenshot_307.png

                 

                Example of the problem:

                Look in row A7

                Cell A is indexed at 7

                Cells B - O are indexed at 6

                Cells P - R are indexed at 5!

                 

                Reason being:

                Row 0 has only 1 cell since they are all merged into the rightmost cell.

                Therefore, column B starts 0 in row 2 (instead of row A)

                Columns P-R start 0 in row 3 since in row 2 it is merged into cell O0

                 

                 

                Again, if anyone can offer some help and advice on this topic, it would be very much appreciated

                Thanks

                Davey

                • 5. Re: Dealing with merged cells in a table
                  romanobstuder Level 1

                  Hi Davey

                  I just had a look at the code I had in mind. I coded this longer ago then I had in mind -)

                  I started with an un-merged table and merged cells if they were empty in order for the content to use as little vertical space as possible.

                   

                  The source of all my tables came from Excel and we used XML to bring some formatting along.

                  Trouble stared when the client wanted to update the content. If the structure of a table stays, that is the same number of rows and cols all is well.

                  But if additional rows or cols are coming through. All formatting goes out...

                   

                  I would potentially duplicate the table, un-merge all cells, maybe still run the data into an Array and go from there.

                  After that you kill the douplicated table.

                  BTW: I keept the "clean" Array in a script label attached to the table. So I had a quick way back to the array (cell by cell data) in later operations.

                   

                  Regards

                  Romano

                  • 6. Re: Dealing with merged cells in a table
                    myDavey Level 1

                    Hi Romano

                    That does cause problems....!

                     

                    I dont understand how your suggestion will help

                    I'll explain - if I unmerge all cells, it will result that in the new table all columns will start at the top which is 0

                    Hence, when I am getting the data for column B, cell B0 will be empty since it will be on the first row (row 0) - cell B1 will have the contents "B0"

                    When I go back to the original chart and try to insert the data for B0 into cell B1 (as displayed in the screenshot above) - it wont come out there, rather in the next row

                    • 7. Re: Dealing with merged cells in a table
                      romanobstuder Level 1

                      I see you problem. Sorry leading you down this path...

                      I understand you need to "remember" where the cells are merged to write your results back into the table.

                      Regards

                      Romano

                      • 8. Re: Dealing with merged cells in a table
                        cchimi Level 2

                        Why do you need to know the row indexes? Isn't parentRow getting you to the correct row? I've tried to recreate this but I can't see the problem. Maybe you can post a little more of your code so it's clearer where exactly the mismatch is.

                        • 9. Re: Dealing with merged cells in a table
                          myDavey Level 1

                          Hi cchimi

                          I tried that and it didnt work - it returned the wrong row......

                          But now that you mentioned it.... I tried it again and IT DID WORK!

                          I dont know what happened last time - but now its working

                           

                          Sorry for the inconvenience

                          Thanks for the help

                           

                          Davey