Class Position

  • APosition represents a location in a document relative to a specific element.

  • Scripts can only access the cursor of the user running the script if the script is bound to the document.

  • ThePosition class includes methods to get the containing element, its offset, surrounding text, insert bookmarks, inline images, and text.

  • Methods likegetOffset(),getSurroundingText(), and insertion methods may require specific authorization scopes.

Position

A reference to a location in the document tab, relative to a specific element. The user's cursoris represented as aPosition, among other uses. Scripts can only access the cursor of theuser who is running the script, and only if the script isbound to the document.

// Insert some text at the cursor position and make it bold.constcursor=DocumentApp.getActiveDocument().getCursor();if(cursor){// Attempt to insert text at the cursor position. If the insertion returns// null, the cursor's containing element doesn't allow insertions, so show the// user an error message.constelement=cursor.insertText('ಠ‿ಠ');if(element){element.setBold(true);}else{DocumentApp.getUi().alert('Cannot insert text here.');}}else{DocumentApp.getUi().alert('Cannot find a cursor.');}

Methods

MethodReturn typeBrief description
getElement()ElementGets the element that contains thisPosition.
getOffset()IntegerGets thisPosition's relative location within the element that contains it.
getSurroundingText()TextCreates an artificialText element that represents the text and formatting of theParagraph orListItem that contains thePosition, either directly or through a chain of child elements.
getSurroundingTextOffset()IntegerGets the offset of thisPosition within theText element returned bygetSurroundingText().
insertBookmark()BookmarkCreates and inserts a newBookmark at thisPosition.
insertInlineImage(image)InlineImage|nullCreates and inserts a newInlineImage at thisPosition from the specified imageblob.
insertText(text)Text|nullInserts the specified text at thisPosition.

Detailed documentation

getElement()

Gets the element that contains thisPosition. This will be either aTextelement or a container element likeParagraph. In either case, the relativeposition within the element can be determined withgetOffset().

Return

Element — the container orText element in which thisPosition object is located


getOffset()

Gets thisPosition's relative location within the element that contains it. If theelement is aText element, the offset is the number of characters before thePosition (that is, the index of the character after thisPosition); for any otherelement, the offset is the number of child elements before thisPosition within thesame container element (that is, the index of the child element after thePosition).

Return

Integer — forText elements, the number of characters before thisPosition; for other elements, the number of child elements before thisPosition within the same container element

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getSurroundingText()

Creates an artificialText element that represents the text and formatting of theParagraph orListItem that contains thePosition, either directly or through a chain of child elements. To determine thePosition's offset in the returnedText element, usegetSurroundingTextOffset().

Return

Text — an element equivalent to the result of callingeditAsText() on theParagraph orListItem that contains thePosition, either directly or through a chain of child elements

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getSurroundingTextOffset()

Gets the offset of thisPosition within theText element returned bygetSurroundingText(). The offset is the number of characters before thePosition(that is, the index of the character after thisPosition).

Return

Integer — the number of characters before thisPosition in theParagraph orListItem that contains thePosition, either directly or through a chain of child elements

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertBookmark()

Creates and inserts a newBookmark at thisPosition.

Return

Bookmark — the new bookmark

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertInlineImage(image)

Creates and inserts a newInlineImage at thisPosition from the specified imageblob.

Parameters

NameTypeDescription
imageBlobSourcethe image data to insert at thisPosition

Return

InlineImage|null — the new image element, ornull if the element in which thisPosition is located does not allow images to be inserted

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertText(text)

Inserts the specified text at thisPosition. This method creates a newTextelement, even if the string is inserted within an existingText element, so that it iseasy to style the new element.

Parameters

NameTypeDescription
textStringthe string to insert at thisPosition

Return

Text|null — the new text element, ornull if the element in which thisPosition is located does not allow text to be inserted

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.