Skip to main content
January 29, 2018
Answered

Align to decimal point and center

  • January 29, 2018
  • 3 replies
  • 6087 views

Hi,

I am trying to set up a paragraph style which I want to use in a table for aligning figures to a decimal dot. However, as table columns width varies, I wouldn't like to set a fixed X, but rather I would like the X to adjust depending on the column width, always to be more or less centered (so the wider the column is, the X is greater). Is there any way to accomplish that?

This topic has been closed for replies.
Correct answer vinny38

Hi

Can you give this script a try and report back?

(run it on a copy of your document first)

if (app.documents.length > 0) {

    var myDocument = app.documents[0];

    //----------------------------Open Dialog box----------------------------//

    var myStylesNames = [];

    for (i = 0; i < myDocument.allParagraphStyles.length; i++) {

        myStylesNames.push(myDocument.allParagraphStyles.name);

    }

    var myDialog = app.dialogs.add({

        name: "Align to dots script",

        canCancel: true

    });

    with(myDialog.dialogColumns.add()) {

        staticTexts.add({

            staticLabel: "Choose the paragraph style applied to the cells you want to align on dot"

        });

    }

    with(myDialog.dialogColumns.add()) {

        var myDDSTyles = dropdowns.add({

            stringList: myStylesNames,

            selectedIndex: 0

        });

    }

    var myResult = myDialog.show();

    if (myResult == true) {

        var myParaStyle = myStylesNames[myDDSTyles.selectedIndex];

    }

    myDialog.destroy();

    //---------------------------- add a tab before (if doesn't exist)  ----------------------------//

    app.findGrepPreferences = null;

    app.changeGrepPreferences = null;

    app.findGrepPreferences.properties = {

        findWhat: "^(\\d)",

        appliedParagraphStyle: myParaStyle,

    }

    app.changeGrepPreferences.changeTo = "\\t$1";

    app.activeDocument.changeGrep();

    app.findGrepPreferences = null;

    app.changeGrepPreferences = null;

    //---------------------------- create array     ----------------------------//

    var myText = [];

    var allText = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().getElements();

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

        if (allText.appliedParagraphStyle.name == myParaStyle) {

            myText.push(allText);

        }

    }

    //---------------------------- Set tab alignment  and apply to cells   ----------------------------//

    var tabStopProperties = {

        alignment: TabStopAlignment.CHARACTER_ALIGN,

        alignmentCharacter: "."

    };

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

        var cellWidth = myText.parent.width;

        var cellLeftInset = myText.parent.leftInset;

        var cellRightInset = myText.parent.rightInset;

        var position = (cellWidth - cellRightInset - cellLeftInset - 1.058) / 2;

        myText.tabStops.everyItem().remove();

        myText.tabStops.add();

        myText.tabStops[0].properties = tabStopProperties;

        myText.tabStops[0].position = position;

    }

} else {

    alert("Please open a document");

}

3 replies

vinny38
vinny38Correct answer
Legend
January 30, 2018

Hi

Can you give this script a try and report back?

(run it on a copy of your document first)

if (app.documents.length > 0) {

    var myDocument = app.documents[0];

    //----------------------------Open Dialog box----------------------------//

    var myStylesNames = [];

    for (i = 0; i < myDocument.allParagraphStyles.length; i++) {

        myStylesNames.push(myDocument.allParagraphStyles.name);

    }

    var myDialog = app.dialogs.add({

        name: "Align to dots script",

        canCancel: true

    });

    with(myDialog.dialogColumns.add()) {

        staticTexts.add({

            staticLabel: "Choose the paragraph style applied to the cells you want to align on dot"

        });

    }

    with(myDialog.dialogColumns.add()) {

        var myDDSTyles = dropdowns.add({

            stringList: myStylesNames,

            selectedIndex: 0

        });

    }

    var myResult = myDialog.show();

    if (myResult == true) {

        var myParaStyle = myStylesNames[myDDSTyles.selectedIndex];

    }

    myDialog.destroy();

    //---------------------------- add a tab before (if doesn't exist)  ----------------------------//

    app.findGrepPreferences = null;

    app.changeGrepPreferences = null;

    app.findGrepPreferences.properties = {

        findWhat: "^(\\d)",

        appliedParagraphStyle: myParaStyle,

    }

    app.changeGrepPreferences.changeTo = "\\t$1";

    app.activeDocument.changeGrep();

    app.findGrepPreferences = null;

    app.changeGrepPreferences = null;

    //---------------------------- create array     ----------------------------//

    var myText = [];

    var allText = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().getElements();

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

        if (allText.appliedParagraphStyle.name == myParaStyle) {

            myText.push(allText);

        }

    }

    //---------------------------- Set tab alignment  and apply to cells   ----------------------------//

    var tabStopProperties = {

        alignment: TabStopAlignment.CHARACTER_ALIGN,

        alignmentCharacter: "."

    };

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

        var cellWidth = myText.parent.width;

        var cellLeftInset = myText.parent.leftInset;

        var cellRightInset = myText.parent.rightInset;

        var position = (cellWidth - cellRightInset - cellLeftInset - 1.058) / 2;

        myText.tabStops.everyItem().remove();

        myText.tabStops.add();

        myText.tabStops[0].properties = tabStopProperties;

        myText.tabStops[0].position = position;

    }

} else {

    alert("Please open a document");

}

FRIdNGE
January 29, 2018

Hi,

Just by curiosity, could you post a screenshot?

Best,

Greg, from FRIdNGE

January 30, 2018

Greg, I'm not sure whether your request refers to my original post or the response by Barb.

FRIdNGE
January 30, 2018

Hi,

It's about your original post! Could you post a sample of what you mean?

or do you talk about tables with numbers columns? [as Barbara showed us!]

Best,

Greg, from FRIdNGE

Barb Binder
Community Expert
Community Expert
January 29, 2018

Hi piotreba ,

Not within the InDesign feature set. You can add a decimal tab stop to a table cell (without having to add a tab character, by the way) but the the tab stop is fixed distance from the left edge of the column. If you change the column width, you'll need to update the tab location. Maybe ask in the InDesign Scripting ​forum?

There's a workaround that is useful in limited situations—not too many instances, and you need to use font with monospaced figures.

Set the figures to align center with the column:

Use figure spaces in front of the short lines with Type > Insert White Space > Figure Space:

Now when you resize the column, the figures stay centered.

As I said, good to know but of limited usefulness. I deal with this situation more often than I'd like to. Sometimes using figures spaces is the answer, sometimes moving the tabs is the answer, but my preference is hold off on the alignment until we reach the end of edits. My editors can proof the numbers in advance, but they wait on proofing the alignment until I let them know that I've finalized it.

~Barb

~Barb at Rocky Mountain Training
January 30, 2018

Thanks BarbBinder​,

Indeed in my case playing with figure spaces would be challenging. But it's a good hint anyway for some situations.

So I' have related question though: can I configure paragraph style in a way so that I set some basic tab options and then I manually adjust, here, aligning to a decimal, but without producing style overrides?

Barb Binder
Community Expert
Community Expert
January 30, 2018

Hi piotreba ,

can I configure paragraph style in a way so that I set some basic tab options and then I manually adjust, here, aligning to a decimal, but without producing style overrides?

That's the definition of an override: you set up a style with a dec tab position and then tweak it for the various column widths.

You could save multiply styles for the column widths, but that's also too much work.

Let us know if Vinny's script helps.

~Barb

~Barb at Rocky Mountain Training