Skip to main content
dublove
Legend
September 7, 2025
Question

How can I obtain the maximum width of text in a column? I will set the column's minimum width it.

  • September 7, 2025
  • 1 reply
  • 296 views

I want to release overflow text within cells.
The prerequisite is to identify columns containing overflow text, then set the minimum width for those columns.

 

Perhaps there's no way to directly release columns with overflow.
Do I need to set a possible cellwidth?

1 reply

m1b
Community Expert
Community Expert
September 7, 2025

Hi @dublove, try this script.

- Mark

 

/**
 * @file Expand Overflowing Cells.js
 * 
 * Expands any overset cells in the selected table.
 *
 * @author m1b
 * @version 2025-09-07
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-can-i-obtain-the-maximum-width-of-text-in-a-column-i-will-set-the-column-s-minimum-width-it/m-p/15493987
 */
function main() {

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    var doc = app.activeDocument;
    var tables = getTables(doc.selection);

    tableLoop:
    for (var t = 0; t < tables.length; t++) {

        var table = tables[t];

        cellLoop:
        for (var i = 0; i < table.cells.length; i++) {

            var cell = table.cells[i];

            if (!cell.overflows)
                continue cellLoop;

            var timeout = 5000;

            while (cell.overflows && timeout--) {
                cell.parentColumn.width += 1;
                cell.parentColumn.recompose();
            }

            if (!timeout) {
                alert('Could not expand cells.');
                continue tableLoop;
            }

        }
    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Expand Overflowing Cells');

/**
 * Get tables from object(s)
 * for Adobe Indesign
 * @author m1b
 * @version 2024-12-20
 *
 * Finds tables inside different objects,
 * eg. selection, page, text frame, story.
 * Supply a filter function to narrow down
 * the table(s) even more.
 *
 * @param {Document|Page|PageItem|TextFrame|Cell|Text} items - item, collection or array to get tables from
 * @param {Boolean} [onlyFirst] - only return the first table found
 * @param {Function} [filter] - only match tables that pass this filter function
 * @param {Array[Table]} [found] - results (private argument for recursive calls)
 * @returns {Array[Table]} - the found table(s) * if onlyFirst flag then can be {Table}
 */
function getTables(items, onlyFirst, filter, found) {

    // sanity
    if (items == undefined)
        return [];

    if (found == undefined)
        found = [];

    else if (
        onlyFirst
        && 'Table' === found.constructor.name
    )
        // immediately return the table
        return found;

    if (!items.hasOwnProperty('0'))
        // put in array to process
        items = [items];

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

        var item = items[i];

        if ('Table' === item.constructor.name) {

            if (
                filter == undefined
                || filter(item) === true
            ) {

                // item is a table
                if (onlyFirst)
                    return item;

                else
                    found.push(item);

            }
        }

        else if ('TextFrame' === item.constructor.name) {

            var textFrameTables = item.tables.everyItem().getElements();

            if (
                item.hasOwnProperty('textFrames')
                && item.textFrames.length > 0
            )
                // check in anchored text frames
                textFrameTables = textFrameTables.concat(getTables(item.textFrames, onlyFirst, filter));

            found = found.concat(textFrameTables);

        }

        else if (
            item.hasOwnProperty('tables')
            && item.tables.length > 0
        )
            // item contains tables
            found = found.concat(getTables(item.tables, onlyFirst, filter));

        else if (
            item.hasOwnProperty('parent')
            && 'Cell' === item.parent.constructor.name
        )
            // parent is a table cell
            found = found.concat(getTables(item.parent.parent, onlyFirst, filter));

        else if (
            item.hasOwnProperty('paragraphs')
            && item.paragraphs.length > 0
        )
            // is in a story, which may contain tables
            found = found.concat(getTables(item.paragraphs, onlyFirst, filter));

        else if (
            item.hasOwnProperty('parentTextFrames')
            && item.parentTextFrames.length > 0
        )
            // is in a story, which may contain tables
            found = found.concat(getTables(item.parentTextFrames, onlyFirst, filter));

        else if (
            item.hasOwnProperty('parentStory')
        )
            // is in a story, which may contain tables
            found = found.concat(getTables(item.parentStory, onlyFirst, filter));

        else if (
            item.hasOwnProperty('pageItems')
            && item.pageItems.length > 0
        )
            // contains page items, which may be tables
            found = found.concat(getTables(item.pageItems, onlyFirst, filter));

        else if (
            item.hasOwnProperty('parent')
            && 'Document' !== item.parent.constructor.name
            && 'Spread' !== item.parent.constructor.name
        )
            // has a parent which might be a table
            found = found.concat(getTables(item.parent, onlyFirst, filter));

    }

    if (
        onlyFirst
        && found.length > 0
    )
        return found[0]

    else
        return found;

};

 Edit 2025-09-07: replaced getTable with getTables function to allow collection of multiple tables.

dublove
dubloveAuthor
Legend
September 7, 2025

Hi @m1b .

Thank you very much.

When your selected textFrame contains two or more tables, "getTable(doc.selection[0])"  seems to only capture one table—it's not a collection.

 

Is the logic of this code to increment the width incrementally and then adjust each cell?
It doesn't seem particularly efficient.

 

Could we set a large initial value, adjust the column width to the longest content, and then check for overflow?
If overflow persists, set another value.
Adjusting column widths seems slower than adjusting each cell width individually.

 

Would modifying "cell.parentColumn.width += 100;" suffice?

It's too big; the width won't retract.

m1b
Community Expert
Community Expert
September 7, 2025

@dublove I have updated the above code to handle multiple tables in the selection.

 

You mention that "it doesn't seem particularly efficient". Yes, you are right. The algorithm was brute force, and could be slow with very large tables.

 

Here is a more efficient one that uses binary search to get to the minimum width in fewer steps. I will post as a separate code, because it is harder for you to learn from.

- Mark

 

Edit: removed code due to bad bug. Will try to fix and re-post when I get time.