error on removing bookmark

104 views
Skip to first unread message

Ashish Burman

unread,
Jan 4, 2022, 9:58:43 AM1/4/22
to Google Apps Script Community
the exact error msg is 

Exception: Unexpected error while getting the method or property remove on object DocumentApp.Bookmark.
when i am trying to get a book mark from the document by bookmark id which i have already stored some where. There are 3-4 bookmarks in the doc and issue is there only for 3rd and 4th bookmark. Please help.

Clark Lind

unread,
Jan 5, 2022, 12:25:23 PM1/5/22
to Google Apps Script Community
If you are removing bookmarks with script, you will have to remove them from last to first, not first to last. (This is just a hunch of what may be going on for you). 

Ashish Burman

unread,
Jan 6, 2022, 5:53:42 AM1/6/22
to Google Apps Script Community
Hi, 
here is the code snippet, which I wrote.

var bookmark = DocumentApp.getActiveDocument().getBookmark(bookmarkId);
        if(bookmark){
        bookmark.getPosition().insertText(busnessText);
        bookmark.remove();
}

here the order in which i am getting the bookmark ids is not a concern for me. Ok as you said i'll make sure to access bookmarks from reverse. thanks.

Clark Lind

unread,
Jan 6, 2022, 9:37:38 AM1/6/22
to google-apps-sc...@googlegroups.com
Without seeing more of your code, I am just speculating. 
This is how I tried to reproduce a little of what you are doing. I don't know where "busnessText" is coming from, so I just used some lorem text. This works for me:
function getBookmarkIds() {
    var bookmarkIds = [];
    DocumentApp.getActiveDocument()
        .getBookmarks()
        .forEach((bm) => {
            bookmarkIds.push(bm.getId())
        });
    return bookmarkIds;
}

function main() {
    var busnessText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`

    var ids = getBookmarkIds();
    ids.forEach((id) => {
        var bookmark = DocumentApp.getActiveDocument().getBookmark(id);
        if (bookmark) {
            bookmark.getPosition().insertText(busnessText);
            bookmark.remove();
        }
    })
}

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/-_jk313R4kk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/a50b9a0b-7068-44a0-9697-73806c8434ban%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages