Skip to main content
dublove
Legend
Jul 14, 2025 1:42 PM UTC
Question

What's going on here? Can't figure out why: The numbers in the table are centered and left-aligned.

Hi everyone.

Could you take a look for me?

This script's function:
Centers and left-aligns numbers within tables.

 

However, for some reason, it works fine when the table fits on a single page.
If the table is too long and spans multiple pages, errors occur.


Additionally, it runs somewhat slowly.

I'm unsure where the error lies.

Thank you very much.

 

 

It Prompt:

 

var doc = app.activeDocument;
var item = doc.selection[0];
app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;

(function () {
    var oColumns = app.selection[0].columns,
        i = 0,
        rCount = app.selection[0].rows.length,
        cCount = app.selection[0].columns.length,
        name, lastCell;
    for (; i < oColumns.length; i++) {
        name = app.selection[0].cells[i].name.split(':');
        lastCell = rCount > 1 ? oColumns[i].cells.item(name[0] + ':' + (Number(name[1]) + rCount - 1)) : oColumns[i].cells[-1];
        alignMe(oColumns[i].cells.itemByRange(app.selection[0].cells[i], lastCell));
    }

    function alignMe(c) {
        c.paragraphs.everyItem().rightIndent = 0
        c.paragraphs.everyItem().leftIndent = 0
        c.paragraphs.everyItem().justification = Justification.CENTER_ALIGN;
        var offsets = c.paragraphs.everyItem().endHorizontalOffset;
        maxOffset = Math.max.apply(null, offsets);
        //alert(maxOffset);
        c.paragraphs.everyItem().justification = Justification.LEFT_ALIGN;
        var newOffset = c.paragraphs[0].endHorizontalOffset;
        var nIndent = Math.abs(newOffset - maxOffset);
        //alert(nIndent);
        c.paragraphs.everyItem().leftIndent = nIndent;
    }
})();

 

13 replies

dublove
dubloveAuthor
Legend
Oct 24, 2025 5:14 PM UTC

It's been a long time, and I still haven't figured out the problem.

Translate
rob day
Community Expert
Community Expert
Oct 24, 2025 10:22 PM UTC

Check your var nIndent math with a $.writeln(). In this sample it’s returning a 559 pt indent

 

Translate
dublove
dubloveAuthor
Legend
Oct 25, 2025 3:38 AM UTC

@rob day 

I don't know how it's calculated.
Also, does the "$.writeln(nIndent)" you're using work in VS? It seems to output results without needing to run the script?

I used "alert(nIndent);" and got 182.

 

I noticed that some of the code is duplicated...

 

 

Translate
Mike Witherell
Community Expert
Community Expert
Jul 15, 2025 11:25 AM UTC

Why not use, simply, Cell Styles for this?

Translate
Mike Witherell
dublove
dubloveAuthor
Legend
Jul 15, 2025 12:40 PM UTC

Hi Mike Witherell 

That's not the point.

Translate