Skip to main content
dublove
Legend
October 15, 2025
Question

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

  • October 15, 2025
  • 1 reply
  • 202 views

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]

1 reply

m1b
Community Expert
Community Expert
October 17, 2025

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;

};
dublove
dubloveAuthor
Legend
October 17, 2025

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

Thank you very much.

m1b
Community Expert
Community Expert
October 17, 2025

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