Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Can you help me adapt this table script?

Tagged: , ,

Return to Member Forum

  • Author
    Posts
    • #113588

      I found this script, which is very close to what I need. But instead of it merging the cells with the one above I need it to merge with the cell to left of any empty cell.

      I have tried to experiment with it and manged to get it to merge horizontally once but it merge three cells that weren’t empty into one.

      Would be greatful for any help

      Thanks

      var myDoc = app.activeDocument;

      myPage = myDoc.pages;

      for (var p=0; myPage.length>p; p++){

      try{

      var myTable = myPage[p].textFrames.everyItem().tables.everyItem();

      if (myTable.constructor.name == ‘Table’){

      for (var t = myTable.cells.length – 1; t >= 0; t–)
      {

      if (myTable.cells[t].contents==””){
      var w = myTable.columns.length;
      myTable.cells[t-w].merge(myTable.cells[t]);

      }

      }

      }

      }

      catch(e){}

      }

    • #113667
      Peter Kahrel
      Participant

      It’s not so clear what’s going wrong. And that script is rather inefficient — here is another approach:

      var i, j, cells;
      // Get all the rows in the document
      var rows = app.documents[0].stories.everyItem().tables.everyItem().rows.everyItem().getElements();
      for (i = 0; i < rows.length; i++) {
      	// Get all the cells in a row
      	cells = rows[i].cells.everyItem().getElements();
      	for (j = cells.length-1; j >= 1; j--) {
      		if (cells[j-1].contents == '') {
      			cells[j-1].merge (cells[j]);
      		}
      	}
      }

      P.

      • #113674

        Hi Peter,

        Thanks for taking the time and looking at my question.

        What I need though is for the cells to merge to the left if the cell to it’s right is empty – such as…

        __________________________
        | “Some text”|”” |
        __________________________

        |”Some text” |”Some text”|
        __________________________

        would become…

        __________________________
        | “Some text” |
        __________________________

        |”Some text” |”Some text”|
        __________________________

        Thanks again

        Rob

    • #113675
      Peter Kahrel
      Participant

      Missed that… Change this line:

      if (cells[j-1].contents == '')

      to this:

      if (cells[j].contents == '')

      P.

      • #113679

        Bravo – thank you!

        I must have changed every variable other than that one.

        Thanks again

        PM

Viewing 2 reply threads
  • You must be logged in to reply to this topic.
Forum Ads