Skip to main content
Inspiring
Aug 12, 2022 10:31 AM UTC
Answered

Set hyperlink to cell contents with JavaScript

Hi all

 

I need to create hyperlinks to certain cell contents. The cells will have a script label, '1' in my example here, and it's always going to be in the first column of the table. I then have to add the content of the cell, which is always an article number, to the hyperlink, hence the + in the URLDestinations setting. Setting the Destination works and myCellData contains a text object which I think is correct. I think I'm almost there but adding the hyperlinkTextSources doesn't work and I can't figure out why.

 

my current code:

var myDoc = app.activeDocument;
var myCells = myDoc.stories.everyItem().tables.everyItem().cells.everyItem().getElements(); 
var mySource;
var myDest;
var myURL = 'https://myURL.com/';

for (var i = 0; i < myCells.length; i++){
    if (myCells[i].label == '1') {
        var myCellData = myCells[i].texts[0];
        mySource = myDoc.hyperlinkTextSources.add(myCellData);
        myDest = myDoc.hyperlinkURLDestinations.add(myURL+myCellData);
        myDoc.hyperlinks.add(mySource, myDest, {name:myURL});
    }
}

 Any help is much appreciated!

This topic is closed to new replies. Start a new post to keep the conversation going.
LaubenderCorrect answer
Community Expert
Aug 12, 2022 11:44 AM UTC

Hi @nna_top ,

look up method add() for hyperlinkURLDestinations in the DOM documentation.

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#HyperlinkURLDestinations.html#d1e119486__d1e119535

 

The type of the first argument should be a string.

The type of myCellData is not a string. It's a text object.

So currently you try to concat a string with something else, a text object.

 

Because this "something else" will be converted to a string by the concatening process, you should see something like that as the result and not what you expect:

https://myUrl/[object Text]

instead of the contents of the text cell.

 

Depending what exactly texts[0] in your cell contains, you could be successful if you use property contents of the text object.

myDest = myDoc.hyperlinkURLDestinations.add( myURL + myCellData.contents );

 

Regards,
Uwe Laubender
( Adobe Community Professional )

5 replies

Community Expert
Aug 12, 2022 3:00 PM UTC

One other thing:

If you test modified code do that every time on a new document.

You could prepare an *.indt template file and work from that.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Translate
nna_topAuthor
Inspiring
Aug 19, 2022 6:52 AM UTC

This might be a small thing but was a very valuable input I hadn't thought of. So thank you!!

Translate
Community Expert
Aug 12, 2022 1:26 PM UTC

Test your code on a single cell first before you go on writing a loop with an if statement.

Start with e.g. a selected cell where myCellData is app.selection[0].texts[0].

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Translate
LaubenderCommunity ExpertCorrect answer
Community Expert
Aug 12, 2022 11:44 AM UTC

Hi @nna_top ,

look up method add() for hyperlinkURLDestinations in the DOM documentation.

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#HyperlinkURLDestinations.html#d1e119486__d1e119535

 

The type of the first argument should be a string.

The type of myCellData is not a string. It's a text object.

So currently you try to concat a string with something else, a text object.

 

Because this "something else" will be converted to a string by the concatening process, you should see something like that as the result and not what you expect:

https://myUrl/[object Text]

instead of the contents of the text cell.

 

Depending what exactly texts[0] in your cell contains, you could be successful if you use property contents of the text object.

myDest = myDoc.hyperlinkURLDestinations.add( myURL + myCellData.contents );

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Translate
nna_topAuthor
Inspiring
Aug 12, 2022 12:22 PM UTC

Hi Uwe

 

Thank you for your input! The hyperlinkURLDestinations wasn't the issue (or at least not yet ;-)). Because I couldn't figure out what was wrong, I set an alert after every action inside the if statement and the one after hyperlinkTextSources didn't trigger. If I switch the Dest und Source parts, Dest works but Source doesn't...

Translate