简体   繁体   中英

Google Apps Script - How to get the bookmark link?

I would like to get the bookmark link after I run addBookmark() on Google Docs. I see that we cannot use the getUrl() for the bookmark. I'm thinking of using the id of bookmark but I am having trouble on the url.

My current code to get the bookmark link looks like this.

doc = DocumentApp.getActiveDocument();
bookmark = doc.addBookmark(position);
let bookmarklink = String(doc.getUrl() + '/edit#bookmark=' + boomark.getId());

Is there any other implementation that is more dynamic? I would like for it to work on test (test deploy), my problem is that I get a different url from doc.getUrl() on test.

thanks!

It seems that DocumentApp.getActiveDocument().getUrl() returns the URL like https://docs.google.com/open?id={fileId} . On the other hand, the hyperlink of bookmark is like https://docs.google.com/document/d/{fileId}/edit#bookmark=id.{bookmarkId} . I thought that this might be the reason for your current issue. If you want to retrieve the bookmark URL, how about the following modification?

From:

let bookmarklink = String(doc.getUrl() + '/edit#bookmark=' + boomark.getId());

To:

let bookmarklink = DriveApp.getFileById(doc.getId()).getUrl().replace("?usp=drivesdk", "#bookmark=" + boomark.getId());

or

let bookmarklink = `https://docs.google.com/document/d/${doc.getId()}/edit#bookmark=${boomark.getId()}`;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM