Skip to main content
Known Participant
March 10, 2024
Answered

Create hypherlinks from table numbers to the page

  • March 10, 2024
  • 6 replies
  • 2753 views

Hello, 

Here is another task for creative minds. 

I am working with many account tables and some of them have links with source informations. I attached sample table and it described what I need. I need  automate this process, because of many tables have this kind of links. 

"Find the numbers under the page column and link it to the page it shows.˜ PDF describe it more. 

 

This topic has been closed for replies.
Correct answer Peter Kahrel

I see what you mean. Sorry, I misunderstood. The script tries to create only document-internal links, but the destination page can be in a different file. Here's the upgraded script.

 

Open all the documents, then run the script.

 

(function () {

  var docs = app.documents.everyItem().getElements();

  function findExternalPage (folio) {
    for (var i = 0; i < docs.length; i++) {
      if (docs[i].pages.item(folio).isValid) {
        return docs[i].pages.item(folio);
      }
    }
    return null;
  }

  function addLinks (d, cells) {
    var page;
    for (var i = 1; i < cells.length; i++) {
      page = d.pages.item(cells[i].contents);
      if (page.isValid) {
        d.hyperlinks.add (
          d.hyperlinkTextSources.add (cells[i].texts[0]),
          d.hyperlinkPageDestinations.add (page)
        );
      } else {
        page = findExternalPage (cells[i].contents);
        if (page) {
          d.hyperlinks.add (
            d.hyperlinkTextSources.add (cells[i].texts[0]),
            d.hyperlinkExternalPageDestinations.add (page)
          );
        }
      }
    }
  }

  function processDocument (d) {
		
    var columns = d.stories.everyItem().
      tables.everyItem().
      columns.everyItem().
      getElements();
    
    for (var i = 0; i < columns.length; i++) {
      if (columns[i].cells[0].contents === 'Page') {
        addLinks (d, columns[i].cells.everyItem().getElements());
      }
    }
  }

  for (var i = 0; i < docs.length; i++) {
    processDocument (docs[i]);
  }

}());

6 replies

Community Expert
March 12, 2024

That indicates that there is a document that lacks the paragraph style named at the top of the script. Check that all your documents in fact do have that style, and that that style is not in a style group.

Known Participant
June 20, 2024

Can this script work for me, I have a yarli calendar and want to auto link with the daily page foot of that date, if it can be done, if you help me for this, I will thank you very much. I am uploading you my .indd file. I have done manual work in it. I want to auto like that. Can you help me for that, I will thank you very much from the bottom of my heart

Known Participant
June 20, 2024

You need an entirely different script, and I doubt that it's feasible with the state of your document. For instance, the rectangles that should be clickable are not marked up at all. So for any rectangle, how do you know which date it is? Furthermore, on the target pages the dates are in a group and in the [Basic Paragraph] style, so you won't be able unambiguously to find the correct pages.


Thank you for your prompt response. I would greatly appreciate your assistance with this change as you suggested. Your help would mean a lot to me, as I have been spending many days on this task, and it has become quite tedious. Please help me if you can. Thank you very much.

Community Expert
March 12, 2024

Here you go. Another change will cost you a case of the best Rioja!

 

Insert the name of your style where it now says ''page-number reference', open all the documents, and run the script.

 

(function () {
  
  var styleName = 'page-number reference';

  var docs = app.documents.everyItem().getElements();
	
  //------------------------------------------------------

  function findExternalPage (folio) {
    for (var i = 0; i < docs.length; i++) {
      if (docs[i].pages.item(folio).isValid) {
        return docs[i].pages.item (folio);
      }
    }
    return null;
  }

  function addLinks (d, found) {
    var page = d.pages.item (found.contents);
    if (page.isValid) {
      d.hyperlinks.add (
        d.hyperlinkTextSources.add (found),
        d.hyperlinkPageDestinations.add (page)
      );
    } else {
      page = findExternalPage (found.contents);
      if (page) {
        d.hyperlinks.add (
          d.hyperlinkTextSources.add (found),
          d.hyperlinkExternalPageDestinations.add (page)
        );
      }
    }
  }

  function processDocument (d) {
    app.findGrepPreferences.appliedParagraphStyle = 
      d.paragraphStyles.item (styleName);
    var pp = d.findGrep();
    for (var i = pp.length-1; i >= 0; i--) {
      addLinks (d, pp[i]);
    }
  }

  //--------------------------------------------------
	
  app.findGrepPreferences = null;

  for (var i = 0; i < docs.length; i++) {
    processDocument (docs[i]);
  }

}());

 

Known Participant
March 12, 2024

Peter Kahrel, 

Script run and links generated perfectly. But attached error comes up. 

Why is that?  Maybe my poor knowledge about scripts missed something???

Community Expert
March 12, 2024

What do you mean by 'table text style'? A character style? Cell style? Paragraph style?

Known Participant
March 12, 2024

Peter Kahrel, 

Sorry, I mean paragraph style. 

Because, sometimes tables has column with header named "Page". Sometimes it's "Notes" . Maybe in future it can be another name.  But we can keep same paragraph style everytime.  

Peter KahrelCorrect answer
Community Expert
March 11, 2024

I see what you mean. Sorry, I misunderstood. The script tries to create only document-internal links, but the destination page can be in a different file. Here's the upgraded script.

 

Open all the documents, then run the script.

 

(function () {

  var docs = app.documents.everyItem().getElements();

  function findExternalPage (folio) {
    for (var i = 0; i < docs.length; i++) {
      if (docs[i].pages.item(folio).isValid) {
        return docs[i].pages.item(folio);
      }
    }
    return null;
  }

  function addLinks (d, cells) {
    var page;
    for (var i = 1; i < cells.length; i++) {
      page = d.pages.item(cells[i].contents);
      if (page.isValid) {
        d.hyperlinks.add (
          d.hyperlinkTextSources.add (cells[i].texts[0]),
          d.hyperlinkPageDestinations.add (page)
        );
      } else {
        page = findExternalPage (cells[i].contents);
        if (page) {
          d.hyperlinks.add (
            d.hyperlinkTextSources.add (cells[i].texts[0]),
            d.hyperlinkExternalPageDestinations.add (page)
          );
        }
      }
    }
  }

  function processDocument (d) {
		
    var columns = d.stories.everyItem().
      tables.everyItem().
      columns.everyItem().
      getElements();
    
    for (var i = 0; i < columns.length; i++) {
      if (columns[i].cells[0].contents === 'Page') {
        addLinks (d, columns[i].cells.everyItem().getElements());
      }
    }
  }

  for (var i = 0; i < docs.length; i++) {
    processDocument (docs[i]);
  }

}());
Known Participant
March 12, 2024

Peter Kahrel, 

This fulfilled my requirement. Great script for keeping hours of time from my works. Thank you very much. 

In your script, you targeting page column in the table, right? Is it possible to target specifit table text style? If so no need to worry about column header. Script catch all the values in text tyles we mention. Is it possible? If so, can I edit this script?

Please let me know.

Community Expert
March 11, 2024

You can use this script to run your script against a folder of files:

https://creativepro.com/files/kahrel/indesign/batch_convert.html

See the section 'Run a script' for more details.

Known Participant
March 11, 2024

Peter Kahrel, 

I am confusing.... This batch process not support to me. 

Actually I need to make links between two ID files with same book folder. 

Example : "If my book folder has 3 ID files. The table with page numbers in third ID file. Some numbers want to link with first ID file. " Page numbers are fixed. 

Is that your batch process script can handle this? 

Community Expert
March 10, 2024

Good exercise for a Sunday afternoon:

 

(function () {

  var d = app.activeDocument;

  var columns = d.stories.everyItem().
    tables.everyItem().
    columns.everyItem().
    getElements();

  function addLinks (cells, p) {
    for (var i = 1; i < cells.length; i++) {
      if (d.pages.item(cells[i].contents).isValid) {
        d.hyperlinks.add (
          d.hyperlinkTextSources.add (cells[i].texts[0]),
          d.hyperlinkPageDestinations.add (d.pages.item(cells[i].contents))
        );
      }
    }
  }

  for (var i = 0; i < columns.length; i++) {
    if (columns[i].cells[0].contents === 'Page') {
      addLinks (columns[i].cells.everyItem().getElements());
    }
  }

}());
Known Participant
March 11, 2024

Peter Kahrel, 

This is superb  !!!!! 

You saved lot of my time.... I realy appreaciate your creativity... This works fine.... 

oh.... how powerful this script....

Thank you very much.... _/|\_ 

Known Participant
March 11, 2024

Peter Kahrel, 

I think to develop this script as using multiple files in a book folder. 

If I want to link that numbers to another ID file, but in a same book folder, how can I do that? 

If  so, I think this scrip more and more useful.