1

Using GAS, I'm trying to navigate paragraphs in a google doc and record their starting position so that I can display an interactive navigation menu in a sidebar. But I can't find any GAS method that returns the position of a paragraph (or an element for that matter), except when the cursor is within that paragraph. I thought of inserting bookmarks to retrieve their positions, but it seems that one cannot insert a bookmark without having first a position…

Am I missing anything?

Note that I'm aware of the fact that headings have hash locations (except for normal paragraphs) that are easy to retrieve using a TOC; but I can't use these hash locations directly in any GAS add-on, because the caja compiler prevents it. Besides, I'd like to list more than headings in my navigation menu.

1 Answer 1

2

Use the Document.newPosition(element, offset) method, where element is your Paragraph object and offset is 0.

For example:

var newParagraph = body.appendParagraph("some text");
var paragraphPosition = doc.newPosition(newParagraph, 0);
1
  • Thanks! The complete sequence is thus: let positions = []; body.getParagraphs().forEach( par => positions.push (doc.newPosition (par, 0) ) ); Feb 22, 2017 at 19:28

Your Answer

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

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