Skip to main content
Participant
March 10, 2025
Answered

Insert URL automatically

  • March 10, 2025
  • 1 reply
  • 281 views

Hello,

Here is my problem: I have tables where one column contains product references.
At the moment, I manually add the URLs for these references by selecting the reference text and then adding the URL via the Hyperlinks panel, but it would save me a lot of time if I could do it automatically.


Here’s what it looks like:
REF01 | Description | otherInfo
REF02 | Description | otherInfo


The URLs are all the same, only the reference at the end of the URL changes. In the example, it would look like this:
www.url.com/REF01
www.url.com/REF02

Is there a solution to import the URLs into my tables automatically?

Thanks in advance for your help!

Correct answer Eugene Tyson

Hello - I had a similar script in my folders and adapted it for you 

 

It may not be the best approach but it works for me 

 

It should ignore header and footer rows - so you can define those in the table so the script doesn't pick them up.

Then it will add the Base URL - first line - change to the URL you need. 

 

Then select all the table - and run the script. 

Downside is if the URL hyperlinks exists it can't add the same one again - if that's an issue maybe there's a way around it to assign the hyperlink that already exists, you might have the product in different categories for example. 

 

Let us know how it goes and if you need any improvements. 

 

var baseURL = "https://www.url.com/"; // Change as needed

function addHyperlinksToTable() {
    var doc = app.activeDocument;
    var selection = app.selection;

    if (selection.length === 0 || !(selection[0] instanceof Table)) {
        alert("Please select a table.");
        return;
    }

    var table = selection[0];

    for (var i = 0; i < table.rows.length; i++) {
        var row = table.rows[i];

        // Ignore header and footer rows
        if (row.rowType !== RowTypes.BODY_ROW) {
            continue;
        }

        var cell = row.cells[0]; // First column assumed to contain the reference
        var refText = String(cell.contents).replace(/^\s+|\s+$/g, ""); // Ensure it's a string and trim spaces

        if (refText !== "") {
            var hyperlinkURL = baseURL + refText;

            // Check if a destination already exists with this name
            var destination;
            try {
                destination = doc.hyperlinkURLDestinations.itemByName(refText);
                if (!destination.isValid) {
                    destination = doc.hyperlinkURLDestinations.add(hyperlinkURL);
                }
            } catch (e) {
                destination = doc.hyperlinkURLDestinations.add(hyperlinkURL);
            }

            // Explicitly set the destination name
            destination.name = refText;

            // Create a hyperlink text source
            var source = doc.hyperlinkTextSources.add(cell.texts[0]);

            // Add the hyperlink
            var hyperlink = doc.hyperlinks.add(source, destination);

            // Ensure the hyperlink itself is named properly
            hyperlink.name = refText;
        }
    }
    alert("Hyperlinks added successfully!");
}

addHyperlinksToTable();

 

 

1 reply

Eugene TysonCommunity ExpertCorrect answer
Community Expert
March 10, 2025

Hello - I had a similar script in my folders and adapted it for you 

 

It may not be the best approach but it works for me 

 

It should ignore header and footer rows - so you can define those in the table so the script doesn't pick them up.

Then it will add the Base URL - first line - change to the URL you need. 

 

Then select all the table - and run the script. 

Downside is if the URL hyperlinks exists it can't add the same one again - if that's an issue maybe there's a way around it to assign the hyperlink that already exists, you might have the product in different categories for example. 

 

Let us know how it goes and if you need any improvements. 

 

var baseURL = "https://www.url.com/"; // Change as needed

function addHyperlinksToTable() {
    var doc = app.activeDocument;
    var selection = app.selection;

    if (selection.length === 0 || !(selection[0] instanceof Table)) {
        alert("Please select a table.");
        return;
    }

    var table = selection[0];

    for (var i = 0; i < table.rows.length; i++) {
        var row = table.rows[i];

        // Ignore header and footer rows
        if (row.rowType !== RowTypes.BODY_ROW) {
            continue;
        }

        var cell = row.cells[0]; // First column assumed to contain the reference
        var refText = String(cell.contents).replace(/^\s+|\s+$/g, ""); // Ensure it's a string and trim spaces

        if (refText !== "") {
            var hyperlinkURL = baseURL + refText;

            // Check if a destination already exists with this name
            var destination;
            try {
                destination = doc.hyperlinkURLDestinations.itemByName(refText);
                if (!destination.isValid) {
                    destination = doc.hyperlinkURLDestinations.add(hyperlinkURL);
                }
            } catch (e) {
                destination = doc.hyperlinkURLDestinations.add(hyperlinkURL);
            }

            // Explicitly set the destination name
            destination.name = refText;

            // Create a hyperlink text source
            var source = doc.hyperlinkTextSources.add(cell.texts[0]);

            // Add the hyperlink
            var hyperlink = doc.hyperlinks.add(source, destination);

            // Ensure the hyperlink itself is named properly
            hyperlink.name = refText;
        }
    }
    alert("Hyperlinks added successfully!");
}

addHyperlinksToTable();

 

 

JigehhAuthor
Participant
March 11, 2025

Hi Eugene!

 

Thanks for this, it works perfectly (:

You just saved me a lot of time!

 

Again, many thanks!