Skip to main content
Inspiring
July 11, 2023
Question

Auto-size function doesn't work on linked text boxes with Data Merge

  • July 11, 2023
  • 4 replies
  • 1241 views

I have two auto-size text boxes that are linked. Data merge field A sits in the top box, data merge field B sits in bottom text box. They are separated by a Coloumn Break set right after field A. The text boxes are auto resized correctly when holding the data merge fields but after merging, the top text box doesnt resize while the lower text box does. So one text box resizes to merged copy while the other doesnt. How can i fix this?

Thanks, JK 

This topic has been closed for replies.

4 replies

m1b
Community Expert
July 25, 2023

Hi @JacquesCapeTown (and @Eugene Tyson) here's one idea. I've written a script that just expands the width of any text frame marked with the script label "expandFrame" until the first paragraph fits. It won't always work, but it should work whenever the datamerge is expecting a single line paragraph. If you find a bug, let me know.

- Mark

 

 

/**
 * Expands width of text frames having label "expandFrame" in
 * an attempt to fit first paragraph of text frame in one line.
 * Notes:
 * - Label can contain other text as well as "expandFrame"
 * - Does not expand frame height
 * - Does not expand wider than the page width
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/auto-size-function-doesn-t-work-on-linked-text-boxes-with-data-merge/m-p/13950905
 */
function main() {

    if (app.documents.length > 0)
        expandFrames(app.activeDocument, /\bexpandFrame\b/i);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Expand Labelled Frames');


/**
 * Expand width of all text frames
 * in `doc` with matching label, in
 * attempt to fit first paragraph.
 * @author m1b
 * @version 2023-07-25
 * @param {Document} doc - an Indesign Document.
 * @param {RegExp} matcher - the regex to test each frame's label.
 */
function expandFrames(doc, matcher) {

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    var autoSizeRefToAnchorPoint = {
        1651467109: AnchorPoint.BOTTOM_CENTER_ANCHOR,
        1651469413: AnchorPoint.BOTTOM_LEFT_ANCHOR,
        1651470953: AnchorPoint.BOTTOM_RIGHT_ANCHOR,
        1668183154: AnchorPoint.CENTER_ANCHOR,
        1818583909: AnchorPoint.LEFT_CENTER_ANCHOR,
        1919509349: AnchorPoint.RIGHT_CENTER_ANCHOR,
        1953456997: AnchorPoint.TOP_CENTER_ANCHOR,
        1953459301: AnchorPoint.TOP_LEFT_ANCHOR,
        1953460841: AnchorPoint.TOP_RIGHT_ANCHOR,
    };

    var frames = doc.textFrames,
        inc = 1,
        frame,
        label,
        width,
        maxWidth,
        paragraph,
        end;

    for (var i = frames.length - 1; i >= 0; i--) {

        frame = frames[i];
        label = frame.label;
        width = (frame.geometricBounds[3] - frame.geometricBounds[1]);

        if (
            !label
            || !matcher.test(label)
        )
            continue;

        anchor = autoSizeRefToAnchorPoint[Number(frame.textFramePreferences.autoSizingReferencePoint)];
        paragraph = frame.paragraphs.firstItem();
        end = paragraph.characters.lastItem();

        // maxWidth is for reality testing
        if (frame.parentPage.isValid)
            maxWidth = frame.parentPage.bounds[3] - frame.parentPage.bounds[1];
        else
            maxWidth = doc.pages.firstItem().bounds[3] - doc.pages.firstItem().bounds[1];

        while (
            frame.id !== end.parentTextFrames[0].id
            && (width += inc) <= maxWidth
        )
            // expand the frame by a bit
            frame.resize(CoordinateSpaces.PARENT_COORDINATES, anchor, ResizeMethods.ADDING_CURRENT_DIMENSIONS_TO, [inc, 0]);

    }

};
Barb Binder
Community Expert
July 18, 2023

And that holds true with your examples. The top unthreaded frames adjust, the threaded frames only adjust the last frame.

 

Do they need to be threaded?

 

~Barb

~Barb at Rocky Mountain Training
Inspiring
July 19, 2023

Morning Barb, thanks for that.

The two text boxes will need to be linked for our purposes on this side. Always two text boxes, always two data merge field entries per merged document. But the Field A and Field B entries will differ wildly in length per merged document (they will be international translations from the english original) hence the need for the auto-sizing effect. So, In the linked text boxes, just after the Field A entry, there is a Column Break Character. By my reckoning, at merge, Indesign should pull in both entries from the CSV, notice that the Field A entry and the Field B entry need to be split into their separate text boxes (due to the Column Break Character) and then resize each text box individually. Thanks JK

Community Expert
July 19, 2023

I haven't looked at the files but tables will autosize if you have size set to at least 1.058mm I think is the min

 

So a table with 2 rows would change size depending on content, in theory

Barb Binder
Community Expert
July 18, 2023

Hi Jacques:

 

In my experience, it doesn't work that way. If two frames aren't linked, both will adjust to fit the content.

 

If two (or more) frames are linked, only the last box will adjust even though the property is enabled for both, because the overflowing text in frame 1 just flows to the next box in the series.

 

~Barb

~Barb at Rocky Mountain Training
Community Expert
July 18, 2023

Having a hard time digitising the workflow in my brain - can you provide images or a sample doc? 

 

Sorry - my 3D engine seems to be down

Inspiring
July 18, 2023

Sure - herewith. thanks JK