Google-docs – General bookmark for url link for Google docs

bookmarksgoogle docslinks

I understand that you can insert a bookmark into Google docs and link to it with the url. Also if you have headings inside the document, each heading has it's own url.

My question is: is there a generic form of it?

For example. Let's say I have 50 documents, in each document, I will have a heading titled "questions". Currently the url is unique for each different doc for the heading "questions". Is there a way to have a generic formula for that heading and I could just swap out the file ID when generating a url link?

Best Answer

Experiment shows the generation of bookmark Id does not follow any simple rule; Google may be using some hash function for this purpose.

I created two bookmarks in one document, they got Ids
id.bp1zcvxln5kg and id.f93qmv8z8g1s. Then created a bookmark in another, it got Id id.apnggattoj3a. Beyond the initial id., there is no pattern here.

If you need to generate URLs of bookmarks programmatically, a script can help. For example, this function logs the URLs of all bookmarks in the active document.

function bookmarks() {
  var doc = DocumentApp.getActiveDocument();
  var docId = doc.getId();
  var bookmarkUrls = doc.getBookmarks().map(function(bookmark) {
    return 'https://docs.google.com/document/d/' + docId + '/edit#bookmark=' + bookmark.getId();
  });
  Logger.log(bookmarkUrls);  
}