Copy link to clipboard
Copied
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.
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
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
Copy link to clipboard
Copied
See: Remove a Column in Tab Text …
(^/)
Copy link to clipboard
Copied
Thank you Obi.
But when i run the script it show object is invalid error in line 25
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
Awesome.. thank you Kai
Copy link to clipboard
Copied
If i delete the last column, it deleted the return marks too.. may i know why?
Copy link to clipboard
Copied
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 + " |");
Copy link to clipboard
Copied
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. 😉
Copy link to clipboard
Copied
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!].
(^/)
Copy link to clipboard
Copied
Aha! Right!
I've found the error and corrected in other topic!
(^/)
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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) {};
Copy link to clipboard
Copied