Skip to main content
dublove
Legend
Oct 17, 2025 1:50 PM UTC
Answered

How to determine the number of columns in a text box and obtain the width of one column?

I want to get the width of the parent(textFrame) of the current table.
If the text box has only one column, the width is the entire text box width.
If the text box has multiple columns, I want to get the width of one column.

 

Suppose I have now selected the table and want to obtain myWidth.

 

m1b
m1bCorrect answer
Community Expert
Oct 17, 2025 2:24 PM UTC

Hi @dublove try this:

/**
 * @file Get Column Width of Table.js
 * 
 * Example showing how to get a table from the selection,
 * and then the parent text frame's column width.
 * 
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-determine-the-number-of-columns-in-a-text-box-and-obtain-the-width-of-one-column/m-p/15550852
 */
function main() {

    var doc = app.activeDocument;
    var table = getParent(doc.selection[0], 'Table');

    if (!table)
        return alert('Please put cursor into a table and try again.');

    var textFrame = table.storyOffset.parentTextFrames[0];
    var columnCount = textFrame.textFramePreferences.textColumnCount;
    var columnGutter = textFrame.textFramePreferences.textColumnGutter;
    var b = textFrame.geometricBounds;
    var frameWidth = b[3] - b[1];
    var inset = textFrame.textFramePreferences.insetSpacing;
    var columnWidth = (frameWidth - inset[1] - inset[3] - (columnGutter * (columnCount - 1))) / columnCount;

    alert('Column width = ' + columnWidth);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Get Column Width of Table');

/**
 * Return an item with the matching constructor name, if a parent of `item`.
 * @author m1b
 * @version 2025-10-18
 * @param {PageItem|*} item - an Indesign DOM item.
 * @param {String} constructorName - the constructor name to match.
 * @returns {*?}
 */
function getParent(item, constructorName) {

    try {

        while (item.hasOwnProperty('parent')) {

            if (constructorName === item.constructor.name)
                return item;

            item = item.parent;

        }

    } catch (error) { }

};

5 replies

Dave Creamer of IDEAS
Community Expert
Community Expert
Oct 17, 2025 3:59 PM UTC

If you just want some quick info...

For the column width, use the Text Frame Options:

 

For a table column width, use the Cell Options (select column, fully or partially):

 

Translate
David Creamer: Community Expert (ACI and ACE 1995-2023)
dublove
dubloveAuthor
Legend
Oct 18, 2025 9:19 AM UTC

Hi Dave Creamer of IDEAS.

I want to use scripts to control

Translate
m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
Oct 17, 2025 2:24 PM UTC

Hi @dublove try this:

/**
 * @file Get Column Width of Table.js
 * 
 * Example showing how to get a table from the selection,
 * and then the parent text frame's column width.
 * 
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-determine-the-number-of-columns-in-a-text-box-and-obtain-the-width-of-one-column/m-p/15550852
 */
function main() {

    var doc = app.activeDocument;
    var table = getParent(doc.selection[0], 'Table');

    if (!table)
        return alert('Please put cursor into a table and try again.');

    var textFrame = table.storyOffset.parentTextFrames[0];
    var columnCount = textFrame.textFramePreferences.textColumnCount;
    var columnGutter = textFrame.textFramePreferences.textColumnGutter;
    var b = textFrame.geometricBounds;
    var frameWidth = b[3] - b[1];
    var inset = textFrame.textFramePreferences.insetSpacing;
    var columnWidth = (frameWidth - inset[1] - inset[3] - (columnGutter * (columnCount - 1))) / columnCount;

    alert('Column width = ' + columnWidth);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Get Column Width of Table');

/**
 * Return an item with the matching constructor name, if a parent of `item`.
 * @author m1b
 * @version 2025-10-18
 * @param {PageItem|*} item - an Indesign DOM item.
 * @param {String} constructorName - the constructor name to match.
 * @returns {*?}
 */
function getParent(item, constructorName) {

    try {

        while (item.hasOwnProperty('parent')) {

            if (constructorName === item.constructor.name)
                return item;

            item = item.parent;

        }

    } catch (error) { }

};
Translate
dublove
dubloveAuthor
Legend
Oct 17, 2025 3:13 PM UTC

Hi @m1b 

Seems like there's an issue—did it get stuck in an infinite loop?
I tried three times with no results. After five seconds, the ID software shut down.

Translate