11 Replies Latest reply on Apr 5, 2015 2:43 AM by Kai Rübsamen

    Script remove empty columns

    Johnwhite Level 1

      Hi experts

       

      I got this script, for remove the empty columns in tables

      but the function is in active every table,

      and I wan to change it to only work on selection[0]

      Could someone tell me how can do that?

       

      Regard

      John

       

       

       

       

       

       

      myDoc = app.activeDocument;
      findTable();

      function findTable(){
      for ( s = myDoc.stories.length-1; s >= 0; s-- ){
        for ( t = myDoc.stories[s].tables.length-1; t >= 0; t-- ){
         myTable = myDoc.stories[s].tables[t];
         removeCurrentColumn();
        }
      }
      }

      function removeCurrentColumn(){
      for ( i = myTable.columns.length-1; i >= 0; i-- ){
         if (currentColumnIsEmpty(i) == true){
         myTable.columns[i].remove();
        }
      }
      }

      function currentColumnIsEmpty(col){
          theColumnIsEmpty = true;
      for ( k = myTable.rows.length-1; k >= 0; k-- ){
        if (myTable.cells.item(col + ":" + k).texts[0].contents != ""){
         theColumnIsEmpty = false;
        }
      }
        return theColumnIsEmpty;
      }

      function findTableDel(){
      if (myDoc.stories.everyItem().tables.length > 0) {
        myAllTables = myDoc.stories.everyItem().tables.everyItem();
        for ( s = myAllTables.length-1; s >= 0; s-- ){
         myTable = myAllTables.item[s];
         removeCurrentColumn(myAllTables[s]);
        }
      }
      }

        • 1. Re: Script remove empty columns
          csm_phil Level 4

          Hi Johnwhite,

           

          I just modified your js code. First, select the table then run the script.

           

          myTable = app.selection[0].parent.tables[0];
          removeCurrentColumn();
          function removeCurrentColumn(){
              for ( i = myTable.columns.length-1; i >= 0; i-- ){
                  if (currentColumnIsEmpty(i) == true){
                      myTable.columns[i].remove();
                      }
                  }
              }
          
          function currentColumnIsEmpty(col){
              theColumnIsEmpty = true;
              for ( k = myTable.rows.length-1; k >= 0; k-- ){
                  if (myTable.cells.item(col + ":" + k).texts[0].contents != ""){
                      theColumnIsEmpty = false;
                      }
                  }
              return theColumnIsEmpty;
              }
          
          

           

          thx,

          csm_phil

          • 2. Re: Script remove empty columns
            tanujad54435002 Level 1

            If we select the table column then the selection parent would be "Object Table", hence if we want to select a table column and want to delete it then the correct code should be

            myTable = app.selection[0].parent

            instead of

            myTable = app.selection[0].parent.tables[0]; 

            • 3. Re: Script remove empty columns
              Laubender Adobe Community Professional & MVP

              Yes, and if we selected a table, then app.selection[0].parent.tables[0] will give us the first table of the story where our table is sitting in.

              Even if we selected the second table in that story. So we really should know and check what is selected, if we procede with the myTable variable.

               

              Uwe

              • 4. Re: Script remove empty columns
                Kai Rübsamen Adobe Community Professional

                Hi Guys!

                So we really should know and check what is selected …

                Why? Give the user an alert, if his selection is wrong. Done ;-)

                 

                I would do it in this way:

                If nothing is selected, the user get an alert and all cols in the doc will be removed. If the cursor is an insertionPoint in a cell, only this table is honored. If the user select something else, he will get an alert.

                 

                var curDoc = app.documents[0];
                
                if (app.selection.length == 0) {
                    var res = confirm("Warning:\rRemove empty columns from the whole document?\r\rOtherwise put your cursor in a special table cell and start the script again!");
                    if (res) {
                        var colsToChange = curDoc.stories.everyItem().tables.everyItem().columns.everyItem().getElements();    
                    }
                    else {
                        exit();
                    }
                }
                else {
                    if (app.selection[0].parent.constructor.name != "Cell") {
                        alert("He wrong selection!\rPut your cursor in a cell.")
                        exit();
                    }
                    else {
                        var colsToChange = app.selection[0].parent.parent.columns;    
                    }
                }
                
                for (var i = colsToChange.length-1; i >= 0; i--) {
                  var curCol = colsToChange[i];
                  if (findEmptyCells(i) == true) {
                    curCol.remove();
                  }
                }
                
                function findEmptyCells(col){  
                  var emptyCol = true;
                  for (var j = 0; j < curCol.cells.length; j++) {
                      var curCell = curCol.cells[j];
                      if (curCell.contents != "") {
                        emptyCol = false;
                        break;
                      }
                    }
                    return emptyCol;  
                }
                

                 

                –Kai

                • 5. Re: Script remove empty columns
                  Johnwhite Level 1

                  Hi thank you all

                   

                  csm_phil's idea only work on first, in second time, it will do zero task.

                   

                  Kai's idea is great, but only work on one single table.

                   

                  Is that script call work on selection but no matter how many times?

                   

                  and I thank, script need to split merged cells first.

                   

                  Regard

                  John

                  • 6. Re: Script remove empty columns
                    Kai Rübsamen Adobe Community Professional

                    John, how do you want to select multiple tables? Selecting a textframe?

                    • 7. Re: Script remove empty columns
                      Johnwhite Level 1

                      Hi Kai

                      Yes!

                      The text in text frame, but with sometables, but not all the table need to remove empty column.

                       

                      Regard

                      John

                      • 8. Re: Script remove empty columns
                        [Jongware] Most Valuable Participant

                        Kai: (veering off-topic) Today I was experimenting with a table script and wanted to test it on several different tables. Bored of having to click 'in' a table, select the text around it and even selecting its frame, I went for something that worked out surprisingly user friendly: activePage! A script knows at which page you are looking, and then you can test if there is at least one table visible.

                        • 9. Re: Script remove empty columns
                          Kai Rübsamen Adobe Community Professional

                          Theunis: This is great! Thanks

                          • 10. Re: Script remove empty columns
                            Johnwhite Level 1

                            Thank you

                             

                             

                             

                            • 11. Re: Script remove empty columns
                              Kai Rübsamen Adobe Community Professional

                              That make no sense. So it is better to not delete those columns and correct them manually

                               

                              Change this line

                              if (curCell.contents != "") {

                               

                              to

                              if (curCell.texts[0].contents != "") {

                               

                              Does that help?