Skip to main content
Inspiring
April 5, 2015
Answered

How to make a script for expand column width

  • April 5, 2015
  • 2 replies
  • 1026 views

Hi experts,

Is that possible to make a script for expand the column width aim to let the columns show up all the overset text?

Regard

John

This topic has been closed for replies.
Correct answer BSKTCreation

Hi John,

Sorry about that, try this, it'll count through all the tables in your document.

myTables = app.documents[0].stories.everyItem().tables.everyItem().getElements();

for (var t = 0; t < myTables.length; t++) {

    BE_resizeColumnsToFitContents(myTables);

    }

function BE_resizeColumnsToFitContents(tableToEdit) {

    for (var i = 0; i < tableToEdit.columns.length; i++) {

        while (tableToEdit.columns.overflows === true) {

            tableToEdit.columns.width += 1;

            tableToEdit.columns.recompose();

            }

        }

    }

Brett

2 replies

JohnwhiteAuthor
Inspiring
April 5, 2015

thank you so much!

Inspiring
April 5, 2015

Hi John,

As Uwe advised it would be wise to put a stop on any while loop in case the condition is never fulfilled which would mean the script will break.

Without knowing what specifically you are working on you could make the below amendments to do this.

myTables = app.documents[0].stories.everyItem().tables.everyItem().getElements();

for (var t = 0; t < myTables.length; t++) {

    BE_resizeColumnsToFitContents(myTables, 200);

    }

function BE_resizeColumnsToFitContents(tableToEdit, tableMaxWidth) {

    for (var i = 0; i < tableToEdit.columns.length; i++) {

        while (tableToEdit.columns.overflows === true) {

            if (tableToEdit.width < tableMaxWidth) {

                tableToEdit.columns.width += 1;

                tableToEdit.columns.recompose();

                }

            else {

                alert("Column " + i + " contents too large for column.");

                break;

                }

            }

        }

    }

I won't put any extra functions in this because it might not be what you're after. To use this you just define the table width as a second argument to the function (but you could change this parameter to something else, like page, column or cell width). And, if you want to have a fail action you just put it in the 'else' part.

Brett

Inspiring
April 7, 2015

Is this possible to decrease the  column width and  based on the text

Inspiring
April 5, 2015

Yes you can.

You use something like...

BE_resizeColumnsToFitContents(app.activeDocument.stories[0].tables[0]);

function BE_resizeColumnsToFitContents(tableToEdit) {

    for (var i = 0; i < tableToEdit.columns.length; i++) {

        while (tableToEdit.columns.overflows === true) {

            tableToEdit.columns.width += 1;

            tableToEdit.columns.recompose();

            }

        }

    }

Brett

JohnwhiteAuthor
Inspiring
April 5, 2015

Hi Brett

Thank you for your help.

But it seems nothing response to me.

even I changed it to this:

BE_resizeColumnsToFitContents(app.activeDocument.stories.everyItem().tables.everyItem()); 

function BE_resizeColumnsToFitContents(tableToEdit) { 

    for (var i = 0; i < tableToEdit.columns.length; i++) { 

        while (tableToEdit.columns.overflows === true) { 

            tableToEdit.columns.width += 1; 

            tableToEdit.columns.recompose(); 

            } 

        } 

    }

still nothing.

Regard

John

BSKTCreationCorrect answer
Inspiring
April 5, 2015

Hi John,

Sorry about that, try this, it'll count through all the tables in your document.

myTables = app.documents[0].stories.everyItem().tables.everyItem().getElements();

for (var t = 0; t < myTables.length; t++) {

    BE_resizeColumnsToFitContents(myTables);

    }

function BE_resizeColumnsToFitContents(tableToEdit) {

    for (var i = 0; i < tableToEdit.columns.length; i++) {

        while (tableToEdit.columns.overflows === true) {

            tableToEdit.columns.width += 1;

            tableToEdit.columns.recompose();

            }

        }

    }

Brett