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

Get the same width for all the table

Participant ,
Jun 03, 2022 Jun 03, 2022

Copy link to clipboard

Copied

Hi,

 

I need a script to get the width of the image to fit the page size and the width of all the tables must be unique. And the table must fit to the document and the frame. Please guide me on this...

 

 

-Monisha
TOPICS
How to , Scripting

Views

194

Likes

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

Community Expert , Jun 04, 2022 Jun 04, 2022

Hi @MonishaRajendran, I've written a script that will (a) work out the maximum width for the table and (b) set the table to that width. It probably won't do exactly what you need straight away, but at least it shows an approach to the problem. It was an interesting problem for me to solve, but I have a suspicion that there must be an easier way than mine. 🙂 Please let me know if it's helpful.

- Mark

 

 

/*

 Get Paragraph Max Width
 for Adobe Indesign 2022
 by m1b
 from https://community.adobe.
...

Likes

Translate

Translate
Community Expert ,
Jun 03, 2022 Jun 03, 2022

Copy link to clipboard

Copied

Guide you on writing the script yourself or guide you on how to find someone to do it for you for a fee?

P.

Likes

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
Participant ,
Jun 03, 2022 Jun 03, 2022

Copy link to clipboard

Copied

Hi @Peter Kahrel , 

 

Please guide me on writing the script. 

-Monisha

Likes

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
Community Expert ,
Jun 03, 2022 Jun 03, 2022

Copy link to clipboard

Copied

What are your scripting skills?

Likes

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
Participant ,
Jun 03, 2022 Jun 03, 2022

Copy link to clipboard

Copied

I used the script for fitting the table into the page indent but it was overriding the values in the tables.

 

var textFitoption= app.scriptArgs.get("textFitOption");
var imageFitoption= app.scriptArgs.get("imageFitOption");
var tableFitoption= app.scriptArgs.get("tableFitOption");
var myDocument=app.activeDocument;

with (myDocument.viewPreferences){
var myOldXUnits = horizontalMeasurementUnits;
var myOldYUnits = verticalMeasurementUnits;
}

//alert("5");

myDocument.viewPreferences.horizontalMeasurementUnits = myOldXUnits;
myDocument.viewPreferences.verticalMeasurementUnits = myOldYUnits;
for(myCounter = 0; myCounter < app.activeDocument.activeLayer.textFrames.length; myCounter++)
{
myStory = app.activeDocument.activeLayer.textFrames.item(myCounter);
if ((myStory.previousTextFrame == null && myStory.nextTextFrame == null) || (myStory.previousTextFrame != null && myStory.nextTextFrame == null));
FitPageItem (myStory);
}
function FitPageItem (mySel)
{
if (mySel.constructor.name== "Rectangle")
{
if(imageFitoption=="Fit Frame To Content")
{
mySel.fit(FitOptions.frameToContent);
}
else if(imageFitoption=="Fit Content To Frame")
{
mySel.fit(FitOptions.contentToFrame);
}
else if(imageFitoption=="Center Content")
{
mySel.fit(FitOptions.centerContent);
}
else if(imageFitoption=="Fit Content Proportionally")
{
mySel.fit(FitOptions.proportionally);
}
else if(imageFitoption=="Fill Frame Proportionally")
{
mySel.fit(FitOptions.fillProportionally);
}
}

if (mySel.constructor.name == "TextFrame")
{
if (mySel.tables.length>0)
{
if(tableFitoption=="true")
{
FitTable (mySel);
mySel.fit(FitOptions.frameToContent);
}
}


//alert( myDocument.viewPreferences.horizontalMeasurementUnits);
//~ myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.INCHES;
//~ myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES;

graphicItems = mySel.allGraphics;
for(g=0;g<graphicItems.length;g++)
{
graphicItems[g].parent.resize(CoordinateSpaces.innerCoordinates, AnchorPoint.centerAnchor,
ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, [50,50]);
FitTable (mySel);
mySel.fit(FitOptions.frameToContent);

if(imageFitoption=="Fit Frame To Content")
{
graphicItems[g].fit(FitOptions.frameToContent);
}
else if(imageFitoption=="Fit Content To Frame")
{
graphicItems[g].fit(FitOptions.contentToFrame);
}
else if(imageFitoption=="Center Content")
{
graphicItems[g].fit(FitOptions.centerContent);
}
else if(imageFitoption=="Fit Content Proportionally")
{
graphicItems[g].fit(FitOptions.proportionally);
}
else if(imageFitoption=="Fill Frame Proportionally")
{
graphicItems[g].fit(FitOptions.fillProportionally);
}

}
//else
{
if(textFitoption=="true")
{
mySel.fit(FitOptions.frameToContent);
}
}

}
}


myDocument.viewPreferences.horizontalMeasurementUnits = myOldXUnits;
myDocument.viewPreferences.verticalMeasurementUnits = myOldYUnits;

function FitTable (mySel)
{

var frameBounds = mySel.geometricBounds;
frameBounds[2] = frameBounds[2] + 40;
frameBounds[3] = frameBounds[3] + 60;
mySel.geometricBounds = frameBounds;
mySel.recompose();
try
{
for( var j = 0; j< mySel.tables.length ; j++ )
{
var table = mySel.tables[j];
//alert(mySel.tables[j].columns.length);
for( var i = 0; i< mySel.tables[j].columns.length ;i++ )
{
//alert("COLUNMSS");
// mySel.tables[j].columns[i].width=mySel.tables[j].columns[i].width*10;
//alert("Table column width:::"+mySel.tables[j].columns[i].width);
try
{
if(mySel.tables[j].columns[i].width<100)
{
//alert("111");
mySel.tables[j].columns[i].width=mySel.tables[j].columns[i].width*10;
}
else
{
// alert("222");
mySel.tables[j].columns[i].width=mySel.tables[j].columns[i].width;
}
}
catch(Exception)
{}
}

//Recompose Table and its Cells
mySel.tables[j].recompose();
for( var i = 0; i< mySel.tables[j].cells.length ;i++ )
{
mySel.tables[j].cells[i].recompose();
}

//Get the Cell Widths
var cellWidth = new Array(mySel.tables[j].cells.length);
for( var i = 0; i< mySel.tables[j].cells.length ;i++ )
{
if(mySel.tables[j].cells[i].insertionPoints.length>0)
{
if(mySel.tables[j].cells[i].insertionPoints[0].texts.length>0)
{
var newWidth;
var startPt,endPt;
newWidth = mySel.tables[j].cells[i].leftInset + mySel.tables[j].cells[i].rightInset;
mySel.tables[j].cells[i].insertionPoints[0].texts[0].recompose();
try{
startPt = mySel.tables[j].cells[i].insertionPoints[0].horizontalOffset;
//endPt = mySel.tables[j].cells[i].texts[mySel.tables[j].cells[i].texts.length-1].endHorizontalOffset;
endPt =table.cells[i].insertionPoints[table.cells[i].insertionPoints.count()-1].texts[mySel.tables[j].cells[i].texts.length-1].endHorizontalOffset;
}
catch(errCode){startPt=0;endPt=0;}

var len = endPt - startPt;
newWidth = newWidth+ len;
cellWidth[i] = newWidth; //alert("cellwidth:"+cellWidth[i]);
}
}
}

//Fit header Column First
//alert("Fit header Column First");
//for( var i= mySel.tables[j].columns.length ; i>=0; i-- )

//Fit header Column First
for( var i = 0; i< mySel.tables[j].columns.length ;i++ )
{
if(mySel.tables[j].cells[i].insertionPoints.length>0)
{
if(mySel.tables[j].cells[i].insertionPoints[0].texts.length>0)
{
var newWidth;
var startPt,endPt;
newWidth = mySel.tables[j].cells[i].leftInset + mySel.tables[j].cells[i].rightInset;
startPt = mySel.tables[j].cells[i].insertionPoints[0].texts[0].horizontalOffset;
endPt = mySel.tables[j].cells[i].texts[mySel.tables[j].cells[i].texts.length-1].endHorizontalOffset;
var len = endPt - startPt;
newWidth = newWidth+ len;
//mySel.tables[j].cells[i].parentColumn.width=newWidth;

if(mySel.tables[j].cells[i].leftInset<=0)
{
newWidth=newWidth*100;
mySel.tables[j].cells[i].parentColumn.width = newWidth; //alert(table.cells[i].parentColumn.width);
}

else if(mySel.tables[j].cells[i].leftInset<100)
{
// newWidth=newWidth*10;
mySel.tables[j].cells[i].parentColumn.width = newWidth; //alert(table.cells[i].parentColumn.width);
}
}
}
}

//Fit Body cells
try
{
for( var i = mySel.tables[j].columns.length ; i< mySel.tables[j].cells.length ;i++ )
{
var newWidth = cellWidth[i];
if(mySel.tables[j].cells[i].parentColumn.width<newWidth)
{
//alert("contents...***"+mySel.tables[j].cells[i].contents);
mySel.tables[j].cells[i].parentColumn.width = newWidth;
}
}
}
catch(Exception)
{}
}
}
catch(Exception)
{}
}

 

 

 

 

-Monisha

Likes

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
Community Expert ,
Jun 03, 2022 Jun 03, 2022

Copy link to clipboard

Copied

Hi @MonishaRajendran,

For me, as always your problem is not described aptly to understand it properly. However, I have a suggestion, when you post the code make sure it is properly indented and use the code block for formatting it, that way it's easier to read. Secondly don't just paste the whole blob of code, but try to paste a sample that demonstrates the problem and describe it briefly as how it currently works and what is lacking.

Hope this makes sense

-Manan

Likes

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
Participant ,
Jun 03, 2022 Jun 03, 2022

Copy link to clipboard

Copied

Hi @Manan Joshi 

 

My aim is to fit the table into a page for that I have the cell width, and left and right indent spacing values. In that table, we have some text attributes and image attributes. While resizing the image attributes, the table gets collapsed and doesn't fit into the particular page. For fitting the text attributes I have used the fitting script but the page remains the same. I have attached the screenshot of the document 

 

 

-Monisha

Likes

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
Community Expert ,
Jun 04, 2022 Jun 04, 2022

Copy link to clipboard

Copied

Hi @MonishaRajendran, I've written a script that will (a) work out the maximum width for the table and (b) set the table to that width. It probably won't do exactly what you need straight away, but at least it shows an approach to the problem. It was an interesting problem for me to solve, but I have a suspicion that there must be an easier way than mine. 🙂 Please let me know if it's helpful.

- Mark

 

 

/*

 Get Paragraph Max Width
 for Adobe Indesign 2022
 by m1b
 from https://community.adobe.com/t5/indesign-discussions/get-the-same-width-for-all-the-table/m-p/12982913

 Calculate the full width of a paragraph column measure
 and, in this example case, apply to a table width.

*/

function main() {

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    var doc = app.activeDocument;
    if (doc.selection.length && doc.selection[0].hasOwnProperty('tables')) {
        var table = doc.selection[0].tables[0];
        if (table.isValid) {
            var newWidth = getParagraphMaxWidth(table.storyOffset.texts[0]);
            if (newWidth != undefined)
                table.width = newWidth;
        }
    }
    else alert('Please select a text frame containing a table and try again.');


    /**
     * Returns the maximum width of the
     * column measure, taking into account
     * eg. paragraph indents, frame indents, etc.
     * It works by left- then right-justifying
     * the paragraph and measuring the location
     * of the first and last insertion points.
     *
     * @param {Text} text - text object
     * @return {Number} the max width
     */
    function getParagraphMaxWidth(text) {
        if (
            text.constructor.name != 'Paragraph'
            && text.paragraphs[0].isValid
        )
            text = text.paragraphs[0];

        if (text.constructor.name != 'Paragraph')
            return;

        var oldJustification = text.justification,
            hasTable = text.tables.length > 0,
            tableSideThickness = 0,
            // when an insertionPoint is before or after
            // a table that is justified towards it,
            // it has an offset of 1 point for some reason
            specialTableHorizontalOffset = hasTable ? 1 : 0;

        var left, right, insertionPointOffsets;

        // get left position when left justified
        text.justification = Justification.LEFT_ALIGN;
        insertionPointOffsets = text.paragraphs[0].insertionPoints.everyItem().horizontalOffset;
        // ignore last insertionPoint if not in same parent text frame
        if (text.insertionPoints[0].parentTextFrames[0].id !== text.insertionPoints[-1].parentTextFrames[0].id)
            insertionPointOffsets.pop();
        left = Math.min.apply(null, insertionPointOffsets) + specialTableHorizontalOffset;

        // get right position when right justified
        text.justification = Justification.RIGHT_ALIGN;
        insertionPointOffsets = text.paragraphs[0].insertionPoints.everyItem().horizontalOffset;
        // ignore last insertionPoint if not in same parent text frame
        if (text.insertionPoints[0].parentTextFrames[0].id !== text.insertionPoints[-1].parentTextFrames[0].id)
            insertionPointOffsets.pop();
        right = Math.max.apply(null, insertionPointOffsets) - specialTableHorizontalOffset;

        // reinstate old justification
        text.justification = oldJustification;

        if (hasTable)
            // add sideStrokeWidths
            tableSideThickness += (
                text.tables[0].columns[0].leftEdgeStrokeWeight / 2
                + text.tables[0].columns[-1].rightEdgeStrokeWeight / 2
            );

        // calculate maxWidth
        var maxWidth = right - left - tableSideThickness;
        return maxWidth;
    }
}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Maximize Table Width");

 

 

 

Edit 1: removed the setTableWidth function because it didn't do any better than simply setting table.width!

 

Edit 2: improved getParagraphMaxWidth function so it is more robust. Now works correctly when last insertionPoint is in another text frame and takes side stroke weights into account.

Likes

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
Community Expert ,
Jun 05, 2022 Jun 05, 2022

Copy link to clipboard

Copied

LATEST

Hi @MonishaRajendran, just letting you know I have edited my script posted above. The old one will fail in some cases so please check that you haven't copied my weaker code. - Mark

Likes

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