Skip to main content
dublove
Legend
July 14, 2025
Question

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

  • July 14, 2025
  • 2 replies
  • 442 views

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;
    }
})();

 

2 replies

dublove
dubloveAuthor
Legend
October 24, 2025

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

rob day
Community Expert
Community Expert
October 24, 2025

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

 

dublove
dubloveAuthor
Legend
October 25, 2025

@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...

 

 

Mike Witherell
Community Expert
Community Expert
July 15, 2025

Why not use, simply, Cell Styles for this?

Mike Witherell
dublove
dubloveAuthor
Legend
July 15, 2025

Hi Mike Witherell 

That's not the point.