• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Getting the longest physical width of text in a cell column and use it as reference for tab stop.

Explorer ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

Hello,

I need help. I am working with columns. I need to find the longest character width and create a tabstop aligning (character alignment: ")" ) all the figures to the longest character in the selected column.

 

This is the unformatted: (in center align)

Capture.PNG   

This is formatted with tab stop: (character align: ")", left-justified)

 Capture2.PNG

This is the script that I'm using. It only applies to individual cells so basically, it just center-aligns every cell. 

 

var textsArray = app.selection[0].cells.everyItem().texts.everyItem().getElements();
var tabStopProperties =
{
    alignment : TabStopAlignment.CHARACTER_ALIGN ,
    alignmentCharacter : ")"
};

for(var n=0;n<textsArray.length;n++)
{
    start = textsArray[n].insertionPoints[0].horizontalOffset;
    end = textsArray[n].insertionPoints[-1].horizontalOffset;
    distance = end - start;

    var widthA = app.selection[0].cells[0].width;
    var widthB = distance;
    var widthC = widthA - widthB;
    var widthD = widthC/2
    var position = widthA - widthD;
    
    textsArray[n].tabStops.everyItem().remove();
    textsArray[n].tabStops.add();
    textsArray[n].tabStops[0].properties = tabStopProperties;
    textsArray[n].tabStops[0].position = position;
    textsArray[n].justification = Justification.LEFT_ALIGN;

};

 

Thanks,

Martin

 

TOPICS
Scripting

Views

472

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Apr 03, 2020 Apr 03, 2020

Hi Brian!

 

I think Martin tries to get something like this!

[script written for a client 3 years ago!]

 

(^/) The Jedi

 

Capture d’écran 2020-04-03 à 15.10.50.png

Votes

Translate

Translate
Community Expert ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

You need to iterate through each cell, not use everyItem in this case. (But I don't know why you just wouldn't use right align for the text and save yourself the trouble?)

var cells = app.selection[0].cells;
for (var i = 0; i < cells.length; i++) {
   var texts = cells[i].texts.everyItem().getElements();
   ...
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 03, 2020 Apr 03, 2020

Copy link to clipboard

Copied

Hi Brian!

 

I think Martin tries to get something like this!

[script written for a client 3 years ago!]

 

(^/) The Jedi

 

Capture d’écran 2020-04-03 à 15.10.50.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 04, 2020 Apr 04, 2020

Copy link to clipboard

Copied

LATEST

Hi Michel,

 

This is what I need to achieve. Can you share it with me? Hehe.

 

-Martin

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines