Skip to main content
Inspiring
June 3, 2022
Answered

Get the same width for all the table

  • June 3, 2022
  • 8 replies
  • 894 views

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

 

 

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer m1b

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 

 

 


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.

8 replies

Peter Kahrel
Community Expert
Community Expert
June 3, 2022

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

P.

Inspiring
June 3, 2022

Hi @Peter Kahrel , 

 

Please guide me on writing the script. 

-Monisha
Peter Kahrel
Community Expert
Community Expert
June 3, 2022

What are your scripting skills?