Skip to main content
dublove
Legend
Oct 15, 2025 1:57 PM UTC
Question

Use script,How can I get the first cell in each column of a multiple-selection range?

I've selected multiple rows and columns, but I want to get the first cell in each column. Can this be achieved with a script?

 

The selection area is not fixed; it may span two rows or three columns.
It is possible that only one cell was selected.

 

The selection area is not fixed; it could span 2 rows or 3 columns.
It's possible that only a single cell was dragged to select.
Like this:

columns[i].cells[0]

3 replies

m1b
Community Expert
Community Expert
Oct 17, 2025 11:53 AM UTC

Hi @dublove, like this:

function main() {

    var doc = app.activeDocument;
    var cells = getCells(doc.selection);

    // collect only the uppermost cell in each column
    var upperCells = [];

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

        if (
            undefined === upperCells[cells[i].parentColumn.index]
            || cells[i].parentRow.index < upperCells[cells[i].parentColumn.index].parentRow.index
        )
            upperCells[cells[i].parentColumn.index] = cells[i];

    }

    // removing leading space from the array
    while (!upperCells[0])
        upperCells.shift();

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');

/**
 * Returns array of cells, given an object.
 * @author m1b
 * @version 2024-05-03
 * @param {*} obj - an object, can be an array, eg. a document selection.
 * @returns {Array<Cell>}
 */
function getCells(obj) {

    var cells = [];

    if (obj == undefined)
        return [];

    else if ('Array' === obj.constructor.name)
        for (var i = 0; i < obj.length; i++)
            cells = cells.concat(getCells(obj[i]));

    else if ('Document' === obj.constructor.name)
        cells = getCells(getThingsFromDocument(obj, 'stories'));

    else if (
        obj.hasOwnProperty('cells')
        && obj.cells.length > 0
    )
        cells = obj.cells.everyItem().getElements();

    else if (
        obj.hasOwnProperty('tables')
        && obj.tables.length > 0
    )
        cells = getCells(obj.tables.everyItem().cells.everyItem().getElements())

    else if ('Cell' === obj.constructor.name)
        cells = [obj];

    else if (
        obj.hasOwnProperty('parent')
        && 'Cell' === obj.parent.constructor.name
    )
        cells = getCells(obj.parent.parent);

    else if ('Table' === obj.constructor.name)
        cells = obj.cells.everyItem().getElements();

    return cells;

};

/**
 * Returns array of things found in `doc`.
 * `thingPlural` is a Document property name,
 * for example 'formFields' or 'swatches'.
 * @author m1b
 * @version 2024-05-03
 * @param {Document} doc - an Indesign Document.
 * @param {String} thingPlural - collective property of Document eg. 'checkBoxes'.
 * @returns {?Array<*>} - the things.
 */
function getThingsFromDocument(doc, thingPlural) {

    if (
        !doc.hasOwnProperty(thingPlural)
        || 'function' !== typeof doc[thingPlural].everyItem
    )
        return;

    var things = doc[thingPlural].everyItem().getElements();

    if (
        doc.hasOwnProperty('groups')
        && doc.groups.length > 0
        && doc.groups[0].hasOwnProperty(thingPlural)
    )
        // also collect things in groups
        things = things.concat(doc.groups.everyItem()[thingPlural].everyItem().getElements());

    if (
        doc.hasOwnProperty('stories')
        && doc.stories.length > 0
        && doc.stories[0].hasOwnProperty(thingPlural)
    ) {

        // also collect any anchored things
        things = things.concat(doc.stories.everyItem()[thingPlural].everyItem().getElements());

        if (doc.stories.everyItem().tables.length > 0)
            // also collect any thing in tables
            things = things.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem()[thingPlural].everyItem().getElements());
    }

    return things;

};
Translate
dublove
dubloveAuthor
Legend
Oct 17, 2025 1:36 PM UTC

This is a bit complicated.
Perhaps my research direction is incorrect.

Thank you very much.

Translate
m1b
Community Expert
Community Expert
Oct 17, 2025 1:45 PM UTC

It is much simpler if you know what kind of cell selection you have. The real details are in the main function.

Translate