12 Replies Latest reply on Sep 14, 2016 5:23 AM by tpk1982

    Deleting tab separated column content

    tpk1982 Level 4

      Hi,

       

      I am trying to remove content of a particular column in a table. But it is not working if i used the set up like below. The highlighted texts are not removed (under product). Not sure why. Please help to sort out.

       

      Screen Shot 2016-08-29 at 5.33.12 PM.png

       

      Tried coding:

       

      var para=app.selection[0];
      var myColumn=prompt("Enter column number to delete","");
      if(myColumn){
      var lines=para.contents.split('\r');
      var no_of_lines = lines.length;
      for(i = 1; i <= no_of_lines; i++)
      {
      var x = lines[i-1].split('\t');
      app.findGrepPreferences = null;     
      app.changeGrepPreferences = null; 
      app.findGrepPreferences.findWhat=x[myColumn-1];
      app.changeGrepPreferences.changeTo  ="";
      para.changeGrep();
      app.findGrepPreferences = null;     
      app.changeGrepPreferences = null; 
      }
      }
      

       

       

      Thanks,

      K

        • 1. Re: Deleting tab separated column content
          Obi-wan Kenobi Adobe Community Professional
          1 person found this helpful
          • 2. Re: Deleting tab separated column content
            tpk1982 Level 4

            Thank you Obi.

             

            But when i run the script it show object is invalid error in line 25

            • 3. Re: Deleting tab separated column content
              Kai Rübsamen Level 4

              If you use the grep-search, you must escpape '(' >> '\(', but since your findWhat is clear, you could also use the text search!

               

              Try this one:

               

              var curSel = app.selection[0];
              var myColumn = prompt("Enter column number to delete","");
              
              if (!myColumn) exit();
              
              var allParas = curSel.paragraphs.everyItem().getElements();
              app.findTextPreferences = app.changeTextPreferences = null;   
              
              for (var i = allParas.length-1; i >= 0; i--) {
                  var curPara = allParas[i];
                  var curParaContents = curPara.contents;
                  var x = curParaContents.split("\t");
                  app.findTextPreferences.findWhat = x[myColumn-1];
                  app.changeTextPreferences.changeTo  ="";  
                  curPara.changeText();    
              }
              
              app.findTextPreferences = app.changeTextPreferences = null;
              
              • 4. Re: Deleting tab separated column content
                tpk1982 Level 4

                Awesome.. thank you Kai

                • 5. Re: Deleting tab separated column content
                  tpk1982 Level 4

                  If i delete the last column, it deleted the return marks too.. may i know why?

                  • 6. Re: Deleting tab separated column content
                    Kai Rübsamen Level 4

                    You can test this in the estk with the console!

                     

                    Choose first 5 and then 6. If the return is included, the Pipe-symbol is in the next line

                     

                    var curParaContents = curPara.contents;
                    var x = curParaContents.split("\t");
                    var f = x[myColumn-1];
                    $.writeln(f + "  |");
                    
                    • 7. Re: Deleting tab separated column content
                      Obi-wan Kenobi Adobe Community Professional

                      Kaï,

                       

                      Cool! Nice code for me to be carefully studied // Thanks for my learning! 

                       

                      The script only deletes the contents! 

                      Would be cool to have a variant that entirely removes the column [as I want in my script (that doesn't work (?) // The grep code is OK!].

                       

                      (^/)

                      • 8. Re: Deleting tab separated column content
                        Kai Rübsamen Level 4

                        You can solve this problem, by deleting the return from the 'findWhat' string, e.g.

                         

                        app.doScript(main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "remove tab column");  
                        
                        function main() {
                        if (!(app.selection.length == 1 && app.selection[0].hasOwnProperty("baseline"))) return;
                        
                        var curSel = app.selection[0];
                        var myColumn = prompt("Enter column number to delete","");
                        
                        if (!myColumn) return;
                        
                        var allParas = curSel.paragraphs.everyItem().getElements();
                        app.findTextPreferences = app.changeTextPreferences = null;   
                        
                        for (var i = allParas.length-1; i >= 0; i--) {
                          var curPara = allParas[i];
                          var curParaContents = curPara.contents;
                          var x = curParaContents.split("\t");
                          var f = x[myColumn-1].replace("\r","");
                        
                          app.findTextPreferences.findWhat = f;
                          app.changeTextPreferences.changeTo  ="";  
                          curPara.changeText();
                        }
                        
                        app.findTextPreferences = app.changeTextPreferences = null;
                        }
                        

                         

                        Notice also, that line 18 will throw an error, if you e.g. have 6 columns and enter in the prompt-dialog 7 or if you input is not an integer. So you should check this as well. ;-)

                        1 person found this helpful
                        • 9. Re: Deleting tab separated column content
                          Obi-wan Kenobi Adobe Community Professional

                          Aha! Right! 

                           

                          I've found the error and corrected in other topic! 

                           

                          (^/)

                          • 10. Re: Deleting tab separated column content
                            tpk1982 Level 4

                            HI Kai,

                             

                            Sorry to reinitiate this thread. But found one small problem. It is working fine if i have all column and rows content. If some thins missing then it throws error.

                             

                            app.doScript(main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "remove tab co lumn");          function main() {  
                                if (!(app.selection.length == 1 && app.selection[0].hasOwnProperty("baseline"))) return;  
                                  
                                var curSel = app.selection[0];  
                                var myColumn = prompt("Enter column number to delete","");  
                                if (!myColumn) return;  
                                  if (isNaN(myColumn)) {
                                      alert("Please enter Number only");
                                      return;  
                                }
                            
                            
                                var allParas = curSel.paragraphs.everyItem().getElements();  
                                app.findTextPreferences = app.changeTextPreferences = null;     
                                  
                                for (var i = 0; i <allParas.length; i++) {  
                                  var curPara = allParas[i];  
                                  var curParaContents = curPara.contents;  
                                  var x = curParaContents.split("\t");  
                                if (myColumn>x.length){
                                      alert("Selected table contains only "+x.length+" columns"+". Enter only in between "+x.length);
                                      return;  
                                }  
                            
                            
                                  var f = x[myColumn-1].replace("\r","");  
                                  app.findTextPreferences.findWhat = f;  
                                  app.changeTextPreferences.changeTo  ="";    
                                  curPara.changeText();  
                                    
                            
                            
                                }  
                            alert("Column deleted successfully");
                            app.findTextPreferences = app.changeTextPreferences = null;  }
                            

                            Is it possible how to ignore this?

                             

                            Regards,

                            K

                            • 11. Re: Deleting tab separated column content
                              Kai Rübsamen Level 4

                              The problem is, that those columns are not really empty, because of you xml-dots. So the easiest way is, to use try/catch for the fc-operation:

                               

                              try {
                                  app.findTextPreferences.findWhat = f;  
                                  app.changeTextPreferences.changeTo  ="";    
                                  curPara.changeText();
                              }
                              catch (e) {};
                              
                              1 person found this helpful