• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Set hyperlink to cell contents with JavaScript

Community Beginner ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

149

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 12, 2022 Aug 12, 2022

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 someth

...

Votes

Translate

Translate
Community Expert ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

Hi @nna_top ,

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

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

 

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2022 Aug 18, 2022

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines