7 Replies Latest reply on Aug 8, 2012 10:10 AM by Indesigners_es

    Merging empty cells until a filled one is found

    integrafik syd

      Hi there

       

      I`m new to scripting, and but i can figure there`s a way of doing this.

       

      A lot`s of table need to have rows with merged cells.

      If the first cell contains text, then the other 5 don`t, then another cell with text and so on.

      I need to test how many cells are empty after the filled one, so i can merge them, then move to the next and do the same!

       

      Any help please,

       

      thank you

        • 1. Re: Merging empty cells until a filled one is found
          [Jongware] Most Valuable Participant

          Well, what have you got so far? Testing on cell[x].contents.length ought to be enough to test whether it 's empty or not. If so, merge with the left one and continue with the one to the right. If not, continue with the one to the right. (The pattern here is obvious.)

          • 2. Re: Merging empty cells until a filled one is found
            integrafik syd Level 1

            What i need is to test from the first cell, witch is filled by default, to the cell in front of the next filled one, then merge all those cells.

            After that, from the second to the next filled and so on.

            • 3. Re: Merging empty cells until a filled one is found
              TonyTuneson

              Didn't test it, but I guess (in VB) something like:

               

              For R = 1 to myTable.Rows.Count

                   For C = myRow.Cells.Count to 2 Step -1

                        If myRow.Cells.item(C).Contents = "" then

                             myRow.Cells.item(C-1).Merge myRow.Cells.item(C)

                        End If

                   Next C

              next R

               

              hope it helps

              • 4. Re: Merging empty cells until a filled one is found
                [Jongware] Most Valuable Participant

                No need to work backwards here

                 

                What I said:

                Going left to right over a row,

                    if you encounter a cell with contents,

                      move right while seeing cells with contents.

                    If that cell is empty,

                      merge with the previous cell (which may or may already be merged, we don't care);

                      repeat until coming to a cell with contents.

                Repeat until you come to the end of the row.

                 

                It looks like this in Javascript (you didn't mention your language preference, so this is what you are getting):

                 

                table = app.selection[0];
                if (!(table instanceof Table)) table = table.parent;
                if (table instanceof Cell) table = table.parent;
                if (table instanceof Table)
                {
                     for (r=0; r<table.rows.length; r++)
                     {
                          c = 0;
                          while (c < table.rows[r].cells.length)
                          {
                               while (c < table.rows[r].cells.length &&
                                    table.rows[r].cells[c].contents.length > 0)
                                    c++;
                               while (c < table.rows[r].cells.length &&
                                    table.rows[r].cells[c].contents.length == 0)
                                    table.rows[r].cells[c].merge (table.rows[r].cells[c-1]);
                          }
                     }
                }
                

                • 5. Re: Merging empty cells until a filled one is found
                  integrafik syd Level 1

                  Finally made it:

                   

                  for (var i = 0; i < myTable.rows[0].cells.length; i++) {
                    for (var j = i; j < myTable.rows[0].cells.length-1; j++) {
                      if (myTable.rows[0].cells[j+1].contents != "" || myTable.rows[0].cells[j+1].overflows) {
                        break;
                      }
                    }
                    if (i < j) {
                      myTable.rows[0].cells[i].merge(myTable.rows[0].cells[j]);
                    }
                  }

                   

                   

                  not another question comes: is there a way to split a table with 20 rows in 4 tables let`s say 5 rows each?


                  javascripting, of course!

                   

                  Thank everyone for the clues!

                  • 6. Re: Merging empty cells until a filled one is found
                    Indesigners_es

                    Hi, I want to use this script to merge all the empty cells, vertical and horizontal, but It's not working. Can you help me? Thanks!!!!

                    • 7. Re: Merging empty cells until a filled one is found
                      Indesigners_es Level 1

                      The error happens when  the first cell is empty! Is there a way to fix it? Thanks