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.

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);
  • Thanks! The complete sequence is thus: let positions = []; body.getParagraphs().forEach( par => positions.push (doc.newPosition (par, 0) ) ); – Jean-Rene Bouvier Feb 22 '17 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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