I am having trouble moving the cursor to bookmarked locations in Google script.  Overview: I want episodically to update numbers in a Google doc via a Google apps script.  Basically I’m trying to make a hacky alternative to one of RMarkdown’s virtues.  Each formatted number in my Google doc, or target, corresponds to an r character variable exported into a Google sheet.   Here’s what I have so far, though I’m open to other approaches.

  One space back from the start of each target I place a bookmark.  For matching the new value for each target, the target needs a name.  But bookmarks in google docs apparently can’t be named.  So at the end of the document I made a list.  Each element of the list is a concatenation of a find string (“live.”) and the name of the target variable.  Each element is linked back to the appropriate bookmark.  It’s not ideal since I must insert some stray spaces but I can live with that.  Here is a test document:

Text before first target[bookmark1] 77% text between targets in same paragraph [bookmark2] 1,234 and this goes on for several lines.

[bookmark3] 55 paragraph 2 must start with an unwanted space.

And skipping to the end,

live.formatted.number[linked to bookmark 2]

live.a.percent[linked to bookmark1]

live.plain.number[linked to bookmark 3]

  The Google sheet with updates has in column A “formatted.number”, “plain.number”, “a.percent” and in column B “4,321”, “54.5”, “71%”.

  The following script retrieves the bookmark id for each target.  Then I build a url to the bookmark.  If I manually copy the url and paste it into a browser window, the document opens with the cursor at the bookmark.  But I haven’t figured out how to move the cursor to the bookmark using the script.  A couple attempts that don't work are shown.  In case it helps to know, my intent after I move the cursor is to define a range that starts at the bookmark and includes a leading space plus the target which has no spaces plus a trailing space.  I will pad the matching replacement string with a space on either end and use it to replace the old version of each target.


function updateNumbers () {
  var doc = DocumentApp.getActiveDocument()
    bodyText = doc.getBody();
    foundElement = bodyText.findText ("Live.");  // Uses tag I chose to index dynamic text
  while (foundElement !== null) {
    var foundText = foundElement.getElement().asText();
    var bkMarkUrl = foundText.getLinkUrl();
    foundBkMark = doc.getBookmark(bkMarkUrl);  //fails:  returns null
    Logger.log("foundBkMark " + foundBkMark)
    var bkMarkLink = “https://docs.google.com/document/d/${doc.getId()}/${bkMarkUrl}”;
    Logger.log("bkMarkLink " + bkMarkLink);  // works only pasted into browser manually
    var cursor = DocumentApp.getActiveDocument().getCursor();
    var position = doc.newPosition(bkMarklink, 2); //  Generates error. 
    var sometext = cursor.insertText("test");
    // Find the next match
    foundElement = bodyText.findText("Live.", foundElement);

  More options that don’t work:

The document is too complicated to turn it into a form.

I don’t want to assume that either the list of bookmark links or the Google sheet has the targets in any particular order. The document will go through numerous revisions and wrong order is exactly the kind of error opportunity I'm trying to minimize by automating updates.  So I can’t just cycle through the bookmarks in order.


1 Answer 1


I've reread the question several times and still have no idea what you're trying to do. As a guess, here is the way how to set the cursor at the first bookmark:

function jump_to_bookmark() {
  var doc = DocumentApp.getActiveDocument();
  var bookmarks = doc.getBookmarks();
  var bookmark = bookmarks[0];           // get the first bookmark
  doc.setCursor(bookmark.getPosition()); // set the cursor at the bookmark

If you need a bookmarks with particular ID you can check if the bookmark has the wanted ID or not with the method bookmark.getId().

But it's fairly basic knowledge. Probably I've missed something in your question.

  • 1
    Thank you. I was trying to use replacement values stored in a spreadsheet to update text at bookmarked locations in a Google Document. Neither ranges nor bookmarks can be named in Google Docs, as best I can tell bookmark locations get messed up if the number of characters preceding the bookmark in the same paragraph changes, the uncontrollable order in which a script cycles bookmarks is wonky, and the default padding of spaces to add and delete along with selected text makes it tricky to retain bookmarks. I am going back to MSWord! But I do really appreciate your help.
    – InColorado
    Sep 8, 2022 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.