Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Deleting tab separated column content

Guide ,
Aug 29, 2016 Aug 29, 2016

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

TOPICS
Scripting
995
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Aug 29, 2016 Aug 29, 2016

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;

    var curParaContents = curPara.contents;

    var x = c

...
Translate
LEGEND ,
Aug 29, 2016 Aug 29, 2016
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 29, 2016 Aug 29, 2016

Thank you Obi.

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 29, 2016 Aug 29, 2016

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;

    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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 29, 2016 Aug 29, 2016

Awesome.. thank you Kai

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 29, 2016 Aug 29, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 29, 2016 Aug 29, 2016

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 + "  |");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 29, 2016 Aug 29, 2016

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;

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 29, 2016 Aug 29, 2016

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

(^/)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 29, 2016 Aug 29, 2016

Aha! Right! 

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

(^/)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 14, 2016 Sep 14, 2016

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

      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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 14, 2016 Sep 14, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 14, 2016 Sep 14, 2016
LATEST

Screen Shot 2016-09-14 at 5.52.06 PM.png

Screen Shot 2016-09-14 at 5.52.19 PM.png

Screen Shot 2016-09-14 at 5.52.38 PM.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines