Delete all columns containg only...

New Here ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Hi there

I asked a question yesterday and Brianp311 replied me in a right way.

My question was : how could i delete all columns containing only a "-".

The right script that Brianp311 gave me is :

var myDocument = app.documents.item(0); 
var allCols = myDocument.stories.everyItem().tables.everyItem().columns.everyItem().getElements();
var allCells, isAllHyphens;
for ( var i = allCols.length-1; i >= 0; i--)
{
    isAllHyphens = true;
    allCells = allCols[i].cells.everyItem().getElements();
    for (var j = 0; j < allCells.length; j++) {
        if (allCells[j].contents !== "-") { 
            isAllHyphens = false; 
            break; 
        } 
    }
    if (isAllHyphens) { allCols[i].remove(); } 
}

If tested with a simple, manually put "-", it works very well.

My problem is that "-" is inserted automatically by a data merge system (EasyCatalog), with bracket markers. So the script can't find it because it appears like that the joint screencapture.

 

Even if i select it directly, Id gives me a simple "-"...

Thank you for your precious help

 

Regards

Miran

TOPICS
How to, Scripting

Views

118

Likes

Translate

Translate

Report

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

Adobe Community Professional , Jul 13, 2021 Jul 13, 2021
Hi @lemiran, Give the following code a try var myDocument = app.documents.item(0); var allCols = myDocument.stories.everyItem().tables.everyItem().columns.everyItem().getElements(); for ( var i = allCols.length-1; i >= 0; i--) { var hrc = allCols[i].parent.headerRowCount var con = allCols[i].contents.slice(hrc) var a = con.toString().replace(/[,\uFEFF\s]/g,"") var regex = /^(-)+$/; if(regex.test(a)) allCols[i].remove() } If you still have other use cases then as Uwe suggested it would...

Likes

Translate

Translate
Adobe Community Professional ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Hi Miran,

Can you share a sample document with this character?

-Manan

Likes

Translate

Translate

Report

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
New Here ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Hi Manan Joshi

Thank you for your quick reply.

Yes here it is. But i'm not sure the markers will stay if you don't have EasyCatalog plugin or free EasyCatalog reader plugin (https://www.65bit.com/download/reg/#easycatalog-reader)

Likes

Translate

Translate

Report

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 ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Can you please try this? I've added a regular expression to match the weird invisible characters.

 

var myDocument = app.documents.item(0); 
var allCols = myDocument.stories.everyItem().tables.everyItem().columns.everyItem().getElements();
var allCells, isAllHyphens;
for ( var i = allCols.length-1; i >= 0; i--)
{
    isAllHyphens = true;
    allCells = allCols[i].cells.everyItem().getElements();
    var regex = /^\uFEFF\uFEFF-\uFEFF$/;
    for (var j = 0; j < allCells.length; j++) {
        if (regex.test(allCells[j].contents)) { 
            isAllHyphens = false; 
            break; 
        } 
    }
    if (isAllHyphens) { allCols[i].remove(); } 
}

 

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

If the code provided by @m1b does not work, try replacing the following line

 

var regex = /^\uFEFF\uFEFF-\uFEFF$/;

 

with this line

 

var regex = /\s*-\s*/;

 

-Manan

Likes

Translate

Translate

Report

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 ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Thanks Manan! It's weird though... When I run this code on the sample file

var _table = app.activeDocument.stories.everyItem().tables.everyItem().getElements()[0];
var _cell = _table.cells[0];
var str = _cell.contents

var regex1 = /^\uFEFF\uFEFF-\uFEFF$/;
$.writeln(regex1.test(str));  // true

var regex2 = /^\s\s-\s$/;
$.writeln(regex2.test(str));  // false

 

But when I paste that working regex into the Miran's code it fails, and your regex works. It seems I'm missing something! 🙂 

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Hi @m1b,

Try this version of the code, this works for me both for cases where we have whitespaces around - and also FEFF

var myDocument = app.documents.item(0); 
var allCols = myDocument.stories.everyItem().tables.everyItem().columns.everyItem().getElements();
for ( var i = allCols.length-1; i >= 0; i--)
{
	var a = allCols[i].contents.toString().replace(/[,\uFEFF\s]/g,"")
    var regex = /^(-)+$/;
	if(regex.test(a))
		allCols[i].remove()
}

-Manan

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

@m1b in your version of the code, it seems you got the if condition inverted. The following works

var myDocument = app.documents.item(0); 
var allCols = myDocument.stories.everyItem().tables.everyItem().columns.everyItem().getElements();
var allCells, isAllHyphens;
for ( var i = allCols.length-1; i >= 0; i--)
{
    isAllHyphens = true;
    allCells = allCols[i].cells.everyItem().getElements();
    var regex = /^\uFEFF*-\uFEFF*$/;
    for (var j = 0; j < allCells.length; j++) {
        if (!regex.test(allCells[j].contents)) { 
            isAllHyphens = false; 
            break; 
        } 
    }
    if (isAllHyphens) { allCols[i].remove(); } 
}

-Manan

Likes

Translate

Translate

Report

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 ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Thanks Manan. You are right!

Likes

Translate

Translate

Report

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
New Here ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Hi Manan and M1b and thanks a lot for your searchs

If i try this :

var myDocument = app.documents.item(0); 
var allCols = myDocument.stories.everyItem().tables.everyItem().columns.everyItem().getElements();
var allCells, isAllHyphens;
for ( var i = allCols.length-1; i >= 0; i--)
{
    isAllHyphens = true;
    allCells = allCols[i].cells.everyItem().getElements();
    var regex = /^\uFEFF\uFEFF-\uFEFF$/;
    for (var j = 0; j < allCells.length; j++) {
        if (regex.test(allCells[j].contents)) { 
            isAllHyphens = false; 
            break; 
        } 
    }
    if (isAllHyphens) { allCols[i].remove(); } 
}

it deletes me every columns which DO NOT contain "-" :))

It's almost OK!

If i try this code:

var myDocument = app.documents.item(0); 
var allCols = myDocument.stories.everyItem().tables.everyItem().columns.everyItem().getElements();
for ( var i = allCols.length-1; i >= 0; i--)
{
	var a = allCols[i].contents.toString().replace(/[,\uFEFF\s]/g,"")
    var regex = /^(-)+$/;
	if(regex.test(a))
		allCols[i].remove()
}

 

I have no result.

Likes

Translate

Translate

Report

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
New Here ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

I know why it doesn't work for me, because i have a header on my table.

I i try the code on a table without header it works perfectly... But in my case, i always have headers 🙂

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Hi @lemiran,

Give the following code a try

var myDocument = app.documents.item(0); 
var allCols = myDocument.stories.everyItem().tables.everyItem().columns.everyItem().getElements();
for ( var i = allCols.length-1; i >= 0; i--)
{
	var hrc = allCols[i].parent.headerRowCount
	var con = allCols[i].contents.slice(hrc)
	var a = con.toString().replace(/[,\uFEFF\s]/g,"")
    var regex = /^(-)+$/;
	if(regex.test(a))
		allCols[i].remove()
}

If you still have other use cases then as Uwe suggested it would be better if you hire someone to polish the code for you.

-Manan

Likes

Translate

Translate

Report

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
New Here ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

LATEST

Hi Manan

I thank you very much for your precious help.

Tell me how i can pay you and ask for other jobs in the future

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Hi together,

Manan's script is working very well, just tested this with Miran's document, EasyCatalog Reader plug-in installed for InDesign 16.2.1 on Windows 10. Also m1b's script, the one corrected by Manan, is working as expected:

https://community.adobe.com/t5/indesign/delete-all-columns-containg-only/m-p/12173752#M436857

 

However, m1b's approach is more stringent, because every single cell is tested individually.

With Manan's own version also empty text cells will be handled as if they contain a MINUS HYPHEN character.

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

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
New Here ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Hi Laubender

I just tried both of the codes on my file but didn't succeed to get them work. I will retry if you say that's ok...

 

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Hi Miran,

you provided a test InDesign document without any header or footer rows.

Also without graphic cells. Or other things we cannot know.

 

I suggest you make a paid job out of this and engage Manan or m1b.

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

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
New Here ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

hi again Laubender

You are right i totally didn't think it would be helpful to provide a complete file. Mea culpa 😕

And you are right, it is very important for me to have this script, so it would be normal if Manan or m1b want to function as a paid job.

Thank you for your help

Likes

Translate

Translate

Report

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