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

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

Guide ,
Sep 06, 2025 Sep 06, 2025

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?

TOPICS
How to , Scripting
202
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 ,
Sep 07, 2025 Sep 07, 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.

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
Guide ,
Sep 07, 2025 Sep 07, 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.

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 ,
Sep 07, 2025 Sep 07, 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. 

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
Guide ,
Sep 07, 2025 Sep 07, 2025

Hi @m1b 

Expand Overflowing Cells.js works perfectly.

Thank you very much.
Expand Overflowing Cells 2.js seems slower—the screen flickers every time a cell is released.

 

 

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 ,
Sep 07, 2025 Sep 07, 2025

Interesting! Perhaps because it starts maximum width and reduces width until it reaches the optimum width. The old algorithm just adds 1 pt again and again until the overflow is solved.

 

Try reducing the 5000 (pts, approx 1.76 metres!) in this line

const WIDEST_COLUMN = 5000;

to a more realistic maximum, maybe 600 (approx 211 mm).

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
Guide ,
Sep 07, 2025 Sep 07, 2025

Setting column widths instead of cell widths might be more convenient.


First, set the column width to 20mm and check for overflow.
If overflow occurs, set it to 40mm, which should cover most scenarios.

 

Then adjust the width based on the longest content in the column.

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
Guide ,
Sep 07, 2025 Sep 07, 2025

Hi @m1b 

After selecting multiple cells,
getTables incorrectly returned multiple tables.

dublove_0-1757258164783.jpeg

It seems that when the cursor is positioned on text outside the table, the entire text box cannot be selected.

It would be ideal to handle three scenarios:

// Only when cursor is in table + current text frame
tbs = getTablesIn(items);

// When cursor is outside table + current text frame
tbs = getTablesTextFrame(items);

// When cursor is outside table + entire story
tbs = getTablesStorys(items);

 

Using these three existing combinations does not handle cases where the cursor is outside the table. This is because "getTables" is invalid. will throw an error in such situations.

 

In other words, when you select an image or place the cursor within text outside a table, getTables(doc.selection) will overflow and throw an error.

var tfs = getTextFrames(doc.selection);
var grs = getGraphics(doc.selection);
var tbs = getTables(doc.selection);
if(tfs.length>0&&grs.length<0&&tbs==0){.....
}

 

 

 

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
Guide ,
Sep 09, 2025 Sep 09, 2025
LATEST

Hi m1b.

It might be complicated.
It would suffice if it could include the table when the cursor is outside it, and also retrieve the table for the current text box or current article.

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