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();