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

ID script : populate empty cells

Explorer ,
Jul 12, 2021 Jul 12, 2021

Hi there

I'm working with EasyCatalog. And i'm after a little script which could populate all my empty cells (dozens of tables in my ID document) with a "-".

I saw this threat : https://community.adobe.com/t5/indesign/assign-cell-style-to-empty-table-cells-clear-cell-style-over... which is quite similar

 

A little help would be very usefull because i just don't know how to script it 🙂

Thank you very much

Best regards

 

Miran

TOPICS
Scripting
962
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

Community Expert , Jul 12, 2021 Jul 12, 2021

var myDocument = app.documents.item(0); 

var allCells = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

for ( var i = 0; i < allCells.length; i++)

{

    if ( allCells[i].contents === '')

    {

        allCells[i].contents = '-';

     }

}

 

 

Translate
Community Expert ,
Jul 12, 2021 Jul 12, 2021

var myDocument = app.documents.item(0); 

var allCells = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

for ( var i = 0; i < allCells.length; i++)

{

    if ( allCells[i].contents === '')

    {

        allCells[i].contents = '-';

     }

}

 

 

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
Explorer ,
Jul 12, 2021 Jul 12, 2021

Hi brianp311

I thank you very much! That's it! It works like a charm

 

Is there a possibility to try the opposite thing : find columns with only "-" in it and delete them...

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
Community Expert ,
Jul 12, 2021 Jul 12, 2021

Sure. But even though you know nothing of scripting, take a look at the code and think about how you might reverse it... 

var myDocument = app.documents.item(0); 
var allCells = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
for ( var i = 0; i < allCells.length; i++)
{
    if ( allCells[i].contents === '' )
    {
        allCells[i].contents = '-';
    }
    else if ( allCells[i].contents === '-' ) { 
        allCells[i].contents = '';
    }
}
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
Explorer ,
Jul 12, 2021 Jul 12, 2021

So i guess that the beginning is not so difficult :

var myDocument = app.documents.item(0); 
var allCells = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
for ( var i = 0; i < allCells.length; i++)
{
    if ( allCells[i].contents === '-' )
    {

 

but after that how could i ask ID to delete every column which contains cells with only "-"

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
Community Expert ,
Jul 12, 2021 Jul 12, 2021

Oh I see, I thought you just wanted to convert cells with "-" to "" (the solution of which I posted). What you're after is a bit tougher. Try this, though you may run into issues if you have merged cells in your tables.

 

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

 

 

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
Explorer ,
Jul 12, 2021 Jul 12, 2021
LATEST

Goodmorning Brian p311

Oh thank you very much!

It works very 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