Skip navigation
integrafik syd
Currently Being Moderated

Merging empty cells until a filled one is found

Mar 21, 2010 7:20 PM

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

 
Replies
  • Currently Being Moderated
    Mar 22, 2010 8:06 AM   in reply to integrafik syd

    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.)

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 22, 2010 12:39 PM   in reply to integrafik syd

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 22, 2010 4:00 PM   in reply to TonyTuneson

    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]);
              }
         }
    }
    

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 8, 2012 8:36 AM   in reply to integrafik syd

    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!!!!

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 8, 2012 10:10 AM   in reply to traveldis2

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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points